Fix Transforms/IndVarsSimplify/2006-03-31-NegativeStride.ll and
[oota-llvm.git] / include / llvm / Analysis / ScalarEvolutionExpressions.h
1 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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
19 namespace llvm {
20   class ConstantInt;
21   class ConstantRange;
22
23   enum SCEVTypes {
24     // These should be ordered in terms of increasing complexity to make the
25     // folders simpler.
26     scConstant, scTruncate, scZeroExtend, scAddExpr, scMulExpr, scSDivExpr,
27     scAddRecExpr, scUnknown, scCouldNotCompute
28   };
29
30   //===--------------------------------------------------------------------===//
31   /// SCEVConstant - This class represents a constant integer value.
32   ///
33   class SCEVConstant : public SCEV {
34     ConstantInt *V;
35     SCEVConstant(ConstantInt *v) : SCEV(scConstant), V(v) {}
36
37     virtual ~SCEVConstant();
38   public:
39     /// get method - This just gets and returns a new SCEVConstant object.
40     ///
41     static SCEVHandle get(ConstantInt *V);
42
43     ConstantInt *getValue() const { return V; }
44
45     /// getValueRange - Return the tightest constant bounds that this value is
46     /// known to have.  This method is only valid on integer SCEV objects.
47     virtual ConstantRange getValueRange() const;
48
49     virtual bool isLoopInvariant(const Loop *L) const {
50       return true;
51     }
52
53     virtual bool hasComputableLoopEvolution(const Loop *L) const {
54       return false;  // Not loop variant
55     }
56
57     virtual const Type *getType() const;
58
59     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
60                                                  const SCEVHandle &Conc) const {
61       return this;
62     }
63
64     virtual void print(std::ostream &OS) const;
65
66     /// Methods for support type inquiry through isa, cast, and dyn_cast:
67     static inline bool classof(const SCEVConstant *S) { return true; }
68     static inline bool classof(const SCEV *S) {
69       return S->getSCEVType() == scConstant;
70     }
71   };
72
73   //===--------------------------------------------------------------------===//
74   /// SCEVTruncateExpr - This class represents a truncation of an integer value
75   /// to a smaller integer value.
76   ///
77   class SCEVTruncateExpr : public SCEV {
78     SCEVHandle Op;
79     const Type *Ty;
80     SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
81     virtual ~SCEVTruncateExpr();
82   public:
83     /// get method - This just gets and returns a new SCEVTruncate object
84     ///
85     static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
86
87     const SCEVHandle &getOperand() const { return Op; }
88     virtual const Type *getType() const { return Ty; }
89
90     virtual bool isLoopInvariant(const Loop *L) const {
91       return Op->isLoopInvariant(L);
92     }
93
94     virtual bool hasComputableLoopEvolution(const Loop *L) const {
95       return Op->hasComputableLoopEvolution(L);
96     }
97
98     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
99                                                  const SCEVHandle &Conc) const {
100       SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
101       if (H == Op)
102         return this;
103       return get(H, Ty);
104     }
105
106     /// getValueRange - Return the tightest constant bounds that this value is
107     /// known to have.  This method is only valid on integer SCEV objects.
108     virtual ConstantRange getValueRange() const;
109
110     virtual void print(std::ostream &OS) const;
111
112     /// Methods for support type inquiry through isa, cast, and dyn_cast:
113     static inline bool classof(const SCEVTruncateExpr *S) { return true; }
114     static inline bool classof(const SCEV *S) {
115       return S->getSCEVType() == scTruncate;
116     }
117   };
118
119   //===--------------------------------------------------------------------===//
120   /// SCEVZeroExtendExpr - This class represents a zero extension of a small
121   /// integer value to a larger integer value.
122   ///
123   class SCEVZeroExtendExpr : public SCEV {
124     SCEVHandle Op;
125     const Type *Ty;
126     SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
127     virtual ~SCEVZeroExtendExpr();
128   public:
129     /// get method - This just gets and returns a new SCEVZeroExtend object
130     ///
131     static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
132
133     const SCEVHandle &getOperand() const { return Op; }
134     virtual const Type *getType() const { return Ty; }
135
136     virtual bool isLoopInvariant(const Loop *L) const {
137       return Op->isLoopInvariant(L);
138     }
139
140     virtual bool hasComputableLoopEvolution(const Loop *L) const {
141       return Op->hasComputableLoopEvolution(L);
142     }
143
144     /// getValueRange - Return the tightest constant bounds that this value is
145     /// known to have.  This method is only valid on integer SCEV objects.
146     virtual ConstantRange getValueRange() const;
147
148     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
149                                                  const SCEVHandle &Conc) const {
150       SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
151       if (H == Op)
152         return this;
153       return get(H, Ty);
154     }
155
156     virtual void print(std::ostream &OS) const;
157
158     /// Methods for support type inquiry through isa, cast, and dyn_cast:
159     static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
160     static inline bool classof(const SCEV *S) {
161       return S->getSCEVType() == scZeroExtend;
162     }
163   };
164
165
166   //===--------------------------------------------------------------------===//
167   /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
168   /// operators.
169   ///
170   class SCEVCommutativeExpr : public SCEV {
171     std::vector<SCEVHandle> Operands;
172
173   protected:
174     SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
175       : SCEV(T) {
176       Operands.reserve(ops.size());
177       Operands.insert(Operands.end(), ops.begin(), ops.end());
178     }
179     ~SCEVCommutativeExpr();
180
181   public:
182     unsigned getNumOperands() const { return Operands.size(); }
183     const SCEVHandle &getOperand(unsigned i) const {
184       assert(i < Operands.size() && "Operand index out of range!");
185       return Operands[i];
186     }
187
188     const std::vector<SCEVHandle> &getOperands() const { return Operands; }
189     typedef std::vector<SCEVHandle>::const_iterator op_iterator;
190     op_iterator op_begin() const { return Operands.begin(); }
191     op_iterator op_end() const { return Operands.end(); }
192
193
194     virtual bool isLoopInvariant(const Loop *L) const {
195       for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
196         if (!getOperand(i)->isLoopInvariant(L)) return false;
197       return true;
198     }
199
200     // hasComputableLoopEvolution - Commutative expressions have computable loop
201     // evolutions iff they have at least one operand that varies with the loop,
202     // but that all varying operands are computable.
203     virtual bool hasComputableLoopEvolution(const Loop *L) const {
204       bool HasVarying = false;
205       for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
206         if (!getOperand(i)->isLoopInvariant(L))
207           if (getOperand(i)->hasComputableLoopEvolution(L))
208             HasVarying = true;
209           else
210             return false;
211       return HasVarying;
212     }
213
214     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
215                                                  const SCEVHandle &Conc) const;
216
217     virtual const char *getOperationStr() const = 0;
218
219     virtual const Type *getType() const { return getOperand(0)->getType(); }
220     virtual void print(std::ostream &OS) const;
221
222     /// Methods for support type inquiry through isa, cast, and dyn_cast:
223     static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
224     static inline bool classof(const SCEV *S) {
225       return S->getSCEVType() == scAddExpr ||
226              S->getSCEVType() == scMulExpr;
227     }
228   };
229
230
231   //===--------------------------------------------------------------------===//
232   /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
233   ///
234   class SCEVAddExpr : public SCEVCommutativeExpr {
235     SCEVAddExpr(const std::vector<SCEVHandle> &ops)
236       : SCEVCommutativeExpr(scAddExpr, ops) {
237     }
238
239   public:
240     static SCEVHandle get(std::vector<SCEVHandle> &Ops);
241
242     static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
243       std::vector<SCEVHandle> Ops;
244       Ops.push_back(LHS);
245       Ops.push_back(RHS);
246       return get(Ops);
247     }
248
249     static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
250                           const SCEVHandle &Op2) {
251       std::vector<SCEVHandle> Ops;
252       Ops.push_back(Op0);
253       Ops.push_back(Op1);
254       Ops.push_back(Op2);
255       return get(Ops);
256     }
257
258     virtual const char *getOperationStr() const { return " + "; }
259
260     /// Methods for support type inquiry through isa, cast, and dyn_cast:
261     static inline bool classof(const SCEVAddExpr *S) { return true; }
262     static inline bool classof(const SCEV *S) {
263       return S->getSCEVType() == scAddExpr;
264     }
265   };
266
267   //===--------------------------------------------------------------------===//
268   /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
269   ///
270   class SCEVMulExpr : public SCEVCommutativeExpr {
271     SCEVMulExpr(const std::vector<SCEVHandle> &ops)
272       : SCEVCommutativeExpr(scMulExpr, ops) {
273     }
274
275   public:
276     static SCEVHandle get(std::vector<SCEVHandle> &Ops);
277
278     static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
279       std::vector<SCEVHandle> Ops;
280       Ops.push_back(LHS);
281       Ops.push_back(RHS);
282       return get(Ops);
283     }
284
285     virtual const char *getOperationStr() const { return " * "; }
286
287     /// Methods for support type inquiry through isa, cast, and dyn_cast:
288     static inline bool classof(const SCEVMulExpr *S) { return true; }
289     static inline bool classof(const SCEV *S) {
290       return S->getSCEVType() == scMulExpr;
291     }
292   };
293
294
295   //===--------------------------------------------------------------------===//
296   /// SCEVSDivExpr - This class represents a binary unsigned division operation.
297   ///
298   class SCEVSDivExpr : public SCEV {
299     SCEVHandle LHS, RHS;
300     SCEVSDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
301       : SCEV(scSDivExpr), LHS(lhs), RHS(rhs) {}
302
303     virtual ~SCEVSDivExpr();
304   public:
305     /// get method - This just gets and returns a new SCEVSDiv object.
306     ///
307     static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
308
309     const SCEVHandle &getLHS() const { return LHS; }
310     const SCEVHandle &getRHS() const { return RHS; }
311
312     virtual bool isLoopInvariant(const Loop *L) const {
313       return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
314     }
315
316     virtual bool hasComputableLoopEvolution(const Loop *L) const {
317       return LHS->hasComputableLoopEvolution(L) &&
318              RHS->hasComputableLoopEvolution(L);
319     }
320
321     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
322                                                  const SCEVHandle &Conc) const {
323       SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
324       SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
325       if (L == LHS && R == RHS)
326         return this;
327       else
328         return get(L, R);
329     }
330
331
332     virtual const Type *getType() const;
333
334     void print(std::ostream &OS) const;
335
336     /// Methods for support type inquiry through isa, cast, and dyn_cast:
337     static inline bool classof(const SCEVSDivExpr *S) { return true; }
338     static inline bool classof(const SCEV *S) {
339       return S->getSCEVType() == scSDivExpr;
340     }
341   };
342
343
344   //===--------------------------------------------------------------------===//
345   /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
346   /// count of the specified loop.
347   ///
348   /// All operands of an AddRec are required to be loop invariant.
349   ///
350   class SCEVAddRecExpr : public SCEV {
351     std::vector<SCEVHandle> Operands;
352     const Loop *L;
353
354     SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
355       : SCEV(scAddRecExpr), Operands(ops), L(l) {
356       for (unsigned i = 0, e = Operands.size(); i != e; ++i)
357         assert(Operands[i]->isLoopInvariant(l) &&
358                "Operands of AddRec must be loop-invariant!");
359     }
360     ~SCEVAddRecExpr();
361   public:
362     static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
363                           const Loop *);
364     static SCEVHandle get(std::vector<SCEVHandle> &Operands,
365                           const Loop *);
366     static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
367                           const Loop *L) {
368       std::vector<SCEVHandle> NewOp(Operands);
369       return get(NewOp, L);
370     }
371
372     typedef std::vector<SCEVHandle>::const_iterator op_iterator;
373     op_iterator op_begin() const { return Operands.begin(); }
374     op_iterator op_end() const { return Operands.end(); }
375
376     unsigned getNumOperands() const { return Operands.size(); }
377     const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
378     const SCEVHandle &getStart() const { return Operands[0]; }
379     const Loop *getLoop() const { return L; }
380
381
382     /// getStepRecurrence - This method constructs and returns the recurrence
383     /// indicating how much this expression steps by.  If this is a polynomial
384     /// of degree N, it returns a chrec of degree N-1.
385     SCEVHandle getStepRecurrence() const {
386       if (getNumOperands() == 2) return getOperand(1);
387       return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
388                                  getLoop());
389     }
390
391     virtual bool hasComputableLoopEvolution(const Loop *QL) const {
392       if (L == QL) return true;
393       return false;
394     }
395
396     virtual bool isLoopInvariant(const Loop *QueryLoop) const;
397
398     virtual const Type *getType() const { return Operands[0]->getType(); }
399
400     /// isAffine - Return true if this is an affine AddRec (i.e., it represents
401     /// an expressions A+B*x where A and B are loop invariant values.
402     bool isAffine() const {
403       // We know that the start value is invariant.  This expression is thus
404       // affine iff the step is also invariant.
405       return getNumOperands() == 2;
406     }
407
408     /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
409     /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
410     /// invariant values.  This corresponds to an addrec of the form {L,+,M,+,N}
411     bool isQuadratic() const {
412       return getNumOperands() == 3;
413     }
414
415     /// evaluateAtIteration - Return the value of this chain of recurrences at
416     /// the specified iteration number.
417     SCEVHandle evaluateAtIteration(SCEVHandle It) const;
418
419     /// getNumIterationsInRange - Return the number of iterations of this loop
420     /// that produce values in the specified constant range.  Another way of
421     /// looking at this is that it returns the first iteration number where the
422     /// value is not in the condition, thus computing the exit count.  If the
423     /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
424     /// returned.
425     SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
426
427     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
428                                                  const SCEVHandle &Conc) const;
429
430     virtual void print(std::ostream &OS) const;
431
432     /// Methods for support type inquiry through isa, cast, and dyn_cast:
433     static inline bool classof(const SCEVAddRecExpr *S) { return true; }
434     static inline bool classof(const SCEV *S) {
435       return S->getSCEVType() == scAddRecExpr;
436     }
437   };
438
439   //===--------------------------------------------------------------------===//
440   /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
441   /// value, and only represent it as it's LLVM Value.  This is the "bottom"
442   /// value for the analysis.
443   ///
444   class SCEVUnknown : public SCEV {
445     Value *V;
446     SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
447
448   protected:
449     ~SCEVUnknown();
450   public:
451     /// get method - For SCEVUnknown, this just gets and returns a new
452     /// SCEVUnknown.
453     static SCEVHandle get(Value *V);
454
455     /// getIntegerSCEV - Given an integer or FP type, create a constant for the
456     /// specified signed integer value and return a SCEV for the constant.
457     static SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
458
459     Value *getValue() const { return V; }
460
461     virtual bool isLoopInvariant(const Loop *L) const;
462     virtual bool hasComputableLoopEvolution(const Loop *QL) const {
463       return false; // not computable
464     }
465
466     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
467                                                  const SCEVHandle &Conc) const {
468       if (&*Sym == this) return Conc;
469       return this;
470     }
471
472     virtual const Type *getType() const;
473
474     virtual void print(std::ostream &OS) const;
475
476     /// Methods for support type inquiry through isa, cast, and dyn_cast:
477     static inline bool classof(const SCEVUnknown *S) { return true; }
478     static inline bool classof(const SCEV *S) {
479       return S->getSCEVType() == scUnknown;
480     }
481   };
482
483   /// SCEVVisitor - This class defines a simple visitor class that may be used
484   /// for various SCEV analysis purposes.
485   template<typename SC, typename RetVal=void>
486   struct SCEVVisitor {
487     RetVal visit(SCEV *S) {
488       switch (S->getSCEVType()) {
489       case scConstant:
490         return ((SC*)this)->visitConstant((SCEVConstant*)S);
491       case scTruncate:
492         return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
493       case scZeroExtend:
494         return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
495       case scAddExpr:
496         return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
497       case scMulExpr:
498         return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
499       case scSDivExpr:
500         return ((SC*)this)->visitSDivExpr((SCEVSDivExpr*)S);
501       case scAddRecExpr:
502         return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
503       case scUnknown:
504         return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
505       case scCouldNotCompute:
506         return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
507       default:
508         assert(0 && "Unknown SCEV type!");
509         abort();
510       }
511     }
512
513     RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
514       assert(0 && "Invalid use of SCEVCouldNotCompute!");
515       abort();
516       return RetVal();
517     }
518   };
519 }
520
521 #endif
522