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