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