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