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