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