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