Strengthen the "non-constant stride must dominate loop preheader" check.
[oota-llvm.git] / include / llvm / Analysis / ScalarEvolutionExpressions.h
1 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the classes used to represent and build scalar expressions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPRESSIONS_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPRESSIONS_H
16
17 #include "llvm/Analysis/ScalarEvolution.h"
18
19 namespace llvm {
20   class ConstantInt;
21   class ConstantRange;
22   class APInt;
23   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) : SCEV(scConstant), V(v) {}
41
42     virtual ~SCEVConstant();
43   public:
44     ConstantInt *getValue() const { return V; }
45
46     virtual bool isLoopInvariant(const Loop *L) const {
47       return true;
48     }
49
50     virtual bool hasComputableLoopEvolution(const Loop *L) const {
51       return false;  // Not loop variant
52     }
53
54     virtual const Type *getType() const;
55
56     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
57                                                  const SCEVHandle &Conc,
58                                                  ScalarEvolution &SE) const {
59       return this;
60     }
61
62     bool dominates(BasicBlock *BB, DominatorTree *DT) const {
63       return true;
64     }
65
66     virtual void print(std::ostream &OS) const;
67     void print(std::ostream *OS) const { if (OS) print(*OS); }
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   /// SCEVTruncateExpr - This class represents a truncation of an integer value
78   /// to a smaller integer value.
79   ///
80   class SCEVTruncateExpr : public SCEV {
81     friend class ScalarEvolution;
82
83     SCEVHandle Op;
84     const Type *Ty;
85     SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
86     virtual ~SCEVTruncateExpr();
87   public:
88     const SCEVHandle &getOperand() const { return Op; }
89     virtual const Type *getType() const { return Ty; }
90
91     virtual bool isLoopInvariant(const Loop *L) const {
92       return Op->isLoopInvariant(L);
93     }
94
95     virtual bool hasComputableLoopEvolution(const Loop *L) const {
96       return Op->hasComputableLoopEvolution(L);
97     }
98
99     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
100                                                  const SCEVHandle &Conc,
101                                                  ScalarEvolution &SE) const {
102       SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
103       if (H == Op)
104         return this;
105       return SE.getTruncateExpr(H, Ty);
106     }
107
108     virtual bool dominates(BasicBlock *BB, DominatorTree *DT) 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     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
144                                                  const SCEVHandle &Conc,
145                                                  ScalarEvolution &SE) const {
146       SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
147       if (H == Op)
148         return this;
149       return SE.getZeroExtendExpr(H, Ty);
150     }
151
152     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
153
154     virtual void print(std::ostream &OS) const;
155     void print(std::ostream *OS) const { if (OS) print(*OS); }
156
157     /// Methods for support type inquiry through isa, cast, and dyn_cast:
158     static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
159     static inline bool classof(const SCEV *S) {
160       return S->getSCEVType() == scZeroExtend;
161     }
162   };
163
164   //===--------------------------------------------------------------------===//
165   /// SCEVSignExtendExpr - This class represents a sign extension of a small
166   /// integer value to a larger integer value.
167   ///
168   class SCEVSignExtendExpr : public SCEV {
169     friend class ScalarEvolution;
170
171     SCEVHandle Op;
172     const Type *Ty;
173     SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty);
174     virtual ~SCEVSignExtendExpr();
175   public:
176     const SCEVHandle &getOperand() const { return Op; }
177     virtual const Type *getType() const { return Ty; }
178
179     virtual bool isLoopInvariant(const Loop *L) const {
180       return Op->isLoopInvariant(L);
181     }
182
183     virtual bool hasComputableLoopEvolution(const Loop *L) const {
184       return Op->hasComputableLoopEvolution(L);
185     }
186
187     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
188                                                  const SCEVHandle &Conc,
189                                                  ScalarEvolution &SE) const {
190       SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
191       if (H == Op)
192         return this;
193       return SE.getSignExtendExpr(H, Ty);
194     }
195
196     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
197
198     virtual void print(std::ostream &OS) const;
199     void print(std::ostream *OS) const { if (OS) print(*OS); }
200
201     /// Methods for support type inquiry through isa, cast, and dyn_cast:
202     static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
203     static inline bool classof(const SCEV *S) {
204       return S->getSCEVType() == scSignExtend;
205     }
206   };
207
208
209   //===--------------------------------------------------------------------===//
210   /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
211   /// operators.
212   ///
213   class SCEVCommutativeExpr : public SCEV {
214     friend class ScalarEvolution;
215
216     std::vector<SCEVHandle> Operands;
217
218   protected:
219     SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
220       : SCEV(T) {
221       Operands.reserve(ops.size());
222       Operands.insert(Operands.end(), ops.begin(), ops.end());
223     }
224     ~SCEVCommutativeExpr();
225
226   public:
227     unsigned getNumOperands() const { return (unsigned)Operands.size(); }
228     const SCEVHandle &getOperand(unsigned i) const {
229       assert(i < Operands.size() && "Operand index out of range!");
230       return Operands[i];
231     }
232
233     const std::vector<SCEVHandle> &getOperands() const { return Operands; }
234     typedef std::vector<SCEVHandle>::const_iterator op_iterator;
235     op_iterator op_begin() const { return Operands.begin(); }
236     op_iterator op_end() const { return Operands.end(); }
237
238
239     virtual bool isLoopInvariant(const Loop *L) const {
240       for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
241         if (!getOperand(i)->isLoopInvariant(L)) return false;
242       return true;
243     }
244
245     // hasComputableLoopEvolution - Commutative expressions have computable loop
246     // evolutions iff they have at least one operand that varies with the loop,
247     // but that all varying operands are computable.
248     virtual bool hasComputableLoopEvolution(const Loop *L) const {
249       bool HasVarying = false;
250       for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
251         if (!getOperand(i)->isLoopInvariant(L)) {
252           if (getOperand(i)->hasComputableLoopEvolution(L))
253             HasVarying = true;
254           else
255             return false;
256         }
257       return HasVarying;
258     }
259
260     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
261                                                  const SCEVHandle &Conc,
262                                                  ScalarEvolution &SE) const;
263
264     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
265
266     virtual const char *getOperationStr() const = 0;
267
268     virtual const Type *getType() const { return getOperand(0)->getType(); }
269     virtual void print(std::ostream &OS) const;
270     void print(std::ostream *OS) const { if (OS) print(*OS); }
271
272     /// Methods for support type inquiry through isa, cast, and dyn_cast:
273     static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
274     static inline bool classof(const SCEV *S) {
275       return S->getSCEVType() == scAddExpr ||
276              S->getSCEVType() == scMulExpr ||
277              S->getSCEVType() == scSMaxExpr ||
278              S->getSCEVType() == scUMaxExpr;
279     }
280   };
281
282
283   //===--------------------------------------------------------------------===//
284   /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
285   ///
286   class SCEVAddExpr : public SCEVCommutativeExpr {
287     friend class ScalarEvolution;
288
289     explicit SCEVAddExpr(const std::vector<SCEVHandle> &ops)
290       : SCEVCommutativeExpr(scAddExpr, ops) {
291     }
292
293   public:
294     virtual const char *getOperationStr() const { return " + "; }
295
296     /// Methods for support type inquiry through isa, cast, and dyn_cast:
297     static inline bool classof(const SCEVAddExpr *S) { return true; }
298     static inline bool classof(const SCEV *S) {
299       return S->getSCEVType() == scAddExpr;
300     }
301   };
302
303   //===--------------------------------------------------------------------===//
304   /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
305   ///
306   class SCEVMulExpr : public SCEVCommutativeExpr {
307     friend class ScalarEvolution;
308
309     explicit SCEVMulExpr(const std::vector<SCEVHandle> &ops)
310       : SCEVCommutativeExpr(scMulExpr, ops) {
311     }
312
313   public:
314     virtual const char *getOperationStr() const { return " * "; }
315
316     /// Methods for support type inquiry through isa, cast, and dyn_cast:
317     static inline bool classof(const SCEVMulExpr *S) { return true; }
318     static inline bool classof(const SCEV *S) {
319       return S->getSCEVType() == scMulExpr;
320     }
321   };
322
323
324   //===--------------------------------------------------------------------===//
325   /// SCEVUDivExpr - This class represents a binary unsigned division operation.
326   ///
327   class SCEVUDivExpr : public SCEV {
328     friend class ScalarEvolution;
329
330     SCEVHandle LHS, RHS;
331     SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
332       : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
333
334     virtual ~SCEVUDivExpr();
335   public:
336     const SCEVHandle &getLHS() const { return LHS; }
337     const SCEVHandle &getRHS() const { return RHS; }
338
339     virtual bool isLoopInvariant(const Loop *L) const {
340       return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
341     }
342
343     virtual bool hasComputableLoopEvolution(const Loop *L) const {
344       return LHS->hasComputableLoopEvolution(L) &&
345              RHS->hasComputableLoopEvolution(L);
346     }
347
348     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
349                                                  const SCEVHandle &Conc,
350                                                  ScalarEvolution &SE) const {
351       SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
352       SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
353       if (L == LHS && R == RHS)
354         return this;
355       else
356         return SE.getUDivExpr(L, R);
357     }
358
359     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
360
361     virtual const Type *getType() const;
362
363     void print(std::ostream &OS) const;
364     void print(std::ostream *OS) const { if (OS) print(*OS); }
365
366     /// Methods for support type inquiry through isa, cast, and dyn_cast:
367     static inline bool classof(const SCEVUDivExpr *S) { return true; }
368     static inline bool classof(const SCEV *S) {
369       return S->getSCEVType() == scUDivExpr;
370     }
371   };
372
373
374   //===--------------------------------------------------------------------===//
375   /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
376   /// count of the specified loop.
377   ///
378   /// All operands of an AddRec are required to be loop invariant.
379   ///
380   class SCEVAddRecExpr : public SCEV {
381     friend class ScalarEvolution;
382
383     std::vector<SCEVHandle> Operands;
384     const Loop *L;
385
386     SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
387       : SCEV(scAddRecExpr), Operands(ops), L(l) {
388       for (size_t i = 0, e = Operands.size(); i != e; ++i)
389         assert(Operands[i]->isLoopInvariant(l) &&
390                "Operands of AddRec must be loop-invariant!");
391     }
392     ~SCEVAddRecExpr();
393   public:
394     typedef std::vector<SCEVHandle>::const_iterator op_iterator;
395     op_iterator op_begin() const { return Operands.begin(); }
396     op_iterator op_end() const { return Operands.end(); }
397
398     unsigned getNumOperands() const { return (unsigned)Operands.size(); }
399     const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
400     const SCEVHandle &getStart() const { return Operands[0]; }
401     const Loop *getLoop() const { return L; }
402
403
404     /// getStepRecurrence - This method constructs and returns the recurrence
405     /// indicating how much this expression steps by.  If this is a polynomial
406     /// of degree N, it returns a chrec of degree N-1.
407     SCEVHandle getStepRecurrence(ScalarEvolution &SE) const {
408       if (isAffine()) return getOperand(1);
409       return SE.getAddRecExpr(std::vector<SCEVHandle>(op_begin()+1,op_end()),
410                               getLoop());
411     }
412
413     virtual bool hasComputableLoopEvolution(const Loop *QL) const {
414       if (L == QL) return true;
415       return false;
416     }
417
418     virtual bool isLoopInvariant(const Loop *QueryLoop) const;
419
420     virtual const Type *getType() const { return Operands[0]->getType(); }
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     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
455
456     virtual void print(std::ostream &OS) const;
457     void print(std::ostream *OS) const { if (OS) print(*OS); }
458
459     /// Methods for support type inquiry through isa, cast, and dyn_cast:
460     static inline bool classof(const SCEVAddRecExpr *S) { return true; }
461     static inline bool classof(const SCEV *S) {
462       return S->getSCEVType() == scAddRecExpr;
463     }
464   };
465
466
467   //===--------------------------------------------------------------------===//
468   /// SCEVSMaxExpr - This class represents a signed maximum selection.
469   ///
470   class SCEVSMaxExpr : public SCEVCommutativeExpr {
471     friend class ScalarEvolution;
472
473     explicit SCEVSMaxExpr(const std::vector<SCEVHandle> &ops)
474       : SCEVCommutativeExpr(scSMaxExpr, ops) {
475     }
476
477   public:
478     virtual const char *getOperationStr() const { return " smax "; }
479
480     /// Methods for support type inquiry through isa, cast, and dyn_cast:
481     static inline bool classof(const SCEVSMaxExpr *S) { return true; }
482     static inline bool classof(const SCEV *S) {
483       return S->getSCEVType() == scSMaxExpr;
484     }
485   };
486
487
488   //===--------------------------------------------------------------------===//
489   /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
490   ///
491   class SCEVUMaxExpr : public SCEVCommutativeExpr {
492     friend class ScalarEvolution;
493
494     explicit SCEVUMaxExpr(const std::vector<SCEVHandle> &ops)
495       : SCEVCommutativeExpr(scUMaxExpr, ops) {
496     }
497
498   public:
499     virtual const char *getOperationStr() const { return " umax "; }
500
501     /// Methods for support type inquiry through isa, cast, and dyn_cast:
502     static inline bool classof(const SCEVUMaxExpr *S) { return true; }
503     static inline bool classof(const SCEV *S) {
504       return S->getSCEVType() == scUMaxExpr;
505     }
506   };
507
508
509   //===--------------------------------------------------------------------===//
510   /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
511   /// value, and only represent it as it's LLVM Value.  This is the "bottom"
512   /// value for the analysis.
513   ///
514   class SCEVUnknown : public SCEV {
515     friend class ScalarEvolution;
516
517     Value *V;
518     explicit SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
519
520   protected:
521     ~SCEVUnknown();
522   public:
523     Value *getValue() const { return V; }
524
525     virtual bool isLoopInvariant(const Loop *L) const;
526     virtual bool hasComputableLoopEvolution(const Loop *QL) const {
527       return false; // not computable
528     }
529
530     SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
531                                                  const SCEVHandle &Conc,
532                                                  ScalarEvolution &SE) const {
533       if (&*Sym == this) return Conc;
534       return this;
535     }
536
537     bool dominates(BasicBlock *BB, DominatorTree *DT) const;
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 scUDivExpr:
570         return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S);
571       case scAddRecExpr:
572         return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
573       case scSMaxExpr:
574         return ((SC*)this)->visitSMaxExpr((SCEVSMaxExpr*)S);
575       case scUMaxExpr:
576         return ((SC*)this)->visitUMaxExpr((SCEVUMaxExpr*)S);
577       case scUnknown:
578         return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
579       case scCouldNotCompute:
580         return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
581       default:
582         assert(0 && "Unknown SCEV type!");
583         abort();
584       }
585     }
586
587     RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
588       assert(0 && "Invalid use of SCEVCouldNotCompute!");
589       abort();
590       return RetVal();
591     }
592   };
593 }
594
595 #endif