Add a new replaceSymbolicValuesWithConcrete method to the SCEV class,
[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     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     virtual bool hasComputableLoopEvolution(const Loop *L) const {
201       for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
202         if (getOperand(i)->hasComputableLoopEvolution(L)) return true;
203       return false;
204     }
205
206     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
207                                                  const SCEVHandle &Conc) const;
208
209     virtual const char *getOperationStr() const = 0;
210
211     virtual const Type *getType() const { return getOperand(0)->getType(); }
212     virtual void print(std::ostream &OS) const;
213
214     /// Methods for support type inquiry through isa, cast, and dyn_cast:
215     static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
216     static inline bool classof(const SCEV *S) {
217       return S->getSCEVType() == scAddExpr ||
218              S->getSCEVType() == scMulExpr;
219     }
220   };
221
222
223   //===--------------------------------------------------------------------===//
224   /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
225   ///
226   class SCEVAddExpr : public SCEVCommutativeExpr {
227     SCEVAddExpr(const std::vector<SCEVHandle> &ops)
228       : SCEVCommutativeExpr(scAddExpr, ops) {
229     }
230
231   public:
232     static SCEVHandle get(std::vector<SCEVHandle> &Ops);
233
234     static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
235       std::vector<SCEVHandle> Ops;
236       Ops.push_back(LHS);
237       Ops.push_back(RHS);
238       return get(Ops);
239     }
240
241     static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
242                           const SCEVHandle &Op2) {
243       std::vector<SCEVHandle> Ops;
244       Ops.push_back(Op0);
245       Ops.push_back(Op1);
246       Ops.push_back(Op2);
247       return get(Ops);
248     }
249
250     virtual const char *getOperationStr() const { return " + "; }
251
252     /// Methods for support type inquiry through isa, cast, and dyn_cast:
253     static inline bool classof(const SCEVAddExpr *S) { return true; }
254     static inline bool classof(const SCEV *S) {
255       return S->getSCEVType() == scAddExpr;
256     }
257   };
258
259   //===--------------------------------------------------------------------===//
260   /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
261   ///
262   class SCEVMulExpr : public SCEVCommutativeExpr {
263     SCEVMulExpr(const std::vector<SCEVHandle> &ops)
264       : SCEVCommutativeExpr(scMulExpr, ops) {
265     }
266
267   public:
268     static SCEVHandle get(std::vector<SCEVHandle> &Ops);
269
270     static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
271       std::vector<SCEVHandle> Ops;
272       Ops.push_back(LHS);
273       Ops.push_back(RHS);
274       return get(Ops);
275     }
276
277     virtual const char *getOperationStr() const { return " * "; }
278
279     /// Methods for support type inquiry through isa, cast, and dyn_cast:
280     static inline bool classof(const SCEVMulExpr *S) { return true; }
281     static inline bool classof(const SCEV *S) {
282       return S->getSCEVType() == scMulExpr;
283     }
284   };
285
286
287   //===--------------------------------------------------------------------===//
288   /// SCEVUDivExpr - This class represents a binary unsigned division operation.
289   ///
290   class SCEVUDivExpr : public SCEV {
291     SCEVHandle LHS, RHS;
292     SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
293       : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
294
295     virtual ~SCEVUDivExpr();
296   public:
297     /// get method - This just gets and returns a new SCEVUDiv object.
298     ///
299     static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
300
301     const SCEVHandle &getLHS() const { return LHS; }
302     const SCEVHandle &getRHS() const { return RHS; }
303
304     virtual bool isLoopInvariant(const Loop *L) const {
305       return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
306     }
307
308     virtual bool hasComputableLoopEvolution(const Loop *L) const {
309       return LHS->hasComputableLoopEvolution(L) &&
310              RHS->hasComputableLoopEvolution(L);
311     }
312
313     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
314                                                  const SCEVHandle &Conc) const {
315       SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
316       SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
317       if (L == LHS && R == RHS)
318         return this;
319       else
320         return get(L, R);
321     }
322
323
324     virtual const Type *getType() const;
325
326     void print(std::ostream &OS) const;
327
328     /// Methods for support type inquiry through isa, cast, and dyn_cast:
329     static inline bool classof(const SCEVUDivExpr *S) { return true; }
330     static inline bool classof(const SCEV *S) {
331       return S->getSCEVType() == scUDivExpr;
332     }
333   };
334
335
336   //===--------------------------------------------------------------------===//
337   /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
338   /// count of the specified loop.
339   ///
340   /// All operands of an AddRec are required to be loop invariant.
341   ///
342   class SCEVAddRecExpr : public SCEV {
343     std::vector<SCEVHandle> Operands;
344     const Loop *L;
345
346     SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
347       : SCEV(scAddRecExpr), Operands(ops), L(l) {
348       for (unsigned i = 0, e = Operands.size(); i != e; ++i)
349         assert(Operands[i]->isLoopInvariant(l) &&
350                "Operands of AddRec must be loop-invariant!");
351     }
352     ~SCEVAddRecExpr();
353   public:
354     static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
355                           const Loop *);
356     static SCEVHandle get(std::vector<SCEVHandle> &Operands,
357                           const Loop *);
358     static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
359                           const Loop *L) {
360       std::vector<SCEVHandle> NewOp(Operands);
361       return get(NewOp, L);
362     }
363
364     typedef std::vector<SCEVHandle>::const_iterator op_iterator;
365     op_iterator op_begin() const { return Operands.begin(); }
366     op_iterator op_end() const { return Operands.end(); }
367
368     unsigned getNumOperands() const { return Operands.size(); }
369     const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
370     const SCEVHandle &getStart() const { return Operands[0]; }
371     const Loop *getLoop() const { return L; }
372
373
374     /// getStepRecurrence - This method constructs and returns the recurrence
375     /// indicating how much this expression steps by.  If this is a polynomial
376     /// of degree N, it returns a chrec of degree N-1.
377     SCEVHandle getStepRecurrence() const {
378       if (getNumOperands() == 2) return getOperand(1);
379       return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
380                                  getLoop());
381     }
382
383     virtual bool hasComputableLoopEvolution(const Loop *QL) const {
384       if (L == QL) return true;
385       /// FIXME: What if the start or step value a recurrence for the specified
386       /// loop?
387       return false;
388     }
389
390     virtual bool isLoopInvariant(const Loop *QueryLoop) const;
391
392     virtual const Type *getType() const { return Operands[0]->getType(); }
393
394     /// isAffine - Return true if this is an affine AddRec (i.e., it represents
395     /// an expressions A+B*x where A and B are loop invariant values.
396     bool isAffine() const {
397       // We know that the start value is invariant.  This expression is thus
398       // affine iff the step is also invariant.
399       return getNumOperands() == 2;
400     }
401
402     /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
403     /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
404     /// invariant values.  This corresponds to an addrec of the form {L,+,M,+,N}
405     bool isQuadratic() const {
406       return getNumOperands() == 3;
407     }
408
409     /// evaluateAtIteration - Return the value of this chain of recurrences at
410     /// the specified iteration number.
411     SCEVHandle evaluateAtIteration(SCEVHandle It) const;
412
413     /// getNumIterationsInRange - Return the number of iterations of this loop
414     /// that produce values in the specified constant range.  Another way of
415     /// looking at this is that it returns the first iteration number where the
416     /// value is not in the condition, thus computing the exit count.  If the
417     /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
418     /// returned.
419     SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
420
421     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
422                                                  const SCEVHandle &Conc) const;
423
424     virtual void print(std::ostream &OS) const;
425
426     /// Methods for support type inquiry through isa, cast, and dyn_cast:
427     static inline bool classof(const SCEVAddRecExpr *S) { return true; }
428     static inline bool classof(const SCEV *S) {
429       return S->getSCEVType() == scAddRecExpr;
430     }
431   };
432
433   //===--------------------------------------------------------------------===//
434   /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
435   /// value, and only represent it as it's LLVM Value.  This is the "bottom"
436   /// value for the analysis.
437   ///
438   class SCEVUnknown : public SCEV {
439     Value *V;
440     SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
441
442   protected:
443     ~SCEVUnknown();
444   public:
445     /// get method - For SCEVUnknown, this just gets and returns a new
446     /// SCEVUnknown.
447     static SCEVHandle get(Value *V);
448
449     /// getIntegerSCEV - Given an integer or FP type, create a constant for the
450     /// specified signed integer value and return a SCEV for the constant.
451     static SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
452
453     Value *getValue() const { return V; }
454
455     virtual bool isLoopInvariant(const Loop *L) const;
456     virtual bool hasComputableLoopEvolution(const Loop *QL) const {
457       return false; // not computable
458     }
459
460     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
461                                                  const SCEVHandle &Conc) const {
462       if (&*Sym == this) return Conc;
463       return this;
464     }
465
466     virtual const Type *getType() const;
467
468     virtual void print(std::ostream &OS) const;
469
470     /// Methods for support type inquiry through isa, cast, and dyn_cast:
471     static inline bool classof(const SCEVUnknown *S) { return true; }
472     static inline bool classof(const SCEV *S) {
473       return S->getSCEVType() == scUnknown;
474     }
475   };
476
477   /// SCEVVisitor - This class defines a simple visitor class that may be used
478   /// for various SCEV analysis purposes.
479   template<typename SC, typename RetVal=void>
480   struct SCEVVisitor {
481     RetVal visit(SCEV *S) {
482       switch (S->getSCEVType()) {
483       case scConstant:
484         return ((SC*)this)->visitConstant((SCEVConstant*)S);
485       case scTruncate:
486         return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
487       case scZeroExtend:
488         return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
489       case scAddExpr:
490         return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
491       case scMulExpr:
492         return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
493       case scUDivExpr:
494         return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S);
495       case scAddRecExpr:
496         return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
497       case scUnknown:
498         return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
499       case scCouldNotCompute:
500         return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
501       default:
502         assert(0 && "Unknown SCEV type!");
503         abort();
504       }
505     }
506
507     RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
508       assert(0 && "Invalid use of SCEVCouldNotCompute!");
509       abort();
510       return RetVal();
511     }
512   };
513 }
514
515 #endif
516