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