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     /// Splits the SCEV into two vectors of SCEVs representing the subscripts
00356     /// and sizes of an array access. Returns the remainder of the
00357     /// delinearization that is the offset start of the array.
00358     const SCEV *delinearize(ScalarEvolution &SE,
00359                             SmallVectorImpl<const SCEV *> &Subscripts,
00360                             SmallVectorImpl<const SCEV *> &Sizes) const;
00361   };
00362 
00363   //===--------------------------------------------------------------------===//
00364   /// SCEVSMaxExpr - This class represents a signed maximum selection.
00365   ///
00366   class SCEVSMaxExpr : public SCEVCommutativeExpr {
00367     friend class ScalarEvolution;
00368 
00369     SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
00370                  const SCEV *const *O, size_t N)
00371       : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
00372       // Max never overflows.
00373       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
00374     }
00375 
00376   public:
00377     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00378     static inline bool classof(const SCEV *S) {
00379       return S->getSCEVType() == scSMaxExpr;
00380     }
00381   };
00382 
00383 
00384   //===--------------------------------------------------------------------===//
00385   /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
00386   ///
00387   class SCEVUMaxExpr : public SCEVCommutativeExpr {
00388     friend class ScalarEvolution;
00389 
00390     SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
00391                  const SCEV *const *O, size_t N)
00392       : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
00393       // Max never overflows.
00394       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
00395     }
00396 
00397   public:
00398     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00399     static inline bool classof(const SCEV *S) {
00400       return S->getSCEVType() == scUMaxExpr;
00401     }
00402   };
00403 
00404   //===--------------------------------------------------------------------===//
00405   /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
00406   /// value, and only represent it as its LLVM Value.  This is the "bottom"
00407   /// value for the analysis.
00408   ///
00409   class SCEVUnknown : public SCEV, private CallbackVH {
00410     friend class ScalarEvolution;
00411 
00412     // Implement CallbackVH.
00413     void deleted() override;
00414     void allUsesReplacedWith(Value *New) override;
00415 
00416     /// SE - The parent ScalarEvolution value. This is used to update
00417     /// the parent's maps when the value associated with a SCEVUnknown
00418     /// is deleted or RAUW'd.
00419     ScalarEvolution *SE;
00420 
00421     /// Next - The next pointer in the linked list of all
00422     /// SCEVUnknown instances owned by a ScalarEvolution.
00423     SCEVUnknown *Next;
00424 
00425     SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
00426                 ScalarEvolution *se, SCEVUnknown *next) :
00427       SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
00428 
00429   public:
00430     Value *getValue() const { return getValPtr(); }
00431 
00432     /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
00433     /// constant representing a type size, alignment, or field offset in
00434     /// a target-independent manner, and hasn't happened to have been
00435     /// folded with other operations into something unrecognizable. This
00436     /// is mainly only useful for pretty-printing and other situations
00437     /// where it isn't absolutely required for these to succeed.
00438     bool isSizeOf(Type *&AllocTy) const;
00439     bool isAlignOf(Type *&AllocTy) const;
00440     bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
00441 
00442     Type *getType() const { return getValPtr()->getType(); }
00443 
00444     /// Methods for support type inquiry through isa, cast, and dyn_cast:
00445     static inline bool classof(const SCEV *S) {
00446       return S->getSCEVType() == scUnknown;
00447     }
00448   };
00449 
00450   /// SCEVVisitor - This class defines a simple visitor class that may be used
00451   /// for various SCEV analysis purposes.
00452   template<typename SC, typename RetVal=void>
00453   struct SCEVVisitor {
00454     RetVal visit(const SCEV *S) {
00455       switch (S->getSCEVType()) {
00456       case scConstant:
00457         return ((SC*)this)->visitConstant((const SCEVConstant*)S);
00458       case scTruncate:
00459         return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
00460       case scZeroExtend:
00461         return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
00462       case scSignExtend:
00463         return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
00464       case scAddExpr:
00465         return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
00466       case scMulExpr:
00467         return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
00468       case scUDivExpr:
00469         return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
00470       case scAddRecExpr:
00471         return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
00472       case scSMaxExpr:
00473         return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
00474       case scUMaxExpr:
00475         return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
00476       case scUnknown:
00477         return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
00478       case scCouldNotCompute:
00479         return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
00480       default:
00481         llvm_unreachable("Unknown SCEV type!");
00482       }
00483     }
00484 
00485     RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
00486       llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
00487     }
00488   };
00489 
00490   /// Visit all nodes in the expression tree using worklist traversal.
00491   ///
00492   /// Visitor implements:
00493   ///   // return true to follow this node.
00494   ///   bool follow(const SCEV *S);
00495   ///   // return true to terminate the search.
00496   ///   bool isDone();
00497   template<typename SV>
00498   class SCEVTraversal {
00499     SV &Visitor;
00500     SmallVector<const SCEV *, 8> Worklist;
00501     SmallPtrSet<const SCEV *, 8> Visited;
00502 
00503     void push(const SCEV *S) {
00504       if (Visited.insert(S) && Visitor.follow(S))
00505         Worklist.push_back(S);
00506     }
00507   public:
00508     SCEVTraversal(SV& V): Visitor(V) {}
00509 
00510     void visitAll(const SCEV *Root) {
00511       push(Root);
00512       while (!Worklist.empty() && !Visitor.isDone()) {
00513         const SCEV *S = Worklist.pop_back_val();
00514 
00515         switch (S->getSCEVType()) {
00516         case scConstant:
00517         case scUnknown:
00518           break;
00519         case scTruncate:
00520         case scZeroExtend:
00521         case scSignExtend:
00522           push(cast<SCEVCastExpr>(S)->getOperand());
00523           break;
00524         case scAddExpr:
00525         case scMulExpr:
00526         case scSMaxExpr:
00527         case scUMaxExpr:
00528         case scAddRecExpr: {
00529           const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
00530           for (SCEVNAryExpr::op_iterator I = NAry->op_begin(),
00531                  E = NAry->op_end(); I != E; ++I) {
00532             push(*I);
00533           }
00534           break;
00535         }
00536         case scUDivExpr: {
00537           const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
00538           push(UDiv->getLHS());
00539           push(UDiv->getRHS());
00540           break;
00541         }
00542         case scCouldNotCompute:
00543           llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
00544         default:
00545           llvm_unreachable("Unknown SCEV kind!");
00546         }
00547       }
00548     }
00549   };
00550 
00551   /// Use SCEVTraversal to visit all nodes in the givien expression tree.
00552   template<typename SV>
00553   void visitAll(const SCEV *Root, SV& Visitor) {
00554     SCEVTraversal<SV> T(Visitor);
00555     T.visitAll(Root);
00556   }
00557 
00558   typedef DenseMap<const Value*, Value*> ValueToValueMap;
00559 
00560   /// The SCEVParameterRewriter takes a scalar evolution expression and updates
00561   /// the SCEVUnknown components following the Map (Value -> Value).
00562   struct SCEVParameterRewriter
00563     : public SCEVVisitor<SCEVParameterRewriter, const SCEV*> {
00564   public:
00565     static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
00566                                ValueToValueMap &Map,
00567                                bool InterpretConsts = false) {
00568       SCEVParameterRewriter Rewriter(SE, Map, InterpretConsts);
00569       return Rewriter.visit(Scev);
00570     }
00571 
00572     SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M, bool C)
00573       : SE(S), Map(M), InterpretConsts(C) {}
00574 
00575     const SCEV *visitConstant(const SCEVConstant *Constant) {
00576       return Constant;
00577     }
00578 
00579     const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
00580       const SCEV *Operand = visit(Expr->getOperand());
00581       return SE.getTruncateExpr(Operand, Expr->getType());
00582     }
00583 
00584     const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
00585       const SCEV *Operand = visit(Expr->getOperand());
00586       return SE.getZeroExtendExpr(Operand, Expr->getType());
00587     }
00588 
00589     const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
00590       const SCEV *Operand = visit(Expr->getOperand());
00591       return SE.getSignExtendExpr(Operand, Expr->getType());
00592     }
00593 
00594     const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
00595       SmallVector<const SCEV *, 2> Operands;
00596       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00597         Operands.push_back(visit(Expr->getOperand(i)));
00598       return SE.getAddExpr(Operands);
00599     }
00600 
00601     const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
00602       SmallVector<const SCEV *, 2> Operands;
00603       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00604         Operands.push_back(visit(Expr->getOperand(i)));
00605       return SE.getMulExpr(Operands);
00606     }
00607 
00608     const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
00609       return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
00610     }
00611 
00612     const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
00613       SmallVector<const SCEV *, 2> Operands;
00614       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00615         Operands.push_back(visit(Expr->getOperand(i)));
00616       return SE.getAddRecExpr(Operands, Expr->getLoop(),
00617                               Expr->getNoWrapFlags());
00618     }
00619 
00620     const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
00621       SmallVector<const SCEV *, 2> Operands;
00622       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00623         Operands.push_back(visit(Expr->getOperand(i)));
00624       return SE.getSMaxExpr(Operands);
00625     }
00626 
00627     const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
00628       SmallVector<const SCEV *, 2> Operands;
00629       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00630         Operands.push_back(visit(Expr->getOperand(i)));
00631       return SE.getUMaxExpr(Operands);
00632     }
00633 
00634     const SCEV *visitUnknown(const SCEVUnknown *Expr) {
00635       Value *V = Expr->getValue();
00636       if (Map.count(V)) {
00637         Value *NV = Map[V];
00638         if (InterpretConsts && isa<ConstantInt>(NV))
00639           return SE.getConstant(cast<ConstantInt>(NV));
00640         return SE.getUnknown(NV);
00641       }
00642       return Expr;
00643     }
00644 
00645     const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
00646       return Expr;
00647     }
00648 
00649   private:
00650     ScalarEvolution &SE;
00651     ValueToValueMap &Map;
00652     bool InterpretConsts;
00653   };
00654 
00655   typedef DenseMap<const Loop*, const SCEV*> LoopToScevMapT;
00656 
00657   /// The SCEVApplyRewriter takes a scalar evolution expression and applies
00658   /// the Map (Loop -> SCEV) to all AddRecExprs.
00659   struct SCEVApplyRewriter
00660     : public SCEVVisitor<SCEVApplyRewriter, const SCEV*> {
00661   public:
00662     static const SCEV *rewrite(const SCEV *Scev, LoopToScevMapT &Map,
00663                                ScalarEvolution &SE) {
00664       SCEVApplyRewriter Rewriter(SE, Map);
00665       return Rewriter.visit(Scev);
00666     }
00667 
00668     SCEVApplyRewriter(ScalarEvolution &S, LoopToScevMapT &M)
00669       : SE(S), Map(M) {}
00670 
00671     const SCEV *visitConstant(const SCEVConstant *Constant) {
00672       return Constant;
00673     }
00674 
00675     const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
00676       const SCEV *Operand = visit(Expr->getOperand());
00677       return SE.getTruncateExpr(Operand, Expr->getType());
00678     }
00679 
00680     const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
00681       const SCEV *Operand = visit(Expr->getOperand());
00682       return SE.getZeroExtendExpr(Operand, Expr->getType());
00683     }
00684 
00685     const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
00686       const SCEV *Operand = visit(Expr->getOperand());
00687       return SE.getSignExtendExpr(Operand, Expr->getType());
00688     }
00689 
00690     const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
00691       SmallVector<const SCEV *, 2> Operands;
00692       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00693         Operands.push_back(visit(Expr->getOperand(i)));
00694       return SE.getAddExpr(Operands);
00695     }
00696 
00697     const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
00698       SmallVector<const SCEV *, 2> Operands;
00699       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00700         Operands.push_back(visit(Expr->getOperand(i)));
00701       return SE.getMulExpr(Operands);
00702     }
00703 
00704     const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
00705       return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
00706     }
00707 
00708     const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
00709       SmallVector<const SCEV *, 2> Operands;
00710       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00711         Operands.push_back(visit(Expr->getOperand(i)));
00712 
00713       const Loop *L = Expr->getLoop();
00714       const SCEV *Res = SE.getAddRecExpr(Operands, L, Expr->getNoWrapFlags());
00715 
00716       if (0 == Map.count(L))
00717         return Res;
00718 
00719       const SCEVAddRecExpr *Rec = (const SCEVAddRecExpr *) Res;
00720       return Rec->evaluateAtIteration(Map[L], SE);
00721     }
00722 
00723     const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
00724       SmallVector<const SCEV *, 2> Operands;
00725       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00726         Operands.push_back(visit(Expr->getOperand(i)));
00727       return SE.getSMaxExpr(Operands);
00728     }
00729 
00730     const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
00731       SmallVector<const SCEV *, 2> Operands;
00732       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
00733         Operands.push_back(visit(Expr->getOperand(i)));
00734       return SE.getUMaxExpr(Operands);
00735     }
00736 
00737     const SCEV *visitUnknown(const SCEVUnknown *Expr) {
00738       return Expr;
00739     }
00740 
00741     const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
00742       return Expr;
00743     }
00744 
00745   private:
00746     ScalarEvolution &SE;
00747     LoopToScevMapT &Map;
00748   };
00749 
00750 /// Applies the Map (Loop -> SCEV) to the given Scev.
00751 static inline const SCEV *apply(const SCEV *Scev, LoopToScevMapT &Map,
00752                                 ScalarEvolution &SE) {
00753   return SCEVApplyRewriter::rewrite(Scev, Map, SE);
00754 }
00755 
00756 }
00757 
00758 #endif