Home | History | Annotate | Download | only in Analysis
      1 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the classes used to represent and build scalar expressions.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
     15 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
     16 
     17 #include "llvm/ADT/SmallPtrSet.h"
     18 #include "llvm/Analysis/ScalarEvolution.h"
     19 #include "llvm/Support/ErrorHandling.h"
     20 
     21 namespace llvm {
     22   class ConstantInt;
     23   class ConstantRange;
     24   class DominatorTree;
     25 
     26   enum SCEVTypes {
     27     // These should be ordered in terms of increasing complexity to make the
     28     // folders simpler.
     29     scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
     30     scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr,
     31     scUnknown, scCouldNotCompute
     32   };
     33 
     34   //===--------------------------------------------------------------------===//
     35   /// SCEVConstant - This class represents a constant integer value.
     36   ///
     37   class SCEVConstant : public SCEV {
     38     friend class ScalarEvolution;
     39 
     40     ConstantInt *V;
     41     SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
     42       SCEV(ID, scConstant), V(v) {}
     43   public:
     44     ConstantInt *getValue() const { return V; }
     45 
     46     Type *getType() const { return V->getType(); }
     47 
     48     /// Methods for support type inquiry through isa, cast, and dyn_cast:
     49     static inline bool classof(const SCEV *S) {
     50       return S->getSCEVType() == scConstant;
     51     }
     52   };
     53 
     54   //===--------------------------------------------------------------------===//
     55   /// SCEVCastExpr - This is the base class for unary cast operator classes.
     56   ///
     57   class SCEVCastExpr : public SCEV {
     58   protected:
     59     const SCEV *Op;
     60     Type *Ty;
     61 
     62     SCEVCastExpr(const FoldingSetNodeIDRef ID,
     63                  unsigned SCEVTy, const SCEV *op, Type *ty);
     64 
     65   public:
     66     const SCEV *getOperand() const { return Op; }
     67     Type *getType() const { return Ty; }
     68 
     69     /// Methods for support type inquiry through isa, cast, and dyn_cast:
     70     static inline bool classof(const SCEV *S) {
     71       return S->getSCEVType() == scTruncate ||
     72              S->getSCEVType() == scZeroExtend ||
     73              S->getSCEVType() == scSignExtend;
     74     }
     75   };
     76 
     77   //===--------------------------------------------------------------------===//
     78   /// SCEVTruncateExpr - This class represents a truncation of an integer value
     79   /// to a smaller integer value.
     80   ///
     81   class SCEVTruncateExpr : public SCEVCastExpr {
     82     friend class ScalarEvolution;
     83 
     84     SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
     85                      const SCEV *op, Type *ty);
     86 
     87   public:
     88     /// Methods for support type inquiry through isa, cast, and dyn_cast:
     89     static inline bool classof(const SCEV *S) {
     90       return S->getSCEVType() == scTruncate;
     91     }
     92   };
     93 
     94   //===--------------------------------------------------------------------===//
     95   /// SCEVZeroExtendExpr - This class represents a zero extension of a small
     96   /// integer value to a larger integer value.
     97   ///
     98   class SCEVZeroExtendExpr : public SCEVCastExpr {
     99     friend class ScalarEvolution;
    100 
    101     SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
    102                        const SCEV *op, Type *ty);
    103 
    104   public:
    105     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    106     static inline bool classof(const SCEV *S) {
    107       return S->getSCEVType() == scZeroExtend;
    108     }
    109   };
    110 
    111   //===--------------------------------------------------------------------===//
    112   /// SCEVSignExtendExpr - This class represents a sign extension of a small
    113   /// integer value to a larger integer value.
    114   ///
    115   class SCEVSignExtendExpr : public SCEVCastExpr {
    116     friend class ScalarEvolution;
    117 
    118     SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
    119                        const SCEV *op, Type *ty);
    120 
    121   public:
    122     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    123     static inline bool classof(const SCEV *S) {
    124       return S->getSCEVType() == scSignExtend;
    125     }
    126   };
    127 
    128 
    129   //===--------------------------------------------------------------------===//
    130   /// SCEVNAryExpr - This node is a base class providing common
    131   /// functionality for n'ary operators.
    132   ///
    133   class SCEVNAryExpr : public SCEV {
    134   protected:
    135     // Since SCEVs are immutable, ScalarEvolution allocates operand
    136     // arrays with its SCEVAllocator, so this class just needs a simple
    137     // pointer rather than a more elaborate vector-like data structure.
    138     // This also avoids the need for a non-trivial destructor.
    139     const SCEV *const *Operands;
    140     size_t NumOperands;
    141 
    142     SCEVNAryExpr(const FoldingSetNodeIDRef ID,
    143                  enum SCEVTypes T, const SCEV *const *O, size_t N)
    144       : SCEV(ID, T), Operands(O), NumOperands(N) {}
    145 
    146   public:
    147     size_t getNumOperands() const { return NumOperands; }
    148     const SCEV *getOperand(unsigned i) const {
    149       assert(i < NumOperands && "Operand index out of range!");
    150       return Operands[i];
    151     }
    152 
    153     typedef const SCEV *const *op_iterator;
    154     op_iterator op_begin() const { return Operands; }
    155     op_iterator op_end() const { return Operands + NumOperands; }
    156 
    157     Type *getType() const { return getOperand(0)->getType(); }
    158 
    159     NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const {
    160       return (NoWrapFlags)(SubclassData & Mask);
    161     }
    162 
    163     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    164     static inline bool classof(const SCEV *S) {
    165       return S->getSCEVType() == scAddExpr ||
    166              S->getSCEVType() == scMulExpr ||
    167              S->getSCEVType() == scSMaxExpr ||
    168              S->getSCEVType() == scUMaxExpr ||
    169              S->getSCEVType() == scAddRecExpr;
    170     }
    171   };
    172 
    173   //===--------------------------------------------------------------------===//
    174   /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
    175   /// operators.
    176   ///
    177   class SCEVCommutativeExpr : public SCEVNAryExpr {
    178   protected:
    179     SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
    180                         enum SCEVTypes T, const SCEV *const *O, size_t N)
    181       : SCEVNAryExpr(ID, T, O, N) {}
    182 
    183   public:
    184     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    185     static inline bool classof(const SCEV *S) {
    186       return S->getSCEVType() == scAddExpr ||
    187              S->getSCEVType() == scMulExpr ||
    188              S->getSCEVType() == scSMaxExpr ||
    189              S->getSCEVType() == scUMaxExpr;
    190     }
    191 
    192     /// Set flags for a non-recurrence without clearing previously set flags.
    193     void setNoWrapFlags(NoWrapFlags Flags) {
    194       SubclassData |= Flags;
    195     }
    196   };
    197 
    198 
    199   //===--------------------------------------------------------------------===//
    200   /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
    201   ///
    202   class SCEVAddExpr : public SCEVCommutativeExpr {
    203     friend class ScalarEvolution;
    204 
    205     SCEVAddExpr(const FoldingSetNodeIDRef ID,
    206                 const SCEV *const *O, size_t N)
    207       : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
    208     }
    209 
    210   public:
    211     Type *getType() const {
    212       // Use the type of the last operand, which is likely to be a pointer
    213       // type, if there is one. This doesn't usually matter, but it can help
    214       // reduce casts when the expressions are expanded.
    215       return getOperand(getNumOperands() - 1)->getType();
    216     }
    217 
    218     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    219     static inline bool classof(const SCEV *S) {
    220       return S->getSCEVType() == scAddExpr;
    221     }
    222   };
    223 
    224   //===--------------------------------------------------------------------===//
    225   /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
    226   ///
    227   class SCEVMulExpr : public SCEVCommutativeExpr {
    228     friend class ScalarEvolution;
    229 
    230     SCEVMulExpr(const FoldingSetNodeIDRef ID,
    231                 const SCEV *const *O, size_t N)
    232       : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
    233     }
    234 
    235   public:
    236     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    237     static inline bool classof(const SCEV *S) {
    238       return S->getSCEVType() == scMulExpr;
    239     }
    240   };
    241 
    242 
    243   //===--------------------------------------------------------------------===//
    244   /// SCEVUDivExpr - This class represents a binary unsigned division operation.
    245   ///
    246   class SCEVUDivExpr : public SCEV {
    247     friend class ScalarEvolution;
    248 
    249     const SCEV *LHS;
    250     const SCEV *RHS;
    251     SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
    252       : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
    253 
    254   public:
    255     const SCEV *getLHS() const { return LHS; }
    256     const SCEV *getRHS() const { return RHS; }
    257 
    258     Type *getType() const {
    259       // In most cases the types of LHS and RHS will be the same, but in some
    260       // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
    261       // depend on the type for correctness, but handling types carefully can
    262       // avoid extra casts in the SCEVExpander. The LHS is more likely to be
    263       // a pointer type than the RHS, so use the RHS' type here.
    264       return getRHS()->getType();
    265     }
    266 
    267     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    268     static inline bool classof(const SCEV *S) {
    269       return S->getSCEVType() == scUDivExpr;
    270     }
    271   };
    272 
    273 
    274   //===--------------------------------------------------------------------===//
    275   /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
    276   /// count of the specified loop.  This is the primary focus of the
    277   /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
    278   /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
    279   /// created and analyzed.
    280   ///
    281   /// All operands of an AddRec are required to be loop invariant.
    282   ///
    283   class SCEVAddRecExpr : public SCEVNAryExpr {
    284     friend class ScalarEvolution;
    285 
    286     const Loop *L;
    287 
    288     SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
    289                    const SCEV *const *O, size_t N, const Loop *l)
    290       : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {}
    291 
    292   public:
    293     const SCEV *getStart() const { return Operands[0]; }
    294     const Loop *getLoop() const { return L; }
    295 
    296     /// getStepRecurrence - This method constructs and returns the recurrence
    297     /// indicating how much this expression steps by.  If this is a polynomial
    298     /// of degree N, it returns a chrec of degree N-1.
    299     /// We cannot determine whether the step recurrence has self-wraparound.
    300     const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
    301       if (isAffine()) return getOperand(1);
    302       return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
    303                                                            op_end()),
    304                               getLoop(), FlagAnyWrap);
    305     }
    306 
    307     /// isAffine - Return true if this is an affine AddRec (i.e., it represents
    308     /// an expressions A+B*x where A and B are loop invariant values.
    309     bool isAffine() const {
    310       // We know that the start value is invariant.  This expression is thus
    311       // affine iff the step is also invariant.
    312       return getNumOperands() == 2;
    313     }
    314 
    315     /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
    316     /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
    317     /// invariant values.  This corresponds to an addrec of the form {L,+,M,+,N}
    318     bool isQuadratic() const {
    319       return getNumOperands() == 3;
    320     }
    321 
    322     /// Set flags for a recurrence without clearing any previously set flags.
    323     /// For AddRec, either NUW or NSW implies NW. Keep track of this fact here
    324     /// to make it easier to propagate flags.
    325     void setNoWrapFlags(NoWrapFlags Flags) {
    326       if (Flags & (FlagNUW | FlagNSW))
    327         Flags = ScalarEvolution::setFlags(Flags, FlagNW);
    328       SubclassData |= Flags;
    329     }
    330 
    331     /// evaluateAtIteration - Return the value of this chain of recurrences at
    332     /// the specified iteration number.
    333     const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
    334 
    335     /// getNumIterationsInRange - Return the number of iterations of this loop
    336     /// that produce values in the specified constant range.  Another way of
    337     /// looking at this is that it returns the first iteration number where the
    338     /// value is not in the condition, thus computing the exit count.  If the
    339     /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
    340     /// returned.
    341     const SCEV *getNumIterationsInRange(ConstantRange Range,
    342                                        ScalarEvolution &SE) const;
    343 
    344     /// getPostIncExpr - Return an expression representing the value of
    345     /// this expression one iteration of the loop ahead.
    346     const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
    347       return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
    348     }
    349 
    350     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    351     static inline bool classof(const SCEV *S) {
    352       return S->getSCEVType() == scAddRecExpr;
    353     }
    354   };
    355 
    356 
    357   //===--------------------------------------------------------------------===//
    358   /// SCEVSMaxExpr - This class represents a signed maximum selection.
    359   ///
    360   class SCEVSMaxExpr : public SCEVCommutativeExpr {
    361     friend class ScalarEvolution;
    362 
    363     SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
    364                  const SCEV *const *O, size_t N)
    365       : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
    366       // Max never overflows.
    367       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
    368     }
    369 
    370   public:
    371     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    372     static inline bool classof(const SCEV *S) {
    373       return S->getSCEVType() == scSMaxExpr;
    374     }
    375   };
    376 
    377 
    378   //===--------------------------------------------------------------------===//
    379   /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
    380   ///
    381   class SCEVUMaxExpr : public SCEVCommutativeExpr {
    382     friend class ScalarEvolution;
    383 
    384     SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
    385                  const SCEV *const *O, size_t N)
    386       : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
    387       // Max never overflows.
    388       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
    389     }
    390 
    391   public:
    392     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    393     static inline bool classof(const SCEV *S) {
    394       return S->getSCEVType() == scUMaxExpr;
    395     }
    396   };
    397 
    398   //===--------------------------------------------------------------------===//
    399   /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
    400   /// value, and only represent it as its LLVM Value.  This is the "bottom"
    401   /// value for the analysis.
    402   ///
    403   class SCEVUnknown : public SCEV, private CallbackVH {
    404     friend class ScalarEvolution;
    405 
    406     // Implement CallbackVH.
    407     virtual void deleted();
    408     virtual void allUsesReplacedWith(Value *New);
    409 
    410     /// SE - The parent ScalarEvolution value. This is used to update
    411     /// the parent's maps when the value associated with a SCEVUnknown
    412     /// is deleted or RAUW'd.
    413     ScalarEvolution *SE;
    414 
    415     /// Next - The next pointer in the linked list of all
    416     /// SCEVUnknown instances owned by a ScalarEvolution.
    417     SCEVUnknown *Next;
    418 
    419     SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
    420                 ScalarEvolution *se, SCEVUnknown *next) :
    421       SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
    422 
    423   public:
    424     Value *getValue() const { return getValPtr(); }
    425 
    426     /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
    427     /// constant representing a type size, alignment, or field offset in
    428     /// a target-independent manner, and hasn't happened to have been
    429     /// folded with other operations into something unrecognizable. This
    430     /// is mainly only useful for pretty-printing and other situations
    431     /// where it isn't absolutely required for these to succeed.
    432     bool isSizeOf(Type *&AllocTy) const;
    433     bool isAlignOf(Type *&AllocTy) const;
    434     bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
    435 
    436     Type *getType() const { return getValPtr()->getType(); }
    437 
    438     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    439     static inline bool classof(const SCEV *S) {
    440       return S->getSCEVType() == scUnknown;
    441     }
    442   };
    443 
    444   /// SCEVVisitor - This class defines a simple visitor class that may be used
    445   /// for various SCEV analysis purposes.
    446   template<typename SC, typename RetVal=void>
    447   struct SCEVVisitor {
    448     RetVal visit(const SCEV *S) {
    449       switch (S->getSCEVType()) {
    450       case scConstant:
    451         return ((SC*)this)->visitConstant((const SCEVConstant*)S);
    452       case scTruncate:
    453         return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
    454       case scZeroExtend:
    455         return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
    456       case scSignExtend:
    457         return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
    458       case scAddExpr:
    459         return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
    460       case scMulExpr:
    461         return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
    462       case scUDivExpr:
    463         return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
    464       case scAddRecExpr:
    465         return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
    466       case scSMaxExpr:
    467         return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
    468       case scUMaxExpr:
    469         return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
    470       case scUnknown:
    471         return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
    472       case scCouldNotCompute:
    473         return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
    474       default:
    475         llvm_unreachable("Unknown SCEV type!");
    476       }
    477     }
    478 
    479     RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
    480       llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
    481     }
    482   };
    483 
    484   /// Visit all nodes in the expression tree using worklist traversal.
    485   ///
    486   /// Visitor implements:
    487   ///   // return true to follow this node.
    488   ///   bool follow(const SCEV *S);
    489   ///   // return true to terminate the search.
    490   ///   bool isDone();
    491   template<typename SV>
    492   class SCEVTraversal {
    493     SV &Visitor;
    494     SmallVector<const SCEV *, 8> Worklist;
    495     SmallPtrSet<const SCEV *, 8> Visited;
    496 
    497     void push(const SCEV *S) {
    498       if (Visited.insert(S) && Visitor.follow(S))
    499         Worklist.push_back(S);
    500     }
    501   public:
    502     SCEVTraversal(SV& V): Visitor(V) {}
    503 
    504     void visitAll(const SCEV *Root) {
    505       push(Root);
    506       while (!Worklist.empty() && !Visitor.isDone()) {
    507         const SCEV *S = Worklist.pop_back_val();
    508 
    509         switch (S->getSCEVType()) {
    510         case scConstant:
    511         case scUnknown:
    512           break;
    513         case scTruncate:
    514         case scZeroExtend:
    515         case scSignExtend:
    516           push(cast<SCEVCastExpr>(S)->getOperand());
    517           break;
    518         case scAddExpr:
    519         case scMulExpr:
    520         case scSMaxExpr:
    521         case scUMaxExpr:
    522         case scAddRecExpr: {
    523           const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
    524           for (SCEVNAryExpr::op_iterator I = NAry->op_begin(),
    525                  E = NAry->op_end(); I != E; ++I) {
    526             push(*I);
    527           }
    528           break;
    529         }
    530         case scUDivExpr: {
    531           const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
    532           push(UDiv->getLHS());
    533           push(UDiv->getRHS());
    534           break;
    535         }
    536         case scCouldNotCompute:
    537           llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
    538         default:
    539           llvm_unreachable("Unknown SCEV kind!");
    540         }
    541       }
    542     }
    543   };
    544 
    545   /// Use SCEVTraversal to visit all nodes in the givien expression tree.
    546   template<typename SV>
    547   void visitAll(const SCEV *Root, SV& Visitor) {
    548     SCEVTraversal<SV> T(Visitor);
    549     T.visitAll(Root);
    550   }
    551 
    552   /// The SCEVRewriter takes a scalar evolution expression and copies all its
    553   /// components. The result after a rewrite is an identical SCEV.
    554   struct SCEVRewriter
    555     : public SCEVVisitor<SCEVRewriter, const SCEV*> {
    556   public:
    557     SCEVRewriter(ScalarEvolution &S) : SE(S) {}
    558 
    559     virtual ~SCEVRewriter() {}
    560 
    561     virtual const SCEV *visitConstant(const SCEVConstant *Constant) {
    562       return Constant;
    563     }
    564 
    565     virtual const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
    566       const SCEV *Operand = visit(Expr->getOperand());
    567       return SE.getTruncateExpr(Operand, Expr->getType());
    568     }
    569 
    570     virtual const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
    571       const SCEV *Operand = visit(Expr->getOperand());
    572       return SE.getZeroExtendExpr(Operand, Expr->getType());
    573     }
    574 
    575     virtual const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
    576       const SCEV *Operand = visit(Expr->getOperand());
    577       return SE.getSignExtendExpr(Operand, Expr->getType());
    578     }
    579 
    580     virtual const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
    581       SmallVector<const SCEV *, 2> Operands;
    582       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
    583         Operands.push_back(visit(Expr->getOperand(i)));
    584       return SE.getAddExpr(Operands);
    585     }
    586 
    587     virtual const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
    588       SmallVector<const SCEV *, 2> Operands;
    589       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
    590         Operands.push_back(visit(Expr->getOperand(i)));
    591       return SE.getMulExpr(Operands);
    592     }
    593 
    594     virtual const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
    595       return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
    596     }
    597 
    598     virtual const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
    599       SmallVector<const SCEV *, 2> Operands;
    600       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
    601         Operands.push_back(visit(Expr->getOperand(i)));
    602       return SE.getAddRecExpr(Operands, Expr->getLoop(),
    603                               Expr->getNoWrapFlags());
    604     }
    605 
    606     virtual const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
    607       SmallVector<const SCEV *, 2> Operands;
    608       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
    609         Operands.push_back(visit(Expr->getOperand(i)));
    610       return SE.getSMaxExpr(Operands);
    611     }
    612 
    613     virtual const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
    614       SmallVector<const SCEV *, 2> Operands;
    615       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
    616         Operands.push_back(visit(Expr->getOperand(i)));
    617       return SE.getUMaxExpr(Operands);
    618     }
    619 
    620     virtual const SCEV *visitUnknown(const SCEVUnknown *Expr) {
    621       return Expr;
    622     }
    623 
    624     virtual const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
    625       return Expr;
    626     }
    627 
    628   protected:
    629     ScalarEvolution &SE;
    630   };
    631 
    632   typedef DenseMap<const Value*, Value*> ValueToValueMap;
    633 
    634   /// The SCEVParameterRewriter takes a scalar evolution expression and updates
    635   /// the SCEVUnknown components following the Map (Value -> Value).
    636   struct SCEVParameterRewriter: public SCEVRewriter {
    637   public:
    638     static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
    639                                ValueToValueMap &Map) {
    640       SCEVParameterRewriter Rewriter(SE, Map);
    641       return Rewriter.visit(Scev);
    642     }
    643     SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M)
    644       : SCEVRewriter(S), Map(M) {}
    645 
    646     virtual const SCEV *visitUnknown(const SCEVUnknown *Expr) {
    647       Value *V = Expr->getValue();
    648       if (Map.count(V))
    649         return SE.getUnknown(Map[V]);
    650       return Expr;
    651     }
    652 
    653   private:
    654     ValueToValueMap &Map;
    655   };
    656 
    657   typedef DenseMap<const Loop*, const SCEV*> LoopToScevMapT;
    658 
    659   /// The SCEVApplyRewriter takes a scalar evolution expression and applies
    660   /// the Map (Loop -> SCEV) to all AddRecExprs.
    661   struct SCEVApplyRewriter: public SCEVRewriter {
    662   public:
    663     static const SCEV *rewrite(const SCEV *Scev, LoopToScevMapT &Map,
    664                                ScalarEvolution &SE) {
    665       SCEVApplyRewriter Rewriter(SE, Map);
    666       return Rewriter.visit(Scev);
    667     }
    668     SCEVApplyRewriter(ScalarEvolution &S, LoopToScevMapT &M)
    669       : SCEVRewriter(S), Map(M) {}
    670 
    671     virtual const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
    672       SmallVector<const SCEV *, 2> Operands;
    673       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
    674         Operands.push_back(visit(Expr->getOperand(i)));
    675 
    676       const Loop *L = Expr->getLoop();
    677       const SCEV *Res = SE.getAddRecExpr(Operands, L, Expr->getNoWrapFlags());
    678 
    679       if (0 == Map.count(L))
    680         return Res;
    681 
    682       const SCEVAddRecExpr *Rec = (const SCEVAddRecExpr *) Res;
    683       return Rec->evaluateAtIteration(Map[L], SE);
    684     }
    685 
    686   private:
    687     LoopToScevMapT &Map;
    688   };
    689 
    690 /// Applies the Map (Loop -> SCEV) to the given Scev.
    691 static inline const SCEV *apply(const SCEV *Scev, LoopToScevMapT &Map,
    692                                 ScalarEvolution &SE) {
    693   return SCEVApplyRewriter::rewrite(Scev, Map, SE);
    694 }
    695 
    696 }
    697 
    698 #endif
    699