b468fcd025d84ee545fac5f23f7cbb1c252188e8
[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_SCALAREVOLUTIONEXPRESSIONS_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
16
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Analysis/ScalarEvolution.h"
20 #include "llvm/Support/ErrorHandling.h"
21
22 namespace llvm {
23   class ConstantInt;
24   class ConstantRange;
25   class DominatorTree;
26
27   enum SCEVTypes {
28     // These should be ordered in terms of increasing complexity to make the
29     // folders simpler.
30     scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
31     scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr,
32     scUnknown, scCouldNotCompute
33   };
34
35   //===--------------------------------------------------------------------===//
36   /// SCEVConstant - This class represents a constant integer value.
37   ///
38   class SCEVConstant : public SCEV {
39     friend class ScalarEvolution;
40
41     ConstantInt *V;
42     SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
43       SCEV(ID, scConstant), V(v) {}
44   public:
45     ConstantInt *getValue() const { return V; }
46
47     Type *getType() const { return V->getType(); }
48
49     /// Methods for support type inquiry through isa, cast, and dyn_cast:
50     static inline bool classof(const SCEV *S) {
51       return S->getSCEVType() == scConstant;
52     }
53   };
54
55   //===--------------------------------------------------------------------===//
56   /// SCEVCastExpr - This is the base class for unary cast operator classes.
57   ///
58   class SCEVCastExpr : public SCEV {
59   protected:
60     const SCEV *Op;
61     Type *Ty;
62
63     SCEVCastExpr(const FoldingSetNodeIDRef ID,
64                  unsigned SCEVTy, const SCEV *op, Type *ty);
65
66   public:
67     const SCEV *getOperand() const { return Op; }
68     Type *getType() const { return Ty; }
69
70     /// Methods for support type inquiry through isa, cast, and dyn_cast:
71     static inline bool classof(const SCEV *S) {
72       return S->getSCEVType() == scTruncate ||
73              S->getSCEVType() == scZeroExtend ||
74              S->getSCEVType() == scSignExtend;
75     }
76   };
77
78   //===--------------------------------------------------------------------===//
79   /// SCEVTruncateExpr - This class represents a truncation of an integer value
80   /// to a smaller integer value.
81   ///
82   class SCEVTruncateExpr : public SCEVCastExpr {
83     friend class ScalarEvolution;
84
85     SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
86                      const SCEV *op, Type *ty);
87
88   public:
89     /// Methods for support type inquiry through isa, cast, and dyn_cast:
90     static inline bool classof(const SCEV *S) {
91       return S->getSCEVType() == scTruncate;
92     }
93   };
94
95   //===--------------------------------------------------------------------===//
96   /// SCEVZeroExtendExpr - This class represents a zero extension of a small
97   /// integer value to a larger integer value.
98   ///
99   class SCEVZeroExtendExpr : public SCEVCastExpr {
100     friend class ScalarEvolution;
101
102     SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
103                        const SCEV *op, Type *ty);
104
105   public:
106     /// Methods for support type inquiry through isa, cast, and dyn_cast:
107     static inline bool classof(const SCEV *S) {
108       return S->getSCEVType() == scZeroExtend;
109     }
110   };
111
112   //===--------------------------------------------------------------------===//
113   /// SCEVSignExtendExpr - This class represents a sign extension of a small
114   /// integer value to a larger integer value.
115   ///
116   class SCEVSignExtendExpr : public SCEVCastExpr {
117     friend class ScalarEvolution;
118
119     SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
120                        const SCEV *op, Type *ty);
121
122   public:
123     /// Methods for support type inquiry through isa, cast, and dyn_cast:
124     static inline bool classof(const SCEV *S) {
125       return S->getSCEVType() == scSignExtend;
126     }
127   };
128
129
130   //===--------------------------------------------------------------------===//
131   /// SCEVNAryExpr - This node is a base class providing common
132   /// functionality for n'ary operators.
133   ///
134   class SCEVNAryExpr : public SCEV {
135   protected:
136     // Since SCEVs are immutable, ScalarEvolution allocates operand
137     // arrays with its SCEVAllocator, so this class just needs a simple
138     // pointer rather than a more elaborate vector-like data structure.
139     // This also avoids the need for a non-trivial destructor.
140     const SCEV *const *Operands;
141     size_t NumOperands;
142
143     SCEVNAryExpr(const FoldingSetNodeIDRef ID,
144                  enum SCEVTypes T, const SCEV *const *O, size_t N)
145       : SCEV(ID, T), Operands(O), NumOperands(N) {}
146
147   public:
148     size_t getNumOperands() const { return NumOperands; }
149     const SCEV *getOperand(unsigned i) const {
150       assert(i < NumOperands && "Operand index out of range!");
151       return Operands[i];
152     }
153
154     typedef const SCEV *const *op_iterator;
155     typedef iterator_range<op_iterator> op_range;
156     op_iterator op_begin() const { return Operands; }
157     op_iterator op_end() const { return Operands + NumOperands; }
158     op_range operands() const {
159       return make_range(op_begin(), op_end());
160     }
161
162     Type *getType() const { return getOperand(0)->getType(); }
163
164     NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const {
165       return (NoWrapFlags)(SubclassData & Mask);
166     }
167
168     /// Methods for support type inquiry through isa, cast, and dyn_cast:
169     static inline bool classof(const SCEV *S) {
170       return S->getSCEVType() == scAddExpr ||
171              S->getSCEVType() == scMulExpr ||
172              S->getSCEVType() == scSMaxExpr ||
173              S->getSCEVType() == scUMaxExpr ||
174              S->getSCEVType() == scAddRecExpr;
175     }
176   };
177
178   //===--------------------------------------------------------------------===//
179   /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
180   /// operators.
181   ///
182   class SCEVCommutativeExpr : public SCEVNAryExpr {
183   protected:
184     SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
185                         enum SCEVTypes T, const SCEV *const *O, size_t N)
186       : SCEVNAryExpr(ID, T, O, N) {}
187
188   public:
189     /// Methods for support type inquiry through isa, cast, and dyn_cast:
190     static inline bool classof(const SCEV *S) {
191       return S->getSCEVType() == scAddExpr ||
192              S->getSCEVType() == scMulExpr ||
193              S->getSCEVType() == scSMaxExpr ||
194              S->getSCEVType() == scUMaxExpr;
195     }
196
197     /// Set flags for a non-recurrence without clearing previously set flags.
198     void setNoWrapFlags(NoWrapFlags Flags) {
199       SubclassData |= Flags;
200     }
201   };
202
203
204   //===--------------------------------------------------------------------===//
205   /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
206   ///
207   class SCEVAddExpr : public SCEVCommutativeExpr {
208     friend class ScalarEvolution;
209
210     SCEVAddExpr(const FoldingSetNodeIDRef ID,
211                 const SCEV *const *O, size_t N)
212       : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
213     }
214
215   public:
216     Type *getType() const {
217       // Use the type of the last operand, which is likely to be a pointer
218       // type, if there is one. This doesn't usually matter, but it can help
219       // reduce casts when the expressions are expanded.
220       return getOperand(getNumOperands() - 1)->getType();
221     }
222
223     /// Methods for support type inquiry through isa, cast, and dyn_cast:
224     static inline bool classof(const SCEV *S) {
225       return S->getSCEVType() == scAddExpr;
226     }
227   };
228
229   //===--------------------------------------------------------------------===//
230   /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
231   ///
232   class SCEVMulExpr : public SCEVCommutativeExpr {
233     friend class ScalarEvolution;
234
235     SCEVMulExpr(const FoldingSetNodeIDRef ID,
236                 const SCEV *const *O, size_t N)
237       : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
238     }
239
240   public:
241     /// Methods for support type inquiry through isa, cast, and dyn_cast:
242     static inline bool classof(const SCEV *S) {
243       return S->getSCEVType() == scMulExpr;
244     }
245   };
246
247
248   //===--------------------------------------------------------------------===//
249   /// SCEVUDivExpr - This class represents a binary unsigned division operation.
250   ///
251   class SCEVUDivExpr : public SCEV {
252     friend class ScalarEvolution;
253
254     const SCEV *LHS;
255     const SCEV *RHS;
256     SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
257       : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
258
259   public:
260     const SCEV *getLHS() const { return LHS; }
261     const SCEV *getRHS() const { return RHS; }
262
263     Type *getType() const {
264       // In most cases the types of LHS and RHS will be the same, but in some
265       // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
266       // depend on the type for correctness, but handling types carefully can
267       // avoid extra casts in the SCEVExpander. The LHS is more likely to be
268       // a pointer type than the RHS, so use the RHS' type here.
269       return getRHS()->getType();
270     }
271
272     /// Methods for support type inquiry through isa, cast, and dyn_cast:
273     static inline bool classof(const SCEV *S) {
274       return S->getSCEVType() == scUDivExpr;
275     }
276   };
277
278
279   //===--------------------------------------------------------------------===//
280   /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
281   /// count of the specified loop.  This is the primary focus of the
282   /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
283   /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
284   /// created and analyzed.
285   ///
286   /// All operands of an AddRec are required to be loop invariant.
287   ///
288   class SCEVAddRecExpr : public SCEVNAryExpr {
289     friend class ScalarEvolution;
290
291     const Loop *L;
292
293     SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
294                    const SCEV *const *O, size_t N, const Loop *l)
295       : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {}
296
297   public:
298     const SCEV *getStart() const { return Operands[0]; }
299     const Loop *getLoop() const { return L; }
300
301     /// getStepRecurrence - This method constructs and returns the recurrence
302     /// indicating how much this expression steps by.  If this is a polynomial
303     /// of degree N, it returns a chrec of degree N-1.
304     /// We cannot determine whether the step recurrence has self-wraparound.
305     const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
306       if (isAffine()) return getOperand(1);
307       return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
308                                                            op_end()),
309                               getLoop(), FlagAnyWrap);
310     }
311
312     /// isAffine - Return true if this is an affine AddRec (i.e., it represents
313     /// an expressions A+B*x where A and B are loop invariant values.
314     bool isAffine() const {
315       // We know that the start value is invariant.  This expression is thus
316       // affine iff the step is also invariant.
317       return getNumOperands() == 2;
318     }
319
320     /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
321     /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
322     /// invariant values.  This corresponds to an addrec of the form {L,+,M,+,N}
323     bool isQuadratic() const {
324       return getNumOperands() == 3;
325     }
326
327     /// Set flags for a recurrence without clearing any previously set flags.
328     /// For AddRec, either NUW or NSW implies NW. Keep track of this fact here
329     /// to make it easier to propagate flags.
330     void setNoWrapFlags(NoWrapFlags Flags) {
331       if (Flags & (FlagNUW | FlagNSW))
332         Flags = ScalarEvolution::setFlags(Flags, FlagNW);
333       SubclassData |= Flags;
334     }
335
336     /// evaluateAtIteration - Return the value of this chain of recurrences at
337     /// the specified iteration number.
338     const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
339
340     /// getNumIterationsInRange - Return the number of iterations of this loop
341     /// that produce values in the specified constant range.  Another way of
342     /// looking at this is that it returns the first iteration number where the
343     /// value is not in the condition, thus computing the exit count.  If the
344     /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
345     /// returned.
346     const SCEV *getNumIterationsInRange(ConstantRange Range,
347                                        ScalarEvolution &SE) const;
348
349     /// getPostIncExpr - Return an expression representing the value of
350     /// this expression one iteration of the loop ahead.
351     const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
352       return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
353     }
354
355     /// Methods for support type inquiry through isa, cast, and dyn_cast:
356     static inline bool classof(const SCEV *S) {
357       return S->getSCEVType() == scAddRecExpr;
358     }
359
360     /// Collect parametric terms occurring in step expressions.
361     void collectParametricTerms(ScalarEvolution &SE,
362                                 SmallVectorImpl<const SCEV *> &Terms) const;
363
364     /// Return in Subscripts the access functions for each dimension in Sizes.
365     const SCEV *
366     computeAccessFunctions(ScalarEvolution &SE,
367                            SmallVectorImpl<const SCEV *> &Subscripts,
368                            SmallVectorImpl<const SCEV *> &Sizes) const;
369
370     /// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
371     /// subscripts and sizes of an array access. Returns the remainder of the
372     /// delinearization that is the offset start of the array.
373     ///
374     /// The delinearization is a 3 step process: the first two steps compute the
375     /// sizes of each subscript and the third step computes the access functions
376     /// for the delinearized array:
377     ///
378     /// 1. Find the terms in the step functions
379     /// 2. Compute the array size
380     /// 3. Compute the access function: divide the SCEV by the array size
381     ///    starting with the innermost dimensions found in step 2. The Quotient
382     ///    is the SCEV to be divided in the next step of the recursion. The
383     ///    Remainder is the subscript of the innermost dimension. Loop over all
384     ///    array dimensions computed in step 2.
385     ///
386     /// To compute a uniform array size for several memory accesses to the same
387     /// object, one can collect in step 1 all the step terms for all the memory
388     /// accesses, and compute in step 2 a unique array shape. This guarantees
389     /// that the array shape will be the same across all memory accesses.
390     ///
391     /// FIXME: We could derive the result of steps 1 and 2 from a description of
392     /// the array shape given in metadata.
393     ///
394     /// Example:
395     ///
396     /// A[][n][m]
397     ///
398     /// for i
399     ///   for j
400     ///     for k
401     ///       A[j+k][2i][5i] =
402     ///
403     /// The initial SCEV:
404     ///
405     /// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k]
406     ///
407     /// 1. Find the different terms in the step functions:
408     /// -> [2*m, 5, n*m, n*m]
409     ///
410     /// 2. Compute the array size: sort and unique them
411     /// -> [n*m, 2*m, 5]
412     /// find the GCD of all the terms = 1
413     /// divide by the GCD and erase constant terms
414     /// -> [n*m, 2*m]
415     /// GCD = m
416     /// divide by GCD -> [n, 2]
417     /// remove constant terms
418     /// -> [n]
419     /// size of the array is A[unknown][n][m]
420     ///
421     /// 3. Compute the access function
422     /// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m
423     /// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k
424     /// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k
425     /// The remainder is the subscript of the innermost array dimension: [5i].
426     ///
427     /// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n
428     /// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k
429     /// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k
430     /// The Remainder is the subscript of the next array dimension: [2i].
431     ///
432     /// The subscript of the outermost dimension is the Quotient: [j+k].
433     ///
434     /// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
435     const SCEV *delinearize(ScalarEvolution &SE,
436                             SmallVectorImpl<const SCEV *> &Subscripts,
437                             SmallVectorImpl<const SCEV *> &Sizes,
438                             const SCEV *ElementSize) const;
439   };
440
441   //===--------------------------------------------------------------------===//
442   /// SCEVSMaxExpr - This class represents a signed maximum selection.
443   ///
444   class SCEVSMaxExpr : public SCEVCommutativeExpr {
445     friend class ScalarEvolution;
446
447     SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
448                  const SCEV *const *O, size_t N)
449       : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
450       // Max never overflows.
451       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
452     }
453
454   public:
455     /// Methods for support type inquiry through isa, cast, and dyn_cast:
456     static inline bool classof(const SCEV *S) {
457       return S->getSCEVType() == scSMaxExpr;
458     }
459   };
460
461
462   //===--------------------------------------------------------------------===//
463   /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
464   ///
465   class SCEVUMaxExpr : public SCEVCommutativeExpr {
466     friend class ScalarEvolution;
467
468     SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
469                  const SCEV *const *O, size_t N)
470       : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
471       // Max never overflows.
472       setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
473     }
474
475   public:
476     /// Methods for support type inquiry through isa, cast, and dyn_cast:
477     static inline bool classof(const SCEV *S) {
478       return S->getSCEVType() == scUMaxExpr;
479     }
480   };
481
482   //===--------------------------------------------------------------------===//
483   /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
484   /// value, and only represent it as its LLVM Value.  This is the "bottom"
485   /// value for the analysis.
486   ///
487   class SCEVUnknown : public SCEV, private CallbackVH {
488     friend class ScalarEvolution;
489
490     // Implement CallbackVH.
491     void deleted() override;
492     void allUsesReplacedWith(Value *New) override;
493
494     /// SE - The parent ScalarEvolution value. This is used to update
495     /// the parent's maps when the value associated with a SCEVUnknown
496     /// is deleted or RAUW'd.
497     ScalarEvolution *SE;
498
499     /// Next - The next pointer in the linked list of all
500     /// SCEVUnknown instances owned by a ScalarEvolution.
501     SCEVUnknown *Next;
502
503     SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
504                 ScalarEvolution *se, SCEVUnknown *next) :
505       SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
506
507   public:
508     Value *getValue() const { return getValPtr(); }
509
510     /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
511     /// constant representing a type size, alignment, or field offset in
512     /// a target-independent manner, and hasn't happened to have been
513     /// folded with other operations into something unrecognizable. This
514     /// is mainly only useful for pretty-printing and other situations
515     /// where it isn't absolutely required for these to succeed.
516     bool isSizeOf(Type *&AllocTy) const;
517     bool isAlignOf(Type *&AllocTy) const;
518     bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
519
520     Type *getType() const { return getValPtr()->getType(); }
521
522     /// Methods for support type inquiry through isa, cast, and dyn_cast:
523     static inline bool classof(const SCEV *S) {
524       return S->getSCEVType() == scUnknown;
525     }
526   };
527
528   /// SCEVVisitor - This class defines a simple visitor class that may be used
529   /// for various SCEV analysis purposes.
530   template<typename SC, typename RetVal=void>
531   struct SCEVVisitor {
532     RetVal visit(const SCEV *S) {
533       switch (S->getSCEVType()) {
534       case scConstant:
535         return ((SC*)this)->visitConstant((const SCEVConstant*)S);
536       case scTruncate:
537         return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
538       case scZeroExtend:
539         return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
540       case scSignExtend:
541         return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
542       case scAddExpr:
543         return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
544       case scMulExpr:
545         return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
546       case scUDivExpr:
547         return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
548       case scAddRecExpr:
549         return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
550       case scSMaxExpr:
551         return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
552       case scUMaxExpr:
553         return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
554       case scUnknown:
555         return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
556       case scCouldNotCompute:
557         return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
558       default:
559         llvm_unreachable("Unknown SCEV type!");
560       }
561     }
562
563     RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
564       llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
565     }
566   };
567
568   /// Visit all nodes in the expression tree using worklist traversal.
569   ///
570   /// Visitor implements:
571   ///   // return true to follow this node.
572   ///   bool follow(const SCEV *S);
573   ///   // return true to terminate the search.
574   ///   bool isDone();
575   template<typename SV>
576   class SCEVTraversal {
577     SV &Visitor;
578     SmallVector<const SCEV *, 8> Worklist;
579     SmallPtrSet<const SCEV *, 8> Visited;
580
581     void push(const SCEV *S) {
582       if (Visited.insert(S) && Visitor.follow(S))
583         Worklist.push_back(S);
584     }
585   public:
586     SCEVTraversal(SV& V): Visitor(V) {}
587
588     void visitAll(const SCEV *Root) {
589       push(Root);
590       while (!Worklist.empty() && !Visitor.isDone()) {
591         const SCEV *S = Worklist.pop_back_val();
592
593         switch (S->getSCEVType()) {
594         case scConstant:
595         case scUnknown:
596           break;
597         case scTruncate:
598         case scZeroExtend:
599         case scSignExtend:
600           push(cast<SCEVCastExpr>(S)->getOperand());
601           break;
602         case scAddExpr:
603         case scMulExpr:
604         case scSMaxExpr:
605         case scUMaxExpr:
606         case scAddRecExpr: {
607           const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
608           for (SCEVNAryExpr::op_iterator I = NAry->op_begin(),
609                  E = NAry->op_end(); I != E; ++I) {
610             push(*I);
611           }
612           break;
613         }
614         case scUDivExpr: {
615           const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
616           push(UDiv->getLHS());
617           push(UDiv->getRHS());
618           break;
619         }
620         case scCouldNotCompute:
621           llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
622         default:
623           llvm_unreachable("Unknown SCEV kind!");
624         }
625       }
626     }
627   };
628
629   /// Use SCEVTraversal to visit all nodes in the givien expression tree.
630   template<typename SV>
631   void visitAll(const SCEV *Root, SV& Visitor) {
632     SCEVTraversal<SV> T(Visitor);
633     T.visitAll(Root);
634   }
635
636   typedef DenseMap<const Value*, Value*> ValueToValueMap;
637
638   /// The SCEVParameterRewriter takes a scalar evolution expression and updates
639   /// the SCEVUnknown components following the Map (Value -> Value).
640   struct SCEVParameterRewriter
641     : public SCEVVisitor<SCEVParameterRewriter, const SCEV*> {
642   public:
643     static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
644                                ValueToValueMap &Map,
645                                bool InterpretConsts = false) {
646       SCEVParameterRewriter Rewriter(SE, Map, InterpretConsts);
647       return Rewriter.visit(Scev);
648     }
649
650     SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M, bool C)
651       : SE(S), Map(M), InterpretConsts(C) {}
652
653     const SCEV *visitConstant(const SCEVConstant *Constant) {
654       return Constant;
655     }
656
657     const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
658       const SCEV *Operand = visit(Expr->getOperand());
659       return SE.getTruncateExpr(Operand, Expr->getType());
660     }
661
662     const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
663       const SCEV *Operand = visit(Expr->getOperand());
664       return SE.getZeroExtendExpr(Operand, Expr->getType());
665     }
666
667     const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
668       const SCEV *Operand = visit(Expr->getOperand());
669       return SE.getSignExtendExpr(Operand, Expr->getType());
670     }
671
672     const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
673       SmallVector<const SCEV *, 2> Operands;
674       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
675         Operands.push_back(visit(Expr->getOperand(i)));
676       return SE.getAddExpr(Operands);
677     }
678
679     const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
680       SmallVector<const SCEV *, 2> Operands;
681       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
682         Operands.push_back(visit(Expr->getOperand(i)));
683       return SE.getMulExpr(Operands);
684     }
685
686     const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
687       return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
688     }
689
690     const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
691       SmallVector<const SCEV *, 2> Operands;
692       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
693         Operands.push_back(visit(Expr->getOperand(i)));
694       return SE.getAddRecExpr(Operands, Expr->getLoop(),
695                               Expr->getNoWrapFlags());
696     }
697
698     const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
699       SmallVector<const SCEV *, 2> Operands;
700       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
701         Operands.push_back(visit(Expr->getOperand(i)));
702       return SE.getSMaxExpr(Operands);
703     }
704
705     const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
706       SmallVector<const SCEV *, 2> Operands;
707       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
708         Operands.push_back(visit(Expr->getOperand(i)));
709       return SE.getUMaxExpr(Operands);
710     }
711
712     const SCEV *visitUnknown(const SCEVUnknown *Expr) {
713       Value *V = Expr->getValue();
714       if (Map.count(V)) {
715         Value *NV = Map[V];
716         if (InterpretConsts && isa<ConstantInt>(NV))
717           return SE.getConstant(cast<ConstantInt>(NV));
718         return SE.getUnknown(NV);
719       }
720       return Expr;
721     }
722
723     const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
724       return Expr;
725     }
726
727   private:
728     ScalarEvolution &SE;
729     ValueToValueMap &Map;
730     bool InterpretConsts;
731   };
732
733   typedef DenseMap<const Loop*, const SCEV*> LoopToScevMapT;
734
735   /// The SCEVApplyRewriter takes a scalar evolution expression and applies
736   /// the Map (Loop -> SCEV) to all AddRecExprs.
737   struct SCEVApplyRewriter
738     : public SCEVVisitor<SCEVApplyRewriter, const SCEV*> {
739   public:
740     static const SCEV *rewrite(const SCEV *Scev, LoopToScevMapT &Map,
741                                ScalarEvolution &SE) {
742       SCEVApplyRewriter Rewriter(SE, Map);
743       return Rewriter.visit(Scev);
744     }
745
746     SCEVApplyRewriter(ScalarEvolution &S, LoopToScevMapT &M)
747       : SE(S), Map(M) {}
748
749     const SCEV *visitConstant(const SCEVConstant *Constant) {
750       return Constant;
751     }
752
753     const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
754       const SCEV *Operand = visit(Expr->getOperand());
755       return SE.getTruncateExpr(Operand, Expr->getType());
756     }
757
758     const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
759       const SCEV *Operand = visit(Expr->getOperand());
760       return SE.getZeroExtendExpr(Operand, Expr->getType());
761     }
762
763     const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
764       const SCEV *Operand = visit(Expr->getOperand());
765       return SE.getSignExtendExpr(Operand, Expr->getType());
766     }
767
768     const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
769       SmallVector<const SCEV *, 2> Operands;
770       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
771         Operands.push_back(visit(Expr->getOperand(i)));
772       return SE.getAddExpr(Operands);
773     }
774
775     const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
776       SmallVector<const SCEV *, 2> Operands;
777       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
778         Operands.push_back(visit(Expr->getOperand(i)));
779       return SE.getMulExpr(Operands);
780     }
781
782     const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
783       return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
784     }
785
786     const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
787       SmallVector<const SCEV *, 2> Operands;
788       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
789         Operands.push_back(visit(Expr->getOperand(i)));
790
791       const Loop *L = Expr->getLoop();
792       const SCEV *Res = SE.getAddRecExpr(Operands, L, Expr->getNoWrapFlags());
793
794       if (0 == Map.count(L))
795         return Res;
796
797       const SCEVAddRecExpr *Rec = (const SCEVAddRecExpr *) Res;
798       return Rec->evaluateAtIteration(Map[L], SE);
799     }
800
801     const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
802       SmallVector<const SCEV *, 2> Operands;
803       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
804         Operands.push_back(visit(Expr->getOperand(i)));
805       return SE.getSMaxExpr(Operands);
806     }
807
808     const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
809       SmallVector<const SCEV *, 2> Operands;
810       for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
811         Operands.push_back(visit(Expr->getOperand(i)));
812       return SE.getUMaxExpr(Operands);
813     }
814
815     const SCEV *visitUnknown(const SCEVUnknown *Expr) {
816       return Expr;
817     }
818
819     const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
820       return Expr;
821     }
822
823   private:
824     ScalarEvolution &SE;
825     LoopToScevMapT &Map;
826   };
827
828 /// Applies the Map (Loop -> SCEV) to the given Scev.
829 static inline const SCEV *apply(const SCEV *Scev, LoopToScevMapT &Map,
830                                 ScalarEvolution &SE) {
831   return SCEVApplyRewriter::rewrite(Scev, Map, SE);
832 }
833
834 }
835
836 #endif