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_SCALAREVOLUTION_EXPRESSIONS_H
     15 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPRESSIONS_H
     16 
     17 #include "llvm/Analysis/ScalarEvolution.h"
     18 #include "llvm/Support/ErrorHandling.h"
     19 
     20 namespace llvm {
     21   class ConstantInt;
     22   class ConstantRange;
     23   class DominatorTree;
     24 
     25   enum SCEVTypes {
     26     // These should be ordered in terms of increasing complexity to make the
     27     // folders simpler.
     28     scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
     29     scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr,
     30     scUnknown, scCouldNotCompute
     31   };
     32 
     33   //===--------------------------------------------------------------------===//
     34   /// SCEVConstant - This class represents a constant integer value.
     35   ///
     36   class SCEVConstant : public SCEV {
     37     friend class ScalarEvolution;
     38 
     39     ConstantInt *V;
     40     SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
     41       SCEV(ID, scConstant), V(v) {}
     42   public:
     43     ConstantInt *getValue() const { return V; }
     44 
     45     Type *getType() const { return V->getType(); }
     46 
     47     /// Methods for support type inquiry through isa, cast, and dyn_cast:
     48     static inline bool classof(const SCEVConstant *S) { return true; }
     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 SCEVCastExpr *S) { return true; }
     71     static inline bool classof(const SCEV *S) {
     72       return S->getSCEVType() == scTruncate ||
     73              S->getSCEVType() == scZeroExtend ||
     74              S->getSCEVType() == scSignExtend;
     75     }
     76   };
     77 
     78   //===--------------------------------------------------------------------===//
     79   /// SCEVTruncateExpr - This class represents a truncation of an integer value
     80   /// to a smaller integer value.
     81   ///
     82   class SCEVTruncateExpr : public SCEVCastExpr {
     83     friend class ScalarEvolution;
     84 
     85     SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
     86                      const SCEV *op, Type *ty);
     87 
     88   public:
     89     /// Methods for support type inquiry through isa, cast, and dyn_cast:
     90     static inline bool classof(const SCEVTruncateExpr *S) { return true; }
     91     static inline bool classof(const SCEV *S) {
     92       return S->getSCEVType() == scTruncate;
     93     }
     94   };
     95 
     96   //===--------------------------------------------------------------------===//
     97   /// SCEVZeroExtendExpr - This class represents a zero extension of a small
     98   /// integer value to a larger integer value.
     99   ///
    100   class SCEVZeroExtendExpr : public SCEVCastExpr {
    101     friend class ScalarEvolution;
    102 
    103     SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
    104                        const SCEV *op, Type *ty);
    105 
    106   public:
    107     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    108     static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
    109     static inline bool classof(const SCEV *S) {
    110       return S->getSCEVType() == scZeroExtend;
    111     }
    112   };
    113 
    114   //===--------------------------------------------------------------------===//
    115   /// SCEVSignExtendExpr - This class represents a sign extension of a small
    116   /// integer value to a larger integer value.
    117   ///
    118   class SCEVSignExtendExpr : public SCEVCastExpr {
    119     friend class ScalarEvolution;
    120 
    121     SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
    122                        const SCEV *op, Type *ty);
    123 
    124   public:
    125     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    126     static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
    127     static inline bool classof(const SCEV *S) {
    128       return S->getSCEVType() == scSignExtend;
    129     }
    130   };
    131 
    132 
    133   //===--------------------------------------------------------------------===//
    134   /// SCEVNAryExpr - This node is a base class providing common
    135   /// functionality for n'ary operators.
    136   ///
    137   class SCEVNAryExpr : public SCEV {
    138   protected:
    139     // Since SCEVs are immutable, ScalarEvolution allocates operand
    140     // arrays with its SCEVAllocator, so this class just needs a simple
    141     // pointer rather than a more elaborate vector-like data structure.
    142     // This also avoids the need for a non-trivial destructor.
    143     const SCEV *const *Operands;
    144     size_t NumOperands;
    145 
    146     SCEVNAryExpr(const FoldingSetNodeIDRef ID,
    147                  enum SCEVTypes T, const SCEV *const *O, size_t N)
    148       : SCEV(ID, T), Operands(O), NumOperands(N) {}
    149 
    150   public:
    151     size_t getNumOperands() const { return NumOperands; }
    152     const SCEV *getOperand(unsigned i) const {
    153       assert(i < NumOperands && "Operand index out of range!");
    154       return Operands[i];
    155     }
    156 
    157     typedef const SCEV *const *op_iterator;
    158     op_iterator op_begin() const { return Operands; }
    159     op_iterator op_end() const { return Operands + NumOperands; }
    160 
    161     Type *getType() const { return getOperand(0)->getType(); }
    162 
    163     NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const {
    164       return (NoWrapFlags)(SubclassData & Mask);
    165     }
    166 
    167     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    168     static inline bool classof(const SCEVNAryExpr *S) { return true; }
    169     static inline bool classof(const SCEV *S) {
    170       return S->getSCEVType() == scAddExpr ||
    171              S->getSCEVType() == scMulExpr ||
    172              S->getSCEVType() == scSMaxExpr ||
    173              S->getSCEVType() == scUMaxExpr ||
    174              S->getSCEVType() == scAddRecExpr;
    175     }
    176   };
    177 
    178   //===--------------------------------------------------------------------===//
    179   /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
    180   /// operators.
    181   ///
    182   class SCEVCommutativeExpr : public SCEVNAryExpr {
    183   protected:
    184     SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
    185                         enum SCEVTypes T, const SCEV *const *O, size_t N)
    186       : SCEVNAryExpr(ID, T, O, N) {}
    187 
    188   public:
    189     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    190     static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
    191     static inline bool classof(const SCEV *S) {
    192       return S->getSCEVType() == scAddExpr ||
    193              S->getSCEVType() == scMulExpr ||
    194              S->getSCEVType() == scSMaxExpr ||
    195              S->getSCEVType() == scUMaxExpr;
    196     }
    197 
    198     /// Set flags for a non-recurrence without clearing previously set flags.
    199     void setNoWrapFlags(NoWrapFlags Flags) {
    200       SubclassData |= Flags;
    201     }
    202   };
    203 
    204 
    205   //===--------------------------------------------------------------------===//
    206   /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
    207   ///
    208   class SCEVAddExpr : public SCEVCommutativeExpr {
    209     friend class ScalarEvolution;
    210 
    211     SCEVAddExpr(const FoldingSetNodeIDRef ID,
    212                 const SCEV *const *O, size_t N)
    213       : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
    214     }
    215 
    216   public:
    217     Type *getType() const {
    218       // Use the type of the last operand, which is likely to be a pointer
    219       // type, if there is one. This doesn't usually matter, but it can help
    220       // reduce casts when the expressions are expanded.
    221       return getOperand(getNumOperands() - 1)->getType();
    222     }
    223 
    224     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    225     static inline bool classof(const SCEVAddExpr *S) { return true; }
    226     static inline bool classof(const SCEV *S) {
    227       return S->getSCEVType() == scAddExpr;
    228     }
    229   };
    230 
    231   //===--------------------------------------------------------------------===//
    232   /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
    233   ///
    234   class SCEVMulExpr : public SCEVCommutativeExpr {
    235     friend class ScalarEvolution;
    236 
    237     SCEVMulExpr(const FoldingSetNodeIDRef ID,
    238                 const SCEV *const *O, size_t N)
    239       : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
    240     }
    241 
    242   public:
    243     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    244     static inline bool classof(const SCEVMulExpr *S) { return true; }
    245     static inline bool classof(const SCEV *S) {
    246       return S->getSCEVType() == scMulExpr;
    247     }
    248   };
    249 
    250 
    251   //===--------------------------------------------------------------------===//
    252   /// SCEVUDivExpr - This class represents a binary unsigned division operation.
    253   ///
    254   class SCEVUDivExpr : public SCEV {
    255     friend class ScalarEvolution;
    256 
    257     const SCEV *LHS;
    258     const SCEV *RHS;
    259     SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
    260       : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
    261 
    262   public:
    263     const SCEV *getLHS() const { return LHS; }
    264     const SCEV *getRHS() const { return RHS; }
    265 
    266     Type *getType() const {
    267       // In most cases the types of LHS and RHS will be the same, but in some
    268       // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
    269       // depend on the type for correctness, but handling types carefully can
    270       // avoid extra casts in the SCEVExpander. The LHS is more likely to be
    271       // a pointer type than the RHS, so use the RHS' type here.
    272       return getRHS()->getType();
    273     }
    274 
    275     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    276     static inline bool classof(const SCEVUDivExpr *S) { return true; }
    277     static inline bool classof(const SCEV *S) {
    278       return S->getSCEVType() == scUDivExpr;
    279     }
    280   };
    281 
    282 
    283   //===--------------------------------------------------------------------===//
    284   /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
    285   /// count of the specified loop.  This is the primary focus of the
    286   /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
    287   /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
    288   /// created and analyzed.
    289   ///
    290   /// All operands of an AddRec are required to be loop invariant.
    291   ///
    292   class SCEVAddRecExpr : public SCEVNAryExpr {
    293     friend class ScalarEvolution;
    294 
    295     const Loop *L;
    296 
    297     SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
    298                    const SCEV *const *O, size_t N, const Loop *l)
    299       : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {}
    300 
    301   public:
    302     const SCEV *getStart() const { return Operands[0]; }
    303     const Loop *getLoop() const { return L; }
    304 
    305     /// getStepRecurrence - This method constructs and returns the recurrence
    306     /// indicating how much this expression steps by.  If this is a polynomial
    307     /// of degree N, it returns a chrec of degree N-1.
    308     /// We cannot determine whether the step recurrence has self-wraparound.
    309     const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
    310       if (isAffine()) return getOperand(1);
    311       return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
    312                                                            op_end()),
    313                               getLoop(), FlagAnyWrap);
    314     }
    315 
    316     /// isAffine - Return true if this is an affine AddRec (i.e., it represents
    317     /// an expressions A+B*x where A and B are loop invariant values.
    318     bool isAffine() const {
    319       // We know that the start value is invariant.  This expression is thus
    320       // affine iff the step is also invariant.
    321       return getNumOperands() == 2;
    322     }
    323 
    324     /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
    325     /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
    326     /// invariant values.  This corresponds to an addrec of the form {L,+,M,+,N}
    327     bool isQuadratic() const {
    328       return getNumOperands() == 3;
    329     }
    330 
    331     /// Set flags for a recurrence without clearing any previously set flags.
    332     /// For AddRec, either NUW or NSW implies NW. Keep track of this fact here
    333     /// to make it easier to propagate flags.
    334     void setNoWrapFlags(NoWrapFlags Flags) {
    335       if (Flags & (FlagNUW | FlagNSW))
    336         Flags = ScalarEvolution::setFlags(Flags, FlagNW);
    337       SubclassData |= Flags;
    338     }
    339 
    340     /// evaluateAtIteration - Return the value of this chain of recurrences at
    341     /// the specified iteration number.
    342     const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
    343 
    344     /// getNumIterationsInRange - Return the number of iterations of this loop
    345     /// that produce values in the specified constant range.  Another way of
    346     /// looking at this is that it returns the first iteration number where the
    347     /// value is not in the condition, thus computing the exit count.  If the
    348     /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
    349     /// returned.
    350     const SCEV *getNumIterationsInRange(ConstantRange Range,
    351                                        ScalarEvolution &SE) const;
    352 
    353     /// getPostIncExpr - Return an expression representing the value of
    354     /// this expression one iteration of the loop ahead.
    355     const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
    356       return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
    357     }
    358 
    359     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    360     static inline bool classof(const SCEVAddRecExpr *S) { return true; }
    361     static inline bool classof(const SCEV *S) {
    362       return S->getSCEVType() == scAddRecExpr;
    363     }
    364   };
    365 
    366 
    367   //===--------------------------------------------------------------------===//
    368   /// SCEVSMaxExpr - This class represents a signed maximum selection.
    369   ///
    370   class SCEVSMaxExpr : public SCEVCommutativeExpr {
    371     friend class ScalarEvolution;
    372 
    373     SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
    374                  const SCEV *const *O, size_t N)
    375       : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
    376       // Max never overflows.
    377       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
    378     }
    379 
    380   public:
    381     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    382     static inline bool classof(const SCEVSMaxExpr *S) { return true; }
    383     static inline bool classof(const SCEV *S) {
    384       return S->getSCEVType() == scSMaxExpr;
    385     }
    386   };
    387 
    388 
    389   //===--------------------------------------------------------------------===//
    390   /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
    391   ///
    392   class SCEVUMaxExpr : public SCEVCommutativeExpr {
    393     friend class ScalarEvolution;
    394 
    395     SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
    396                  const SCEV *const *O, size_t N)
    397       : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
    398       // Max never overflows.
    399       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
    400     }
    401 
    402   public:
    403     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    404     static inline bool classof(const SCEVUMaxExpr *S) { return true; }
    405     static inline bool classof(const SCEV *S) {
    406       return S->getSCEVType() == scUMaxExpr;
    407     }
    408   };
    409 
    410   //===--------------------------------------------------------------------===//
    411   /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
    412   /// value, and only represent it as its LLVM Value.  This is the "bottom"
    413   /// value for the analysis.
    414   ///
    415   class SCEVUnknown : public SCEV, private CallbackVH {
    416     friend class ScalarEvolution;
    417 
    418     // Implement CallbackVH.
    419     virtual void deleted();
    420     virtual void allUsesReplacedWith(Value *New);
    421 
    422     /// SE - The parent ScalarEvolution value. This is used to update
    423     /// the parent's maps when the value associated with a SCEVUnknown
    424     /// is deleted or RAUW'd.
    425     ScalarEvolution *SE;
    426 
    427     /// Next - The next pointer in the linked list of all
    428     /// SCEVUnknown instances owned by a ScalarEvolution.
    429     SCEVUnknown *Next;
    430 
    431     SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
    432                 ScalarEvolution *se, SCEVUnknown *next) :
    433       SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
    434 
    435   public:
    436     Value *getValue() const { return getValPtr(); }
    437 
    438     /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
    439     /// constant representing a type size, alignment, or field offset in
    440     /// a target-independent manner, and hasn't happened to have been
    441     /// folded with other operations into something unrecognizable. This
    442     /// is mainly only useful for pretty-printing and other situations
    443     /// where it isn't absolutely required for these to succeed.
    444     bool isSizeOf(Type *&AllocTy) const;
    445     bool isAlignOf(Type *&AllocTy) const;
    446     bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
    447 
    448     Type *getType() const { return getValPtr()->getType(); }
    449 
    450     /// Methods for support type inquiry through isa, cast, and dyn_cast:
    451     static inline bool classof(const SCEVUnknown *S) { return true; }
    452     static inline bool classof(const SCEV *S) {
    453       return S->getSCEVType() == scUnknown;
    454     }
    455   };
    456 
    457   /// SCEVVisitor - This class defines a simple visitor class that may be used
    458   /// for various SCEV analysis purposes.
    459   template<typename SC, typename RetVal=void>
    460   struct SCEVVisitor {
    461     RetVal visit(const SCEV *S) {
    462       switch (S->getSCEVType()) {
    463       case scConstant:
    464         return ((SC*)this)->visitConstant((const SCEVConstant*)S);
    465       case scTruncate:
    466         return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
    467       case scZeroExtend:
    468         return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
    469       case scSignExtend:
    470         return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
    471       case scAddExpr:
    472         return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
    473       case scMulExpr:
    474         return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
    475       case scUDivExpr:
    476         return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
    477       case scAddRecExpr:
    478         return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
    479       case scSMaxExpr:
    480         return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
    481       case scUMaxExpr:
    482         return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
    483       case scUnknown:
    484         return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
    485       case scCouldNotCompute:
    486         return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
    487       default:
    488         llvm_unreachable("Unknown SCEV type!");
    489       }
    490     }
    491 
    492     RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
    493       llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
    494       return RetVal();
    495     }
    496   };
    497 }
    498 
    499 #endif
    500