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