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