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