Banish global state from ScalarEvolution! SCEV uniquing is now done by tables attach...
[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, const ScalarEvolution* p) :
40       SCEV(scConstant, p), 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     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
55                                                  const SCEVHandle &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     SCEVHandle Op;
79     const Type *Ty;
80
81     SCEVCastExpr(unsigned SCEVTy, const SCEVHandle &op, const Type *ty,
82                  const ScalarEvolution* p);
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                      const ScalarEvolution* p);
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                        const ScalarEvolution* p);
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                        const ScalarEvolution* p);
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                  const ScalarEvolution* p)
206       : SCEV(T, p), Operands(ops.begin(), ops.end()) {}
207     virtual ~SCEVNAryExpr() {}
208
209   public:
210     unsigned getNumOperands() const { return (unsigned)Operands.size(); }
211     const SCEVHandle &getOperand(unsigned i) const {
212       assert(i < Operands.size() && "Operand index out of range!");
213       return Operands[i];
214     }
215
216     const SmallVectorImpl<SCEVHandle> &getOperands() const { return Operands; }
217     typedef SmallVectorImpl<SCEVHandle>::const_iterator op_iterator;
218     op_iterator op_begin() const { return Operands.begin(); }
219     op_iterator op_end() const { return Operands.end(); }
220
221     virtual bool isLoopInvariant(const Loop *L) const {
222       for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
223         if (!getOperand(i)->isLoopInvariant(L)) return false;
224       return true;
225     }
226
227     // hasComputableLoopEvolution - N-ary expressions have computable loop
228     // evolutions iff they have at least one operand that varies with the loop,
229     // but that all varying operands are computable.
230     virtual bool hasComputableLoopEvolution(const Loop *L) const {
231       bool HasVarying = false;
232       for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
233         if (!getOperand(i)->isLoopInvariant(L)) {
234           if (getOperand(i)->hasComputableLoopEvolution(L))
235             HasVarying = true;
236           else
237             return false;
238         }
239       return HasVarying;
240     }
241
242     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
243
244     virtual const Type *getType() const { return getOperand(0)->getType(); }
245
246     /// Methods for support type inquiry through isa, cast, and dyn_cast:
247     static inline bool classof(const SCEVNAryExpr *S) { return true; }
248     static inline bool classof(const SCEV *S) {
249       return S->getSCEVType() == scAddExpr ||
250              S->getSCEVType() == scMulExpr ||
251              S->getSCEVType() == scSMaxExpr ||
252              S->getSCEVType() == scUMaxExpr ||
253              S->getSCEVType() == scAddRecExpr;
254     }
255   };
256
257   //===--------------------------------------------------------------------===//
258   /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
259   /// operators.
260   ///
261   class SCEVCommutativeExpr : public SCEVNAryExpr {
262   protected:
263     SCEVCommutativeExpr(enum SCEVTypes T,
264                         const SmallVectorImpl<SCEVHandle> &ops,
265                         const ScalarEvolution* p)
266       : SCEVNAryExpr(T, ops, p) {}
267
268   public:
269     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
270                                                  const SCEVHandle &Conc,
271                                                  ScalarEvolution &SE) const;
272
273     virtual const char *getOperationStr() const = 0;
274
275     virtual void print(raw_ostream &OS) const;
276
277     /// Methods for support type inquiry through isa, cast, and dyn_cast:
278     static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
279     static inline bool classof(const SCEV *S) {
280       return S->getSCEVType() == scAddExpr ||
281              S->getSCEVType() == scMulExpr ||
282              S->getSCEVType() == scSMaxExpr ||
283              S->getSCEVType() == scUMaxExpr;
284     }
285   };
286
287
288   //===--------------------------------------------------------------------===//
289   /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
290   ///
291   class SCEVAddExpr : public SCEVCommutativeExpr {
292     friend class ScalarEvolution;
293
294     explicit SCEVAddExpr(const SmallVectorImpl<SCEVHandle> &ops,
295                          const ScalarEvolution* p)
296       : SCEVCommutativeExpr(scAddExpr, ops, p) {
297     }
298
299   public:
300     virtual const char *getOperationStr() const { return " + "; }
301
302     /// Methods for support type inquiry through isa, cast, and dyn_cast:
303     static inline bool classof(const SCEVAddExpr *S) { return true; }
304     static inline bool classof(const SCEV *S) {
305       return S->getSCEVType() == scAddExpr;
306     }
307   };
308
309   //===--------------------------------------------------------------------===//
310   /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
311   ///
312   class SCEVMulExpr : public SCEVCommutativeExpr {
313     friend class ScalarEvolution;
314
315     explicit SCEVMulExpr(const SmallVectorImpl<SCEVHandle> &ops,
316                          const ScalarEvolution* p)
317       : SCEVCommutativeExpr(scMulExpr, ops, p) {
318     }
319
320   public:
321     virtual const char *getOperationStr() const { return " * "; }
322
323     /// Methods for support type inquiry through isa, cast, and dyn_cast:
324     static inline bool classof(const SCEVMulExpr *S) { return true; }
325     static inline bool classof(const SCEV *S) {
326       return S->getSCEVType() == scMulExpr;
327     }
328   };
329
330
331   //===--------------------------------------------------------------------===//
332   /// SCEVUDivExpr - This class represents a binary unsigned division operation.
333   ///
334   class SCEVUDivExpr : public SCEV {
335     friend class ScalarEvolution;
336
337     SCEVHandle LHS, RHS;
338     SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs,
339                  const ScalarEvolution* p)
340       : SCEV(scUDivExpr, p), LHS(lhs), RHS(rhs) {}
341
342   public:
343     const SCEVHandle &getLHS() const { return LHS; }
344     const SCEVHandle &getRHS() const { return RHS; }
345
346     virtual bool isLoopInvariant(const Loop *L) const {
347       return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
348     }
349
350     virtual bool hasComputableLoopEvolution(const Loop *L) const {
351       return LHS->hasComputableLoopEvolution(L) &&
352              RHS->hasComputableLoopEvolution(L);
353     }
354
355     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
356                                                  const SCEVHandle &Conc,
357                                                  ScalarEvolution &SE) const {
358       SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
359       SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
360       if (L == LHS && R == RHS)
361         return this;
362       else
363         return SE.getUDivExpr(L, R);
364     }
365
366     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
367
368     virtual const Type *getType() const;
369
370     void print(raw_ostream &OS) const;
371
372     /// Methods for support type inquiry through isa, cast, and dyn_cast:
373     static inline bool classof(const SCEVUDivExpr *S) { return true; }
374     static inline bool classof(const SCEV *S) {
375       return S->getSCEVType() == scUDivExpr;
376     }
377   };
378
379
380   //===--------------------------------------------------------------------===//
381   /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
382   /// count of the specified loop.  This is the primary focus of the
383   /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
384   /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
385   /// created and analyzed.
386   ///
387   /// All operands of an AddRec are required to be loop invariant.
388   ///
389   class SCEVAddRecExpr : public SCEVNAryExpr {
390     friend class ScalarEvolution;
391
392     const Loop *L;
393
394     SCEVAddRecExpr(const SmallVectorImpl<SCEVHandle> &ops, const Loop *l,
395                    const ScalarEvolution* p)
396       : SCEVNAryExpr(scAddRecExpr, ops, p), L(l) {
397       for (size_t i = 0, e = Operands.size(); i != e; ++i)
398         assert(Operands[i]->isLoopInvariant(l) &&
399                "Operands of AddRec must be loop-invariant!");
400     }
401
402   public:
403     const SCEVHandle &getStart() const { return Operands[0]; }
404     const Loop *getLoop() const { return L; }
405
406     /// getStepRecurrence - This method constructs and returns the recurrence
407     /// indicating how much this expression steps by.  If this is a polynomial
408     /// of degree N, it returns a chrec of degree N-1.
409     SCEVHandle getStepRecurrence(ScalarEvolution &SE) const {
410       if (isAffine()) return getOperand(1);
411       return SE.getAddRecExpr(SmallVector<SCEVHandle, 3>(op_begin()+1,op_end()),
412                               getLoop());
413     }
414
415     virtual bool hasComputableLoopEvolution(const Loop *QL) const {
416       if (L == QL) return true;
417       return false;
418     }
419
420     virtual bool isLoopInvariant(const Loop *QueryLoop) const;
421
422     /// isAffine - Return true if this is an affine AddRec (i.e., it represents
423     /// an expressions A+B*x where A and B are loop invariant values.
424     bool isAffine() const {
425       // We know that the start value is invariant.  This expression is thus
426       // affine iff the step is also invariant.
427       return getNumOperands() == 2;
428     }
429
430     /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
431     /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
432     /// invariant values.  This corresponds to an addrec of the form {L,+,M,+,N}
433     bool isQuadratic() const {
434       return getNumOperands() == 3;
435     }
436
437     /// evaluateAtIteration - Return the value of this chain of recurrences at
438     /// the specified iteration number.
439     SCEVHandle evaluateAtIteration(SCEVHandle It, ScalarEvolution &SE) const;
440
441     /// getNumIterationsInRange - Return the number of iterations of this loop
442     /// that produce values in the specified constant range.  Another way of
443     /// looking at this is that it returns the first iteration number where the
444     /// value is not in the condition, thus computing the exit count.  If the
445     /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
446     /// returned.
447     SCEVHandle getNumIterationsInRange(ConstantRange Range,
448                                        ScalarEvolution &SE) const;
449
450     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
451                                                  const SCEVHandle &Conc,
452                                                  ScalarEvolution &SE) const;
453
454     virtual void print(raw_ostream &OS) const;
455
456     /// Methods for support type inquiry through isa, cast, and dyn_cast:
457     static inline bool classof(const SCEVAddRecExpr *S) { return true; }
458     static inline bool classof(const SCEV *S) {
459       return S->getSCEVType() == scAddRecExpr;
460     }
461   };
462
463
464   //===--------------------------------------------------------------------===//
465   /// SCEVSMaxExpr - This class represents a signed maximum selection.
466   ///
467   class SCEVSMaxExpr : public SCEVCommutativeExpr {
468     friend class ScalarEvolution;
469
470     explicit SCEVSMaxExpr(const SmallVectorImpl<SCEVHandle> &ops,
471                           const ScalarEvolution* p)
472       : SCEVCommutativeExpr(scSMaxExpr, ops, p) {
473     }
474
475   public:
476     virtual const char *getOperationStr() const { return " smax "; }
477
478     /// Methods for support type inquiry through isa, cast, and dyn_cast:
479     static inline bool classof(const SCEVSMaxExpr *S) { return true; }
480     static inline bool classof(const SCEV *S) {
481       return S->getSCEVType() == scSMaxExpr;
482     }
483   };
484
485
486   //===--------------------------------------------------------------------===//
487   /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
488   ///
489   class SCEVUMaxExpr : public SCEVCommutativeExpr {
490     friend class ScalarEvolution;
491
492     explicit SCEVUMaxExpr(const SmallVectorImpl<SCEVHandle> &ops,
493                           const ScalarEvolution* p)
494       : SCEVCommutativeExpr(scUMaxExpr, ops, p) {
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, const ScalarEvolution* p) :
518       SCEV(scUnknown, p), V(v) {}
519       
520   public:
521     Value *getValue() const { return V; }
522
523     virtual bool isLoopInvariant(const Loop *L) const;
524     virtual bool hasComputableLoopEvolution(const Loop *QL) const {
525       return false; // not computable
526     }
527
528     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
529                                                  const SCEVHandle &Conc,
530                                                  ScalarEvolution &SE) const {
531       if (&*Sym == this) return Conc;
532       return this;
533     }
534
535     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
536
537     virtual const Type *getType() const;
538
539     virtual void print(raw_ostream &OS) const;
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(const SCEV *S) {
553       switch (S->getSCEVType()) {
554       case scConstant:
555         return ((SC*)this)->visitConstant((const SCEVConstant*)S);
556       case scTruncate:
557         return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
558       case scZeroExtend:
559         return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
560       case scSignExtend:
561         return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
562       case scAddExpr:
563         return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
564       case scMulExpr:
565         return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
566       case scUDivExpr:
567         return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
568       case scAddRecExpr:
569         return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
570       case scSMaxExpr:
571         return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
572       case scUMaxExpr:
573         return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
574       case scUnknown:
575         return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
576       case scCouldNotCompute:
577         return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
578       default:
579         assert(0 && "Unknown SCEV type!");
580         abort();
581       }
582     }
583
584     RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
585       assert(0 && "Invalid use of SCEVCouldNotCompute!");
586       abort();
587       return RetVal();
588     }
589   };
590 }
591
592 #endif