LLVM API Documentation

ScalarEvolutionExpressions.h
Go to the documentation of this file.
00001 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the classes used to represent and build scalar expressions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
00015 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
00016 
00017 #include "llvm/ADT/SmallPtrSet.h"
00018 #include "llvm/Analysis/ScalarEvolution.h"
00019 #include "llvm/Support/ErrorHandling.h"
00020 
00021 namespace llvm {
00022   class ConstantInt;
00023   class ConstantRange;
00024   class DominatorTree;
00025 
00026   enum SCEVTypes {
00027     // These should be ordered in terms of increasing complexity to make the
00028     // folders simpler.
00029     scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
00030     scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr,
00031     scUnknown, scCouldNotCompute
00032   };
00033 
00034   //===--------------------------------------------------------------------===//
00035   /// SCEVConstant - This class represents a constant integer value.
00036   ///
00037   class SCEVConstant : public SCEV {
00038     friend class ScalarEvolution;
00039 
00040     ConstantInt *V;
00041     SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
00042       SCEV(ID, scConstant), V(v) {}
00043   public:
00044     ConstantInt *getValue() const { return V; }
00045 
00046     Type *getType() const { return V->getType(); }
00047 
00048     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00049     static inline bool classof(const SCEV *S) {
00050       return S->getSCEVType() == scConstant;
00051     }
00052   };
00053 
00054   //===--------------------------------------------------------------------===//
00055   /// SCEVCastExpr - This is the base class for unary cast operator classes.
00056   ///
00057   class SCEVCastExpr : public SCEV {
00058   protected:
00059     const SCEV *Op;
00060     Type *Ty;
00061 
00062     SCEVCastExpr(const FoldingSetNodeIDRef ID,
00063                  unsigned SCEVTy, const SCEV *op, Type *ty);
00064 
00065   public:
00066     const SCEV *getOperand() const { return Op; }
00067     Type *getType() const { return Ty; }
00068 
00069     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00070     static inline bool classof(const SCEV *S) {
00071       return S->getSCEVType() == scTruncate ||
00072              S->getSCEVType() == scZeroExtend ||
00073              S->getSCEVType() == scSignExtend;
00074     }
00075   };
00076 
00077   //===--------------------------------------------------------------------===//
00078   /// SCEVTruncateExpr - This class represents a truncation of an integer value
00079   /// to a smaller integer value.
00080   ///
00081   class SCEVTruncateExpr : public SCEVCastExpr {
00082     friend class ScalarEvolution;
00083 
00084     SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
00085                      const SCEV *op, Type *ty);
00086 
00087   public:
00088     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00089     static inline bool classof(const SCEV *S) {
00090       return S->getSCEVType() == scTruncate;
00091     }
00092   };
00093 
00094   //===--------------------------------------------------------------------===//
00095   /// SCEVZeroExtendExpr - This class represents a zero extension of a small
00096   /// integer value to a larger integer value.
00097   ///
00098   class SCEVZeroExtendExpr : public SCEVCastExpr {
00099     friend class ScalarEvolution;
00100 
00101     SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
00102                        const SCEV *op, Type *ty);
00103 
00104   public:
00105     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00106     static inline bool classof(const SCEV *S) {
00107       return S->getSCEVType() == scZeroExtend;
00108     }
00109   };
00110 
00111   //===--------------------------------------------------------------------===//
00112   /// SCEVSignExtendExpr - This class represents a sign extension of a small
00113   /// integer value to a larger integer value.
00114   ///
00115   class SCEVSignExtendExpr : public SCEVCastExpr {
00116     friend class ScalarEvolution;
00117 
00118     SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
00119                        const SCEV *op, Type *ty);
00120 
00121   public:
00122     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00123     static inline bool classof(const SCEV *S) {
00124       return S->getSCEVType() == scSignExtend;
00125     }
00126   };
00127 
00128 
00129   //===--------------------------------------------------------------------===//
00130   /// SCEVNAryExpr - This node is a base class providing common
00131   /// functionality for n'ary operators.
00132   ///
00133   class SCEVNAryExpr : public SCEV {
00134   protected:
00135     // Since SCEVs are immutable, ScalarEvolution allocates operand
00136     // arrays with its SCEVAllocator, so this class just needs a simple
00137     // pointer rather than a more elaborate vector-like data structure.
00138     // This also avoids the need for a non-trivial destructor.
00139     const SCEV *const *Operands;
00140     size_t NumOperands;
00141 
00142     SCEVNAryExpr(const FoldingSetNodeIDRef ID,
00143                  enum SCEVTypes T, const SCEV *const *O, size_t N)
00144       : SCEV(ID, T), Operands(O), NumOperands(N) {}
00145 
00146   public:
00147     size_t getNumOperands() const { return NumOperands; }
00148     const SCEV *getOperand(unsigned i) const {
00149       assert(i < NumOperands && "Operand index out of range!");
00150       return Operands[i];
00151     }
00152 
00153     typedef const SCEV *const *op_iterator;
00154     op_iterator op_begin() const { return Operands; }
00155     op_iterator op_end() const { return Operands + NumOperands; }
00156 
00157     Type *getType() const { return getOperand(0)->getType(); }
00158 
00159     NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const {
00160       return (NoWrapFlags)(SubclassData & Mask);
00161     }
00162 
00163     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00164     static inline bool classof(const SCEV *S) {
00165       return S->getSCEVType() == scAddExpr ||
00166              S->getSCEVType() == scMulExpr ||
00167              S->getSCEVType() == scSMaxExpr ||
00168              S->getSCEVType() == scUMaxExpr ||
00169              S->getSCEVType() == scAddRecExpr;
00170     }
00171   };
00172 
00173   //===--------------------------------------------------------------------===//
00174   /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
00175   /// operators.
00176   ///
00177   class SCEVCommutativeExpr : public SCEVNAryExpr {
00178   protected:
00179     SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
00180                         enum SCEVTypes T, const SCEV *const *O, size_t N)
00181       : SCEVNAryExpr(ID, T, O, N) {}
00182 
00183   public:
00184     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00185     static inline bool classof(const SCEV *S) {
00186       return S->getSCEVType() == scAddExpr ||
00187              S->getSCEVType() == scMulExpr ||
00188              S->getSCEVType() == scSMaxExpr ||
00189              S->getSCEVType() == scUMaxExpr;
00190     }
00191 
00192     /// Set flags for a non-recurrence without clearing previously set flags.
00193     void setNoWrapFlags(NoWrapFlags Flags) {
00194       SubclassData |= Flags;
00195     }
00196   };
00197 
00198 
00199   //===--------------------------------------------------------------------===//
00200   /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
00201   ///
00202   class SCEVAddExpr : public SCEVCommutativeExpr {
00203     friend class ScalarEvolution;
00204 
00205     SCEVAddExpr(const FoldingSetNodeIDRef ID,
00206                 const SCEV *const *O, size_t N)
00207       : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
00208     }
00209 
00210   public:
00211     Type *getType() const {
00212       // Use the type of the last operand, which is likely to be a pointer
00213       // type, if there is one. This doesn't usually matter, but it can help
00214       // reduce casts when the expressions are expanded.
00215       return getOperand(getNumOperands() - 1)->getType();
00216     }
00217 
00218     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00219     static inline bool classof(const SCEV *S) {
00220       return S->getSCEVType() == scAddExpr;
00221     }
00222   };
00223 
00224   //===--------------------------------------------------------------------===//
00225   /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
00226   ///
00227   class SCEVMulExpr : public SCEVCommutativeExpr {
00228     friend class ScalarEvolution;
00229 
00230     SCEVMulExpr(const FoldingSetNodeIDRef ID,
00231                 const SCEV *const *O, size_t N)
00232       : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
00233     }
00234 
00235   public:
00236     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00237     static inline bool classof(const SCEV *S) {
00238       return S->getSCEVType() == scMulExpr;
00239     }
00240   };
00241 
00242 
00243   //===--------------------------------------------------------------------===//
00244   /// SCEVUDivExpr - This class represents a binary unsigned division operation.
00245   ///
00246   class SCEVUDivExpr : public SCEV {
00247     friend class ScalarEvolution;
00248 
00249     const SCEV *LHS;
00250     const SCEV *RHS;
00251     SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
00252       : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
00253 
00254   public:
00255     const SCEV *getLHS() const { return LHS; }
00256     const SCEV *getRHS() const { return RHS; }
00257 
00258     Type *getType() const {
00259       // In most cases the types of LHS and RHS will be the same, but in some
00260       // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
00261       // depend on the type for correctness, but handling types carefully can
00262       // avoid extra casts in the SCEVExpander. The LHS is more likely to be
00263       // a pointer type than the RHS, so use the RHS' type here.
00264       return getRHS()->getType();
00265     }
00266 
00267     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00268     static inline bool classof(const SCEV *S) {
00269       return S->getSCEVType() == scUDivExpr;
00270     }
00271   };
00272 
00273 
00274   //===--------------------------------------------------------------------===//
00275   /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
00276   /// count of the specified loop.  This is the primary focus of the
00277   /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
00278   /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
00279   /// created and analyzed.
00280   ///
00281   /// All operands of an AddRec are required to be loop invariant.
00282   ///
00283   class SCEVAddRecExpr : public SCEVNAryExpr {
00284     friend class ScalarEvolution;
00285 
00286     const Loop *L;
00287 
00288     SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
00289                    const SCEV *const *O, size_t N, const Loop *l)
00290       : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {}
00291 
00292   public:
00293     const SCEV *getStart() const { return Operands[0]; }
00294     const Loop *getLoop() const { return L; }
00295 
00296     /// getStepRecurrence - This method constructs and returns the recurrence
00297     /// indicating how much this expression steps by.  If this is a polynomial
00298     /// of degree N, it returns a chrec of degree N-1.
00299     /// We cannot determine whether the step recurrence has self-wraparound.
00300     const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
00301       if (isAffine()) return getOperand(1);
00302       return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
00303                                                            op_end()),
00304                               getLoop(), FlagAnyWrap);
00305     }
00306 
00307     /// isAffine - Return true if this is an affine AddRec (i.e., it represents
00308     /// an expressions A+B*x where A and B are loop invariant values.
00309     bool isAffine() const {
00310       // We know that the start value is invariant.  This expression is thus
00311       // affine iff the step is also invariant.
00312       return getNumOperands() == 2;
00313     }
00314 
00315     /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
00316     /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
00317     /// invariant values.  This corresponds to an addrec of the form {L,+,M,+,N}
00318     bool isQuadratic() const {
00319       return getNumOperands() == 3;
00320     }
00321 
00322     /// Set flags for a recurrence without clearing any previously set flags.
00323     /// For AddRec, either NUW or NSW implies NW. Keep track of this fact here
00324     /// to make it easier to propagate flags.
00325     void setNoWrapFlags(NoWrapFlags Flags) {
00326       if (Flags & (FlagNUW | FlagNSW))
00327         Flags = ScalarEvolution::setFlags(Flags, FlagNW);
00328       SubclassData |= Flags;
00329     }
00330 
00331     /// evaluateAtIteration - Return the value of this chain of recurrences at
00332     /// the specified iteration number.
00333     const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
00334 
00335     /// getNumIterationsInRange - Return the number of iterations of this loop
00336     /// that produce values in the specified constant range.  Another way of
00337     /// looking at this is that it returns the first iteration number where the
00338     /// value is not in the condition, thus computing the exit count.  If the
00339     /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
00340     /// returned.
00341     const SCEV *getNumIterationsInRange(ConstantRange Range,
00342                                        ScalarEvolution &SE) const;
00343 
00344     /// getPostIncExpr - Return an expression representing the value of
00345     /// this expression one iteration of the loop ahead.
00346     const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
00347       return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
00348     }
00349 
00350     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00351     static inline bool classof(const SCEV *S) {
00352       return S->getSCEVType() == scAddRecExpr;
00353     }
00354   };
00355 
00356 
00357   //===--------------------------------------------------------------------===//
00358   /// SCEVSMaxExpr - This class represents a signed maximum selection.
00359   ///
00360   class SCEVSMaxExpr : public SCEVCommutativeExpr {
00361     friend class ScalarEvolution;
00362 
00363     SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
00364                  const SCEV *const *O, size_t N)
00365       : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
00366       // Max never overflows.
00367       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
00368     }
00369 
00370   public:
00371     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00372     static inline bool classof(const SCEV *S) {
00373       return S->getSCEVType() == scSMaxExpr;
00374     }
00375   };
00376 
00377 
00378   //===--------------------------------------------------------------------===//
00379   /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
00380   ///
00381   class SCEVUMaxExpr : public SCEVCommutativeExpr {
00382     friend class ScalarEvolution;
00383 
00384     SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
00385                  const SCEV *const *O, size_t N)
00386       : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
00387       // Max never overflows.
00388       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
00389     }
00390 
00391   public:
00392     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00393     static inline bool classof(const SCEV *S) {
00394       return S->getSCEVType() == scUMaxExpr;
00395     }
00396   };
00397 
00398   //===--------------------------------------------------------------------===//
00399   /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
00400   /// value, and only represent it as its LLVM Value.  This is the "bottom"
00401   /// value for the analysis.
00402   ///
00403   class SCEVUnknown : public SCEV, private CallbackVH {
00404     friend class ScalarEvolution;
00405 
00406     // Implement CallbackVH.
00407     virtual void deleted();
00408     virtual void allUsesReplacedWith(Value *New);
00409 
00410     /// SE - The parent ScalarEvolution value. This is used to update
00411     /// the parent's maps when the value associated with a SCEVUnknown
00412     /// is deleted or RAUW'd.
00413     ScalarEvolution *SE;
00414 
00415     /// Next - The next pointer in the linked list of all
00416     /// SCEVUnknown instances owned by a ScalarEvolution.
00417     SCEVUnknown *Next;
00418 
00419     SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
00420                 ScalarEvolution *se, SCEVUnknown *next) :
00421       SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
00422 
00423   public:
00424     Value *getValue() const { return getValPtr(); }
00425 
00426     /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
00427     /// constant representing a type size, alignment, or field offset in
00428     /// a target-independent manner, and hasn't happened to have been
00429     /// folded with other operations into something unrecognizable. This
00430     /// is mainly only useful for pretty-printing and other situations
00431     /// where it isn't absolutely required for these to succeed.
00432     bool isSizeOf(Type *&AllocTy) const;
00433     bool isAlignOf(Type *&AllocTy) const;
00434     bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
00435 
00436     Type *getType() const { return getValPtr()->getType(); }
00437 
00438     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00439     static inline bool classof(const SCEV *S) {
00440       return S->getSCEVType() == scUnknown;
00441     }
00442   };
00443 
00444   /// SCEVVisitor - This class defines a simple visitor class that may be used
00445   /// for various SCEV analysis purposes.
00446   template<typename SC, typename RetVal=void>
00447   struct SCEVVisitor {
00448     RetVal visit(const SCEV *S) {
00449       switch (S->getSCEVType()) {
00450       case scConstant:
00451         return ((SC*)this)->visitConstant((const SCEVConstant*)S);
00452       case scTruncate:
00453         return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
00454       case scZeroExtend:
00455         return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
00456       case scSignExtend:
00457         return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
00458       case scAddExpr:
00459         return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
00460       case scMulExpr:
00461         return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
00462       case scUDivExpr:
00463         return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
00464       case scAddRecExpr:
00465         return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
00466       case scSMaxExpr:
00467         return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
00468       case scUMaxExpr:
00469         return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
00470       case scUnknown:
00471         return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
00472       case scCouldNotCompute:
00473         return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
00474       default:
00475         llvm_unreachable("Unknown SCEV type!");
00476       }
00477     }
00478 
00479     RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
00480       llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
00481     }
00482   };
00483 
00484   /// Visit all nodes in the expression tree using worklist traversal.
00485   ///
00486   /// Visitor implements:
00487   ///   // return true to follow this node.
00488   ///   bool follow(const SCEV *S);
00489   ///   // return true to terminate the search.
00490   ///   bool isDone();
00491   template<typename SV>
00492   class SCEVTraversal {
00493     SV &Visitor;
00494     SmallVector<const SCEV *, 8> Worklist;
00495     SmallPtrSet<const SCEV *, 8> Visited;
00496 
00497     void push(const SCEV *S) {
00498       if (Visited.insert(S) && Visitor.follow(S))
00499         Worklist.push_back(S);
00500     }
00501   public:
00502     SCEVTraversal(SV& V): Visitor(V) {}
00503 
00504     void visitAll(const SCEV *Root) {
00505       push(Root);
00506       while (!Worklist.empty() && !Visitor.isDone()) {
00507         const SCEV *S = Worklist.pop_back_val();
00508 
00509         switch (S->getSCEVType()) {
00510         case scConstant:
00511         case scUnknown:
00512           break;
00513         case scTruncate:
00514         case scZeroExtend:
00515         case scSignExtend:
00516           push(cast<SCEVCastExpr>(S)->getOperand());
00517           break;
00518         case scAddExpr:
00519         case scMulExpr:
00520         case scSMaxExpr:
00521         case scUMaxExpr:
00522         case scAddRecExpr: {
00523           const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
00524           for (SCEVNAryExpr::op_iterator I = NAry->op_begin(),
00525                  E = NAry->op_end(); I != E; ++I) {
00526             push(*I);
00527           }
00528           break;
00529         }
00530         case scUDivExpr: {
00531           const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
00532           push(UDiv->getLHS());
00533           push(UDiv->getRHS());
00534           break;
00535         }
00536         case scCouldNotCompute:
00537           llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
00538         default:
00539           llvm_unreachable("Unknown SCEV kind!");
00540         }
00541       }
00542     }
00543   };
00544 
00545   /// Use SCEVTraversal to visit all nodes in the givien expression tree.
00546   template<typename SV>
00547   void visitAll(const SCEV *Root, SV& Visitor) {
00548     SCEVTraversal<SV> T(Visitor);
00549     T.visitAll(Root);
00550   }
00551 
00552   /// The SCEVRewriter takes a scalar evolution expression and copies all its
00553   /// components. The result after a rewrite is an identical SCEV.
00554   struct SCEVRewriter
00555     : public SCEVVisitor<SCEVRewriter, const SCEV*> {
00556   public:
00557     SCEVRewriter(ScalarEvolution &S) : SE(S) {}
00558 
00559     virtual ~SCEVRewriter() {}
00560 
00561     virtual const SCEV *visitConstant(const SCEVConstant *Constant) {
00562       return Constant;
00563     }
00564 
00565     virtual const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
00566       const SCEV *Operand = visit(Expr->getOperand());
00567       return SE.getTruncateExpr(Operand, Expr->getType());
00568     }
00569 
00570     virtual const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
00571       const SCEV *Operand = visit(Expr->getOperand());
00572       return SE.getZeroExtendExpr(Operand, Expr->getType());
00573     }
00574 
00575     virtual const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
00576       const SCEV *Operand = visit(Expr->getOperand());
00577       return SE.getSignExtendExpr(Operand, Expr->getType());
00578     }
00579 
00580     virtual const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
00581       SmallVector<const SCEV *, 2> Operands;
00582       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00583         Operands.push_back(visit(Expr->getOperand(i)));
00584       return SE.getAddExpr(Operands);
00585     }
00586 
00587     virtual const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
00588       SmallVector<const SCEV *, 2> Operands;
00589       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00590         Operands.push_back(visit(Expr->getOperand(i)));
00591       return SE.getMulExpr(Operands);
00592     }
00593 
00594     virtual const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
00595       return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
00596     }
00597 
00598     virtual const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
00599       SmallVector<const SCEV *, 2> Operands;
00600       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00601         Operands.push_back(visit(Expr->getOperand(i)));
00602       return SE.getAddRecExpr(Operands, Expr->getLoop(),
00603                               Expr->getNoWrapFlags());
00604     }
00605 
00606     virtual const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
00607       SmallVector<const SCEV *, 2> Operands;
00608       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00609         Operands.push_back(visit(Expr->getOperand(i)));
00610       return SE.getSMaxExpr(Operands);
00611     }
00612 
00613     virtual const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
00614       SmallVector<const SCEV *, 2> Operands;
00615       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00616         Operands.push_back(visit(Expr->getOperand(i)));
00617       return SE.getUMaxExpr(Operands);
00618     }
00619 
00620     virtual const SCEV *visitUnknown(const SCEVUnknown *Expr) {
00621       return Expr;
00622     }
00623 
00624     virtual const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
00625       return Expr;
00626     }
00627 
00628   protected:
00629     ScalarEvolution &SE;
00630   };
00631 
00632   typedef DenseMap<const Value*, Value*> ValueToValueMap;
00633 
00634   /// The SCEVParameterRewriter takes a scalar evolution expression and updates
00635   /// the SCEVUnknown components following the Map (Value -> Value).
00636   struct SCEVParameterRewriter: public SCEVRewriter {
00637   public:
00638     static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
00639                                ValueToValueMap &Map) {
00640       SCEVParameterRewriter Rewriter(SE, Map);
00641       return Rewriter.visit(Scev);
00642     }
00643     SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M)
00644       : SCEVRewriter(S), Map(M) {}
00645 
00646     virtual const SCEV *visitUnknown(const SCEVUnknown *Expr) {
00647       Value *V = Expr->getValue();
00648       if (Map.count(V))
00649         return SE.getUnknown(Map[V]);
00650       return Expr;
00651     }
00652 
00653   private:
00654     ValueToValueMap &Map;
00655   };
00656 
00657   typedef DenseMap<const Loop*, const SCEV*> LoopToScevMapT;
00658 
00659   /// The SCEVApplyRewriter takes a scalar evolution expression and applies
00660   /// the Map (Loop -> SCEV) to all AddRecExprs.
00661   struct SCEVApplyRewriter: public SCEVRewriter {
00662   public:
00663     static const SCEV *rewrite(const SCEV *Scev, LoopToScevMapT &Map,
00664                                ScalarEvolution &SE) {
00665       SCEVApplyRewriter Rewriter(SE, Map);
00666       return Rewriter.visit(Scev);
00667     }
00668     SCEVApplyRewriter(ScalarEvolution &S, LoopToScevMapT &M)
00669       : SCEVRewriter(S), Map(M) {}
00670 
00671     virtual const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
00672       SmallVector<const SCEV *, 2> Operands;
00673       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00674         Operands.push_back(visit(Expr->getOperand(i)));
00675 
00676       const Loop *L = Expr->getLoop();
00677       const SCEV *Res = SE.getAddRecExpr(Operands, L, Expr->getNoWrapFlags());
00678 
00679       if (0 == Map.count(L))
00680         return Res;
00681 
00682       const SCEVAddRecExpr *Rec = (const SCEVAddRecExpr *) Res;
00683       return Rec->evaluateAtIteration(Map[L], SE);
00684     }
00685 
00686   private:
00687     LoopToScevMapT &Map;
00688   };
00689 
00690 /// Applies the Map (Loop -> SCEV) to the given Scev.
00691 static inline const SCEV *apply(const SCEV *Scev, LoopToScevMapT &Map,
00692                                 ScalarEvolution &SE) {
00693   return SCEVApplyRewriter::rewrite(Scev, Map, SE);
00694 }
00695 
00696 }
00697 
00698 #endif