1 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the classes used to represent and build scalar expressions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
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"
28 // These should be ordered in terms of increasing complexity to make the
30 scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
31 scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr,
32 scUnknown, scCouldNotCompute
35 //===--------------------------------------------------------------------===//
36 /// SCEVConstant - This class represents a constant integer value.
38 class SCEVConstant : public SCEV {
39 friend class ScalarEvolution;
42 SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
43 SCEV(ID, scConstant), V(v) {}
45 ConstantInt *getValue() const { return V; }
47 Type *getType() const { return V->getType(); }
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;
55 //===--------------------------------------------------------------------===//
56 /// SCEVCastExpr - This is the base class for unary cast operator classes.
58 class SCEVCastExpr : public SCEV {
63 SCEVCastExpr(const FoldingSetNodeIDRef ID,
64 unsigned SCEVTy, const SCEV *op, Type *ty);
67 const SCEV *getOperand() const { return Op; }
68 Type *getType() const { return Ty; }
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;
78 //===--------------------------------------------------------------------===//
79 /// SCEVTruncateExpr - This class represents a truncation of an integer value
80 /// to a smaller integer value.
82 class SCEVTruncateExpr : public SCEVCastExpr {
83 friend class ScalarEvolution;
85 SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
86 const SCEV *op, Type *ty);
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;
95 //===--------------------------------------------------------------------===//
96 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
97 /// integer value to a larger integer value.
99 class SCEVZeroExtendExpr : public SCEVCastExpr {
100 friend class ScalarEvolution;
102 SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
103 const SCEV *op, Type *ty);
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;
112 //===--------------------------------------------------------------------===//
113 /// SCEVSignExtendExpr - This class represents a sign extension of a small
114 /// integer value to a larger integer value.
116 class SCEVSignExtendExpr : public SCEVCastExpr {
117 friend class ScalarEvolution;
119 SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
120 const SCEV *op, Type *ty);
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;
130 //===--------------------------------------------------------------------===//
131 /// SCEVNAryExpr - This node is a base class providing common
132 /// functionality for n'ary operators.
134 class SCEVNAryExpr : public SCEV {
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;
143 SCEVNAryExpr(const FoldingSetNodeIDRef ID,
144 enum SCEVTypes T, const SCEV *const *O, size_t N)
145 : SCEV(ID, T), Operands(O), NumOperands(N) {}
148 size_t getNumOperands() const { return NumOperands; }
149 const SCEV *getOperand(unsigned i) const {
150 assert(i < NumOperands && "Operand index out of range!");
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());
162 Type *getType() const { return getOperand(0)->getType(); }
164 NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const {
165 return (NoWrapFlags)(SubclassData & Mask);
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;
178 //===--------------------------------------------------------------------===//
179 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
182 class SCEVCommutativeExpr : public SCEVNAryExpr {
184 SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
185 enum SCEVTypes T, const SCEV *const *O, size_t N)
186 : SCEVNAryExpr(ID, T, O, N) {}
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;
197 /// Set flags for a non-recurrence without clearing previously set flags.
198 void setNoWrapFlags(NoWrapFlags Flags) {
199 SubclassData |= Flags;
204 //===--------------------------------------------------------------------===//
205 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
207 class SCEVAddExpr : public SCEVCommutativeExpr {
208 friend class ScalarEvolution;
210 SCEVAddExpr(const FoldingSetNodeIDRef ID,
211 const SCEV *const *O, size_t N)
212 : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
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();
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;
229 //===--------------------------------------------------------------------===//
230 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
232 class SCEVMulExpr : public SCEVCommutativeExpr {
233 friend class ScalarEvolution;
235 SCEVMulExpr(const FoldingSetNodeIDRef ID,
236 const SCEV *const *O, size_t N)
237 : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
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;
248 //===--------------------------------------------------------------------===//
249 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
251 class SCEVUDivExpr : public SCEV {
252 friend class ScalarEvolution;
256 SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
257 : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
260 const SCEV *getLHS() const { return LHS; }
261 const SCEV *getRHS() const { return RHS; }
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();
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;
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.
286 /// All operands of an AddRec are required to be loop invariant.
288 class SCEVAddRecExpr : public SCEVNAryExpr {
289 friend class ScalarEvolution;
293 SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
294 const SCEV *const *O, size_t N, const Loop *l)
295 : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {}
298 const SCEV *getStart() const { return Operands[0]; }
299 const Loop *getLoop() const { return L; }
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,
309 getLoop(), FlagAnyWrap);
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;
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;
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;
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;
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
346 const SCEV *getNumIterationsInRange(ConstantRange Range,
347 ScalarEvolution &SE) const;
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)));
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;
360 /// Collect parametric terms occurring in step expressions.
361 void collectParametricTerms(ScalarEvolution &SE,
362 SmallVectorImpl<const SCEV *> &Terms) const;
364 /// Compute the array dimensions Sizes from the set of Terms extracted from
365 /// the memory access function of this SCEVAddRecExpr.
366 void findArrayDimensions(ScalarEvolution &SE,
367 SmallVectorImpl<const SCEV *> &Terms,
368 SmallVectorImpl<const SCEV *> &Sizes) const;
370 /// Return in Subscripts the access functions for each dimension in Sizes.
372 computeAccessFunctions(ScalarEvolution &SE,
373 SmallVectorImpl<const SCEV *> &Subscripts,
374 SmallVectorImpl<const SCEV *> &Sizes) const;
376 /// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
377 /// subscripts and sizes of an array access. Returns the remainder of the
378 /// delinearization that is the offset start of the array.
380 /// The delinearization is a 3 step process: the first two steps compute the
381 /// sizes of each subscript and the third step computes the access functions
382 /// for the delinearized array:
384 /// 1. Find the terms in the step functions
385 /// 2. Compute the array size
386 /// 3. Compute the access function: divide the SCEV by the array size
387 /// starting with the innermost dimensions found in step 2. The Quotient
388 /// is the SCEV to be divided in the next step of the recursion. The
389 /// Remainder is the subscript of the innermost dimension. Loop over all
390 /// array dimensions computed in step 2.
392 /// To compute a uniform array size for several memory accesses to the same
393 /// object, one can collect in step 1 all the step terms for all the memory
394 /// accesses, and compute in step 2 a unique array shape. This guarantees
395 /// that the array shape will be the same across all memory accesses.
397 /// FIXME: We could derive the result of steps 1 and 2 from a description of
398 /// the array shape given in metadata.
409 /// The initial SCEV:
411 /// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k]
413 /// 1. Find the different terms in the step functions:
414 /// -> [2*m, 5, n*m, n*m]
416 /// 2. Compute the array size: sort and unique them
418 /// find the GCD of all the terms = 1
419 /// divide by the GCD and erase constant terms
422 /// divide by GCD -> [n, 2]
423 /// remove constant terms
425 /// size of the array is A[unknown][n][m]
427 /// 3. Compute the access function
428 /// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m
429 /// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k
430 /// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k
431 /// The remainder is the subscript of the innermost array dimension: [5i].
433 /// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n
434 /// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k
435 /// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k
436 /// The Remainder is the subscript of the next array dimension: [2i].
438 /// The subscript of the outermost dimension is the Quotient: [j+k].
440 /// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
441 const SCEV *delinearize(ScalarEvolution &SE,
442 SmallVectorImpl<const SCEV *> &Subscripts,
443 SmallVectorImpl<const SCEV *> &Sizes) const;
446 //===--------------------------------------------------------------------===//
447 /// SCEVSMaxExpr - This class represents a signed maximum selection.
449 class SCEVSMaxExpr : public SCEVCommutativeExpr {
450 friend class ScalarEvolution;
452 SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
453 const SCEV *const *O, size_t N)
454 : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
455 // Max never overflows.
456 setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
460 /// Methods for support type inquiry through isa, cast, and dyn_cast:
461 static inline bool classof(const SCEV *S) {
462 return S->getSCEVType() == scSMaxExpr;
467 //===--------------------------------------------------------------------===//
468 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
470 class SCEVUMaxExpr : public SCEVCommutativeExpr {
471 friend class ScalarEvolution;
473 SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
474 const SCEV *const *O, size_t N)
475 : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
476 // Max never overflows.
477 setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
481 /// Methods for support type inquiry through isa, cast, and dyn_cast:
482 static inline bool classof(const SCEV *S) {
483 return S->getSCEVType() == scUMaxExpr;
487 //===--------------------------------------------------------------------===//
488 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
489 /// value, and only represent it as its LLVM Value. This is the "bottom"
490 /// value for the analysis.
492 class SCEVUnknown : public SCEV, private CallbackVH {
493 friend class ScalarEvolution;
495 // Implement CallbackVH.
496 void deleted() override;
497 void allUsesReplacedWith(Value *New) override;
499 /// SE - The parent ScalarEvolution value. This is used to update
500 /// the parent's maps when the value associated with a SCEVUnknown
501 /// is deleted or RAUW'd.
504 /// Next - The next pointer in the linked list of all
505 /// SCEVUnknown instances owned by a ScalarEvolution.
508 SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
509 ScalarEvolution *se, SCEVUnknown *next) :
510 SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
513 Value *getValue() const { return getValPtr(); }
515 /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
516 /// constant representing a type size, alignment, or field offset in
517 /// a target-independent manner, and hasn't happened to have been
518 /// folded with other operations into something unrecognizable. This
519 /// is mainly only useful for pretty-printing and other situations
520 /// where it isn't absolutely required for these to succeed.
521 bool isSizeOf(Type *&AllocTy) const;
522 bool isAlignOf(Type *&AllocTy) const;
523 bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
525 Type *getType() const { return getValPtr()->getType(); }
527 /// Methods for support type inquiry through isa, cast, and dyn_cast:
528 static inline bool classof(const SCEV *S) {
529 return S->getSCEVType() == scUnknown;
533 /// SCEVVisitor - This class defines a simple visitor class that may be used
534 /// for various SCEV analysis purposes.
535 template<typename SC, typename RetVal=void>
537 RetVal visit(const SCEV *S) {
538 switch (S->getSCEVType()) {
540 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
542 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
544 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
546 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
548 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
550 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
552 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
554 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
556 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
558 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
560 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
561 case scCouldNotCompute:
562 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
564 llvm_unreachable("Unknown SCEV type!");
568 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
569 llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
573 /// Visit all nodes in the expression tree using worklist traversal.
575 /// Visitor implements:
576 /// // return true to follow this node.
577 /// bool follow(const SCEV *S);
578 /// // return true to terminate the search.
580 template<typename SV>
581 class SCEVTraversal {
583 SmallVector<const SCEV *, 8> Worklist;
584 SmallPtrSet<const SCEV *, 8> Visited;
586 void push(const SCEV *S) {
587 if (Visited.insert(S) && Visitor.follow(S))
588 Worklist.push_back(S);
591 SCEVTraversal(SV& V): Visitor(V) {}
593 void visitAll(const SCEV *Root) {
595 while (!Worklist.empty() && !Visitor.isDone()) {
596 const SCEV *S = Worklist.pop_back_val();
598 switch (S->getSCEVType()) {
605 push(cast<SCEVCastExpr>(S)->getOperand());
612 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
613 for (SCEVNAryExpr::op_iterator I = NAry->op_begin(),
614 E = NAry->op_end(); I != E; ++I) {
620 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
621 push(UDiv->getLHS());
622 push(UDiv->getRHS());
625 case scCouldNotCompute:
626 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
628 llvm_unreachable("Unknown SCEV kind!");
634 /// Use SCEVTraversal to visit all nodes in the givien expression tree.
635 template<typename SV>
636 void visitAll(const SCEV *Root, SV& Visitor) {
637 SCEVTraversal<SV> T(Visitor);
641 typedef DenseMap<const Value*, Value*> ValueToValueMap;
643 /// The SCEVParameterRewriter takes a scalar evolution expression and updates
644 /// the SCEVUnknown components following the Map (Value -> Value).
645 struct SCEVParameterRewriter
646 : public SCEVVisitor<SCEVParameterRewriter, const SCEV*> {
648 static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
649 ValueToValueMap &Map,
650 bool InterpretConsts = false) {
651 SCEVParameterRewriter Rewriter(SE, Map, InterpretConsts);
652 return Rewriter.visit(Scev);
655 SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M, bool C)
656 : SE(S), Map(M), InterpretConsts(C) {}
658 const SCEV *visitConstant(const SCEVConstant *Constant) {
662 const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
663 const SCEV *Operand = visit(Expr->getOperand());
664 return SE.getTruncateExpr(Operand, Expr->getType());
667 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
668 const SCEV *Operand = visit(Expr->getOperand());
669 return SE.getZeroExtendExpr(Operand, Expr->getType());
672 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
673 const SCEV *Operand = visit(Expr->getOperand());
674 return SE.getSignExtendExpr(Operand, Expr->getType());
677 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
678 SmallVector<const SCEV *, 2> Operands;
679 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
680 Operands.push_back(visit(Expr->getOperand(i)));
681 return SE.getAddExpr(Operands);
684 const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
685 SmallVector<const SCEV *, 2> Operands;
686 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
687 Operands.push_back(visit(Expr->getOperand(i)));
688 return SE.getMulExpr(Operands);
691 const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
692 return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
695 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
696 SmallVector<const SCEV *, 2> Operands;
697 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
698 Operands.push_back(visit(Expr->getOperand(i)));
699 return SE.getAddRecExpr(Operands, Expr->getLoop(),
700 Expr->getNoWrapFlags());
703 const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
704 SmallVector<const SCEV *, 2> Operands;
705 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
706 Operands.push_back(visit(Expr->getOperand(i)));
707 return SE.getSMaxExpr(Operands);
710 const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
711 SmallVector<const SCEV *, 2> Operands;
712 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
713 Operands.push_back(visit(Expr->getOperand(i)));
714 return SE.getUMaxExpr(Operands);
717 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
718 Value *V = Expr->getValue();
721 if (InterpretConsts && isa<ConstantInt>(NV))
722 return SE.getConstant(cast<ConstantInt>(NV));
723 return SE.getUnknown(NV);
728 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
734 ValueToValueMap ⤅
735 bool InterpretConsts;
738 typedef DenseMap<const Loop*, const SCEV*> LoopToScevMapT;
740 /// The SCEVApplyRewriter takes a scalar evolution expression and applies
741 /// the Map (Loop -> SCEV) to all AddRecExprs.
742 struct SCEVApplyRewriter
743 : public SCEVVisitor<SCEVApplyRewriter, const SCEV*> {
745 static const SCEV *rewrite(const SCEV *Scev, LoopToScevMapT &Map,
746 ScalarEvolution &SE) {
747 SCEVApplyRewriter Rewriter(SE, Map);
748 return Rewriter.visit(Scev);
751 SCEVApplyRewriter(ScalarEvolution &S, LoopToScevMapT &M)
754 const SCEV *visitConstant(const SCEVConstant *Constant) {
758 const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
759 const SCEV *Operand = visit(Expr->getOperand());
760 return SE.getTruncateExpr(Operand, Expr->getType());
763 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
764 const SCEV *Operand = visit(Expr->getOperand());
765 return SE.getZeroExtendExpr(Operand, Expr->getType());
768 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
769 const SCEV *Operand = visit(Expr->getOperand());
770 return SE.getSignExtendExpr(Operand, Expr->getType());
773 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
774 SmallVector<const SCEV *, 2> Operands;
775 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
776 Operands.push_back(visit(Expr->getOperand(i)));
777 return SE.getAddExpr(Operands);
780 const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
781 SmallVector<const SCEV *, 2> Operands;
782 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
783 Operands.push_back(visit(Expr->getOperand(i)));
784 return SE.getMulExpr(Operands);
787 const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
788 return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
791 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
792 SmallVector<const SCEV *, 2> Operands;
793 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
794 Operands.push_back(visit(Expr->getOperand(i)));
796 const Loop *L = Expr->getLoop();
797 const SCEV *Res = SE.getAddRecExpr(Operands, L, Expr->getNoWrapFlags());
799 if (0 == Map.count(L))
802 const SCEVAddRecExpr *Rec = (const SCEVAddRecExpr *) Res;
803 return Rec->evaluateAtIteration(Map[L], SE);
806 const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
807 SmallVector<const SCEV *, 2> Operands;
808 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
809 Operands.push_back(visit(Expr->getOperand(i)));
810 return SE.getSMaxExpr(Operands);
813 const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
814 SmallVector<const SCEV *, 2> Operands;
815 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
816 Operands.push_back(visit(Expr->getOperand(i)));
817 return SE.getUMaxExpr(Operands);
820 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
824 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
833 /// Applies the Map (Loop -> SCEV) to the given Scev.
834 static inline const SCEV *apply(const SCEV *Scev, LoopToScevMapT &Map,
835 ScalarEvolution &SE) {
836 return SCEVApplyRewriter::rewrite(Scev, Map, SE);