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