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_SCALAREVOLUTION_EXPRESSIONS_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPRESSIONS_H
17 #include "llvm/Analysis/ScalarEvolution.h"
18 #include "llvm/Support/ErrorHandling.h"
26 // These should be ordered in terms of increasing complexity to make the
28 scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
29 scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr,
30 scUnknown, scCouldNotCompute
33 //===--------------------------------------------------------------------===//
34 /// SCEVConstant - This class represents a constant integer value.
36 class SCEVConstant : public SCEV {
37 friend class ScalarEvolution;
40 SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
41 SCEV(ID, scConstant), V(v) {}
43 ConstantInt *getValue() const { return V; }
45 virtual bool isLoopInvariant(const Loop *L) const {
49 virtual bool hasComputableLoopEvolution(const Loop *L) const {
50 return false; // Not loop variant
53 virtual const Type *getType() const;
55 virtual bool hasOperand(const SCEV *) const {
59 bool dominates(BasicBlock *BB, DominatorTree *DT) const {
63 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
67 virtual void print(raw_ostream &OS) const;
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;
76 //===--------------------------------------------------------------------===//
77 /// SCEVCastExpr - This is the base class for unary cast operator classes.
79 class SCEVCastExpr : public SCEV {
84 SCEVCastExpr(const FoldingSetNodeIDRef ID,
85 unsigned SCEVTy, const SCEV *op, const Type *ty);
88 const SCEV *getOperand() const { return Op; }
89 virtual const Type *getType() const { return Ty; }
91 virtual bool isLoopInvariant(const Loop *L) const {
92 return Op->isLoopInvariant(L);
95 virtual bool hasComputableLoopEvolution(const Loop *L) const {
96 return Op->hasComputableLoopEvolution(L);
99 virtual bool hasOperand(const SCEV *O) const {
100 return Op == O || Op->hasOperand(O);
103 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
105 virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
107 /// Methods for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const SCEVCastExpr *S) { return true; }
109 static inline bool classof(const SCEV *S) {
110 return S->getSCEVType() == scTruncate ||
111 S->getSCEVType() == scZeroExtend ||
112 S->getSCEVType() == scSignExtend;
116 //===--------------------------------------------------------------------===//
117 /// SCEVTruncateExpr - This class represents a truncation of an integer value
118 /// to a smaller integer value.
120 class SCEVTruncateExpr : public SCEVCastExpr {
121 friend class ScalarEvolution;
123 SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
124 const SCEV *op, const Type *ty);
127 virtual void print(raw_ostream &OS) const;
129 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
130 // Not computable. A truncate of an addrec is always folded into
135 /// Methods for support type inquiry through isa, cast, and dyn_cast:
136 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
137 static inline bool classof(const SCEV *S) {
138 return S->getSCEVType() == scTruncate;
142 //===--------------------------------------------------------------------===//
143 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
144 /// integer value to a larger integer value.
146 class SCEVZeroExtendExpr : public SCEVCastExpr {
147 friend class ScalarEvolution;
149 SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
150 const SCEV *op, const Type *ty);
153 virtual void print(raw_ostream &OS) const;
155 /// Methods for support type inquiry through isa, cast, and dyn_cast:
156 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
157 static inline bool classof(const SCEV *S) {
158 return S->getSCEVType() == scZeroExtend;
162 //===--------------------------------------------------------------------===//
163 /// SCEVSignExtendExpr - This class represents a sign extension of a small
164 /// integer value to a larger integer value.
166 class SCEVSignExtendExpr : public SCEVCastExpr {
167 friend class ScalarEvolution;
169 SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
170 const SCEV *op, const Type *ty);
173 virtual void print(raw_ostream &OS) const;
175 /// Methods for support type inquiry through isa, cast, and dyn_cast:
176 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
177 static inline bool classof(const SCEV *S) {
178 return S->getSCEVType() == scSignExtend;
183 //===--------------------------------------------------------------------===//
184 /// SCEVNAryExpr - This node is a base class providing common
185 /// functionality for n'ary operators.
187 class SCEVNAryExpr : public SCEV {
189 // Since SCEVs are immutable, ScalarEvolution allocates operand
190 // arrays with its SCEVAllocator, so this class just needs a simple
191 // pointer rather than a more elaborate vector-like data structure.
192 // This also avoids the need for a non-trivial destructor.
193 const SCEV *const *Operands;
196 SCEVNAryExpr(const FoldingSetNodeIDRef ID,
197 enum SCEVTypes T, const SCEV *const *O, size_t N)
198 : SCEV(ID, T), Operands(O), NumOperands(N) {}
201 size_t getNumOperands() const { return NumOperands; }
202 const SCEV *getOperand(unsigned i) const {
203 assert(i < NumOperands && "Operand index out of range!");
207 typedef const SCEV *const *op_iterator;
208 op_iterator op_begin() const { return Operands; }
209 op_iterator op_end() const { return Operands + NumOperands; }
211 virtual bool isLoopInvariant(const Loop *L) const {
212 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
213 if (!getOperand(i)->isLoopInvariant(L)) return false;
217 // hasComputableLoopEvolution - N-ary expressions have computable loop
218 // evolutions iff they have at least one operand that varies with the loop,
219 // but that all varying operands are computable.
220 virtual bool hasComputableLoopEvolution(const Loop *L) const {
221 bool HasVarying = false;
222 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
223 if (!getOperand(i)->isLoopInvariant(L)) {
224 if (getOperand(i)->hasComputableLoopEvolution(L))
232 virtual bool hasOperand(const SCEV *O) const {
233 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
234 if (O == getOperand(i) || getOperand(i)->hasOperand(O))
239 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
241 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
243 virtual const Type *getType() const { return getOperand(0)->getType(); }
245 bool hasNoUnsignedWrap() const { return SubclassData & (1 << 0); }
246 void setHasNoUnsignedWrap(bool B) {
247 SubclassData = (SubclassData & ~(1 << 0)) | (B << 0);
249 bool hasNoSignedWrap() const { return SubclassData & (1 << 1); }
250 void setHasNoSignedWrap(bool B) {
251 SubclassData = (SubclassData & ~(1 << 1)) | (B << 1);
254 /// Methods for support type inquiry through isa, cast, and dyn_cast:
255 static inline bool classof(const SCEVNAryExpr *S) { return true; }
256 static inline bool classof(const SCEV *S) {
257 return S->getSCEVType() == scAddExpr ||
258 S->getSCEVType() == scMulExpr ||
259 S->getSCEVType() == scSMaxExpr ||
260 S->getSCEVType() == scUMaxExpr ||
261 S->getSCEVType() == scAddRecExpr;
265 //===--------------------------------------------------------------------===//
266 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
269 class SCEVCommutativeExpr : public SCEVNAryExpr {
271 SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
272 enum SCEVTypes T, const SCEV *const *O, size_t N)
273 : SCEVNAryExpr(ID, T, O, N) {}
276 virtual const char *getOperationStr() const = 0;
278 virtual void print(raw_ostream &OS) const;
280 /// Methods for support type inquiry through isa, cast, and dyn_cast:
281 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
282 static inline bool classof(const SCEV *S) {
283 return S->getSCEVType() == scAddExpr ||
284 S->getSCEVType() == scMulExpr ||
285 S->getSCEVType() == scSMaxExpr ||
286 S->getSCEVType() == scUMaxExpr;
291 //===--------------------------------------------------------------------===//
292 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
294 class SCEVAddExpr : public SCEVCommutativeExpr {
295 friend class ScalarEvolution;
297 SCEVAddExpr(const FoldingSetNodeIDRef ID,
298 const SCEV *const *O, size_t N)
299 : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
303 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
304 // Not computable. An add of an addrec is always folded into the addrec
305 // if the other operands are loop-variant or loop-computable.
309 virtual const char *getOperationStr() const { return " + "; }
311 virtual const Type *getType() const {
312 // Use the type of the last operand, which is likely to be a pointer
313 // type, if there is one. This doesn't usually matter, but it can help
314 // reduce casts when the expressions are expanded.
315 return getOperand(getNumOperands() - 1)->getType();
318 /// Methods for support type inquiry through isa, cast, and dyn_cast:
319 static inline bool classof(const SCEVAddExpr *S) { return true; }
320 static inline bool classof(const SCEV *S) {
321 return S->getSCEVType() == scAddExpr;
325 //===--------------------------------------------------------------------===//
326 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
328 class SCEVMulExpr : public SCEVCommutativeExpr {
329 friend class ScalarEvolution;
331 SCEVMulExpr(const FoldingSetNodeIDRef ID,
332 const SCEV *const *O, size_t N)
333 : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
337 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
338 // Not computable. A mul of an addrec is always folded into the addrec
339 // if the other operands are loop-variant or loop-computable.
343 virtual const char *getOperationStr() const { return " * "; }
345 /// Methods for support type inquiry through isa, cast, and dyn_cast:
346 static inline bool classof(const SCEVMulExpr *S) { return true; }
347 static inline bool classof(const SCEV *S) {
348 return S->getSCEVType() == scMulExpr;
353 //===--------------------------------------------------------------------===//
354 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
356 class SCEVUDivExpr : public SCEV {
357 friend class ScalarEvolution;
361 SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
362 : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
365 const SCEV *getLHS() const { return LHS; }
366 const SCEV *getRHS() const { return RHS; }
368 virtual bool isLoopInvariant(const Loop *L) const {
369 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
372 virtual bool hasComputableLoopEvolution(const Loop *L) const {
373 return LHS->hasComputableLoopEvolution(L) &&
374 RHS->hasComputableLoopEvolution(L);
377 virtual bool hasOperand(const SCEV *O) const {
378 return O == LHS || O == RHS || LHS->hasOperand(O) || RHS->hasOperand(O);
381 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
383 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
385 virtual const Type *getType() const;
387 void print(raw_ostream &OS) const;
389 /// Methods for support type inquiry through isa, cast, and dyn_cast:
390 static inline bool classof(const SCEVUDivExpr *S) { return true; }
391 static inline bool classof(const SCEV *S) {
392 return S->getSCEVType() == scUDivExpr;
397 //===--------------------------------------------------------------------===//
398 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
399 /// count of the specified loop. This is the primary focus of the
400 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
401 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
402 /// created and analyzed.
404 /// All operands of an AddRec are required to be loop invariant.
406 class SCEVAddRecExpr : public SCEVNAryExpr {
407 friend class ScalarEvolution;
411 SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
412 const SCEV *const *O, size_t N, const Loop *l)
413 : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {
414 for (size_t i = 0, e = NumOperands; i != e; ++i)
415 assert(Operands[i]->isLoopInvariant(l) &&
416 "Operands of AddRec must be loop-invariant!");
420 const SCEV *getStart() const { return Operands[0]; }
421 const Loop *getLoop() const { return L; }
423 /// getStepRecurrence - This method constructs and returns the recurrence
424 /// indicating how much this expression steps by. If this is a polynomial
425 /// of degree N, it returns a chrec of degree N-1.
426 const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
427 if (isAffine()) return getOperand(1);
428 return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
433 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
437 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
439 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
441 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
443 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
444 /// an expressions A+B*x where A and B are loop invariant values.
445 bool isAffine() const {
446 // We know that the start value is invariant. This expression is thus
447 // affine iff the step is also invariant.
448 return getNumOperands() == 2;
451 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
452 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
453 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
454 bool isQuadratic() const {
455 return getNumOperands() == 3;
458 /// evaluateAtIteration - Return the value of this chain of recurrences at
459 /// the specified iteration number.
460 const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
462 /// getNumIterationsInRange - Return the number of iterations of this loop
463 /// that produce values in the specified constant range. Another way of
464 /// looking at this is that it returns the first iteration number where the
465 /// value is not in the condition, thus computing the exit count. If the
466 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
468 const SCEV *getNumIterationsInRange(ConstantRange Range,
469 ScalarEvolution &SE) const;
471 /// getPostIncExpr - Return an expression representing the value of
472 /// this expression one iteration of the loop ahead.
473 const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
474 return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
477 virtual void print(raw_ostream &OS) const;
479 /// Methods for support type inquiry through isa, cast, and dyn_cast:
480 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
481 static inline bool classof(const SCEV *S) {
482 return S->getSCEVType() == scAddRecExpr;
487 //===--------------------------------------------------------------------===//
488 /// SCEVSMaxExpr - This class represents a signed maximum selection.
490 class SCEVSMaxExpr : public SCEVCommutativeExpr {
491 friend class ScalarEvolution;
493 SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
494 const SCEV *const *O, size_t N)
495 : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
496 // Max never overflows.
497 setHasNoUnsignedWrap(true);
498 setHasNoSignedWrap(true);
502 virtual const char *getOperationStr() const { return " smax "; }
504 /// Methods for support type inquiry through isa, cast, and dyn_cast:
505 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
506 static inline bool classof(const SCEV *S) {
507 return S->getSCEVType() == scSMaxExpr;
512 //===--------------------------------------------------------------------===//
513 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
515 class SCEVUMaxExpr : public SCEVCommutativeExpr {
516 friend class ScalarEvolution;
518 SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
519 const SCEV *const *O, size_t N)
520 : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
521 // Max never overflows.
522 setHasNoUnsignedWrap(true);
523 setHasNoSignedWrap(true);
527 virtual const char *getOperationStr() const { return " umax "; }
529 /// Methods for support type inquiry through isa, cast, and dyn_cast:
530 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
531 static inline bool classof(const SCEV *S) {
532 return S->getSCEVType() == scUMaxExpr;
536 //===--------------------------------------------------------------------===//
537 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
538 /// value, and only represent it as its LLVM Value. This is the "bottom"
539 /// value for the analysis.
541 class SCEVUnknown : public SCEV, private CallbackVH {
542 friend class ScalarEvolution;
544 // Implement CallbackVH.
545 virtual void deleted();
546 virtual void allUsesReplacedWith(Value *New);
548 /// SE - The parent ScalarEvolution value. This is used to update
549 /// the parent's maps when the value associated with a SCEVUnknown
550 /// is deleted or RAUW'd.
553 /// Next - The next pointer in the linked list of all
554 /// SCEVUnknown instances owned by a ScalarEvolution.
557 SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
558 ScalarEvolution *se, SCEVUnknown *next) :
559 SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
562 Value *getValue() const { return getValPtr(); }
564 /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
565 /// constant representing a type size, alignment, or field offset in
566 /// a target-independent manner, and hasn't happened to have been
567 /// folded with other operations into something unrecognizable. This
568 /// is mainly only useful for pretty-printing and other situations
569 /// where it isn't absolutely required for these to succeed.
570 bool isSizeOf(const Type *&AllocTy) const;
571 bool isAlignOf(const Type *&AllocTy) const;
572 bool isOffsetOf(const Type *&STy, Constant *&FieldNo) const;
574 virtual bool isLoopInvariant(const Loop *L) const;
575 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
576 return false; // not computable
579 virtual bool hasOperand(const SCEV *) const {
583 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
585 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
587 virtual const Type *getType() const;
589 virtual void print(raw_ostream &OS) const;
591 /// Methods for support type inquiry through isa, cast, and dyn_cast:
592 static inline bool classof(const SCEVUnknown *S) { return true; }
593 static inline bool classof(const SCEV *S) {
594 return S->getSCEVType() == scUnknown;
598 /// SCEVVisitor - This class defines a simple visitor class that may be used
599 /// for various SCEV analysis purposes.
600 template<typename SC, typename RetVal=void>
602 RetVal visit(const SCEV *S) {
603 switch (S->getSCEVType()) {
605 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
607 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
609 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
611 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
613 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
615 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
617 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
619 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
621 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
623 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
625 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
626 case scCouldNotCompute:
627 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
629 llvm_unreachable("Unknown SCEV type!");
633 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
634 llvm_unreachable("Invalid use of SCEVCouldNotCompute!");