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