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 /// Methods for support type inquiry through isa, cast, and dyn_cast:
130 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
131 static inline bool classof(const SCEV *S) {
132 return S->getSCEVType() == scTruncate;
136 //===--------------------------------------------------------------------===//
137 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
138 /// integer value to a larger integer value.
140 class SCEVZeroExtendExpr : public SCEVCastExpr {
141 friend class ScalarEvolution;
143 SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
144 const SCEV *op, const Type *ty);
147 virtual void print(raw_ostream &OS) const;
149 /// Methods for support type inquiry through isa, cast, and dyn_cast:
150 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
151 static inline bool classof(const SCEV *S) {
152 return S->getSCEVType() == scZeroExtend;
156 //===--------------------------------------------------------------------===//
157 /// SCEVSignExtendExpr - This class represents a sign extension of a small
158 /// integer value to a larger integer value.
160 class SCEVSignExtendExpr : public SCEVCastExpr {
161 friend class ScalarEvolution;
163 SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
164 const SCEV *op, const Type *ty);
167 virtual void print(raw_ostream &OS) const;
169 /// Methods for support type inquiry through isa, cast, and dyn_cast:
170 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
171 static inline bool classof(const SCEV *S) {
172 return S->getSCEVType() == scSignExtend;
177 //===--------------------------------------------------------------------===//
178 /// SCEVNAryExpr - This node is a base class providing common
179 /// functionality for n'ary operators.
181 class SCEVNAryExpr : public SCEV {
183 // Since SCEVs are immutable, ScalarEvolution allocates operand
184 // arrays with its SCEVAllocator, so this class just needs a simple
185 // pointer rather than a more elaborate vector-like data structure.
186 // This also avoids the need for a non-trivial destructor.
187 const SCEV *const *Operands;
190 SCEVNAryExpr(const FoldingSetNodeIDRef ID,
191 enum SCEVTypes T, const SCEV *const *O, size_t N)
192 : SCEV(ID, T), Operands(O), NumOperands(N) {}
195 size_t getNumOperands() const { return NumOperands; }
196 const SCEV *getOperand(unsigned i) const {
197 assert(i < NumOperands && "Operand index out of range!");
201 typedef const SCEV *const *op_iterator;
202 op_iterator op_begin() const { return Operands; }
203 op_iterator op_end() const { return Operands + NumOperands; }
205 virtual bool isLoopInvariant(const Loop *L) const {
206 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
207 if (!getOperand(i)->isLoopInvariant(L)) return false;
211 // hasComputableLoopEvolution - N-ary expressions have computable loop
212 // evolutions iff they have at least one operand that varies with the loop,
213 // but that all varying operands are computable.
214 virtual bool hasComputableLoopEvolution(const Loop *L) const {
215 bool HasVarying = false;
216 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
217 if (!getOperand(i)->isLoopInvariant(L)) {
218 if (getOperand(i)->hasComputableLoopEvolution(L))
226 virtual bool hasOperand(const SCEV *O) const {
227 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
228 if (O == getOperand(i) || getOperand(i)->hasOperand(O))
233 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
235 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
237 virtual const Type *getType() const { return getOperand(0)->getType(); }
239 bool hasNoUnsignedWrap() const { return SubclassData & (1 << 0); }
240 void setHasNoUnsignedWrap(bool B) {
241 SubclassData = (SubclassData & ~(1 << 0)) | (B << 0);
243 bool hasNoSignedWrap() const { return SubclassData & (1 << 1); }
244 void setHasNoSignedWrap(bool B) {
245 SubclassData = (SubclassData & ~(1 << 1)) | (B << 1);
248 /// Methods for support type inquiry through isa, cast, and dyn_cast:
249 static inline bool classof(const SCEVNAryExpr *S) { return true; }
250 static inline bool classof(const SCEV *S) {
251 return S->getSCEVType() == scAddExpr ||
252 S->getSCEVType() == scMulExpr ||
253 S->getSCEVType() == scSMaxExpr ||
254 S->getSCEVType() == scUMaxExpr ||
255 S->getSCEVType() == scAddRecExpr;
259 //===--------------------------------------------------------------------===//
260 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
263 class SCEVCommutativeExpr : public SCEVNAryExpr {
265 SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
266 enum SCEVTypes T, const SCEV *const *O, size_t N)
267 : SCEVNAryExpr(ID, T, O, N) {}
270 virtual const char *getOperationStr() const = 0;
272 virtual void print(raw_ostream &OS) const;
274 /// Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
276 static inline bool classof(const SCEV *S) {
277 return S->getSCEVType() == scAddExpr ||
278 S->getSCEVType() == scMulExpr ||
279 S->getSCEVType() == scSMaxExpr ||
280 S->getSCEVType() == scUMaxExpr;
285 //===--------------------------------------------------------------------===//
286 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
288 class SCEVAddExpr : public SCEVCommutativeExpr {
289 friend class ScalarEvolution;
291 SCEVAddExpr(const FoldingSetNodeIDRef ID,
292 const SCEV *const *O, size_t N)
293 : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
297 virtual const char *getOperationStr() const { return " + "; }
299 virtual const Type *getType() const {
300 // Use the type of the last operand, which is likely to be a pointer
301 // type, if there is one. This doesn't usually matter, but it can help
302 // reduce casts when the expressions are expanded.
303 return getOperand(getNumOperands() - 1)->getType();
306 /// Methods for support type inquiry through isa, cast, and dyn_cast:
307 static inline bool classof(const SCEVAddExpr *S) { return true; }
308 static inline bool classof(const SCEV *S) {
309 return S->getSCEVType() == scAddExpr;
313 //===--------------------------------------------------------------------===//
314 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
316 class SCEVMulExpr : public SCEVCommutativeExpr {
317 friend class ScalarEvolution;
319 SCEVMulExpr(const FoldingSetNodeIDRef ID,
320 const SCEV *const *O, size_t N)
321 : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
325 virtual const char *getOperationStr() const { return " * "; }
327 /// Methods for support type inquiry through isa, cast, and dyn_cast:
328 static inline bool classof(const SCEVMulExpr *S) { return true; }
329 static inline bool classof(const SCEV *S) {
330 return S->getSCEVType() == scMulExpr;
335 //===--------------------------------------------------------------------===//
336 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
338 class SCEVUDivExpr : public SCEV {
339 friend class ScalarEvolution;
343 SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
344 : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
347 const SCEV *getLHS() const { return LHS; }
348 const SCEV *getRHS() const { return RHS; }
350 virtual bool isLoopInvariant(const Loop *L) const {
351 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
354 virtual bool hasComputableLoopEvolution(const Loop *L) const {
355 return LHS->hasComputableLoopEvolution(L) &&
356 RHS->hasComputableLoopEvolution(L);
359 virtual bool hasOperand(const SCEV *O) const {
360 return O == LHS || O == RHS || LHS->hasOperand(O) || RHS->hasOperand(O);
363 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
365 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
367 virtual const Type *getType() const;
369 void print(raw_ostream &OS) const;
371 /// Methods for support type inquiry through isa, cast, and dyn_cast:
372 static inline bool classof(const SCEVUDivExpr *S) { return true; }
373 static inline bool classof(const SCEV *S) {
374 return S->getSCEVType() == scUDivExpr;
379 //===--------------------------------------------------------------------===//
380 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
381 /// count of the specified loop. This is the primary focus of the
382 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
383 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
384 /// created and analyzed.
386 /// All operands of an AddRec are required to be loop invariant.
388 class SCEVAddRecExpr : public SCEVNAryExpr {
389 friend class ScalarEvolution;
393 SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
394 const SCEV *const *O, size_t N, const Loop *l)
395 : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {
396 for (size_t i = 0, e = NumOperands; i != e; ++i)
397 assert(Operands[i]->isLoopInvariant(l) &&
398 "Operands of AddRec must be loop-invariant!");
402 const SCEV *getStart() const { return Operands[0]; }
403 const Loop *getLoop() const { return L; }
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,
415 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
419 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
421 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
423 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
425 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
426 /// an expressions A+B*x where A and B are loop invariant values.
427 bool isAffine() const {
428 // We know that the start value is invariant. This expression is thus
429 // affine iff the step is also invariant.
430 return getNumOperands() == 2;
433 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
434 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
435 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
436 bool isQuadratic() const {
437 return getNumOperands() == 3;
440 /// evaluateAtIteration - Return the value of this chain of recurrences at
441 /// the specified iteration number.
442 const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
444 /// getNumIterationsInRange - Return the number of iterations of this loop
445 /// that produce values in the specified constant range. Another way of
446 /// looking at this is that it returns the first iteration number where the
447 /// value is not in the condition, thus computing the exit count. If the
448 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
450 const SCEV *getNumIterationsInRange(ConstantRange Range,
451 ScalarEvolution &SE) const;
453 /// getPostIncExpr - Return an expression representing the value of
454 /// this expression one iteration of the loop ahead.
455 const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
456 return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
459 virtual void print(raw_ostream &OS) const;
461 /// Methods for support type inquiry through isa, cast, and dyn_cast:
462 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
463 static inline bool classof(const SCEV *S) {
464 return S->getSCEVType() == scAddRecExpr;
469 //===--------------------------------------------------------------------===//
470 /// SCEVSMaxExpr - This class represents a signed maximum selection.
472 class SCEVSMaxExpr : public SCEVCommutativeExpr {
473 friend class ScalarEvolution;
475 SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
476 const SCEV *const *O, size_t N)
477 : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
478 // Max never overflows.
479 setHasNoUnsignedWrap(true);
480 setHasNoSignedWrap(true);
484 virtual const char *getOperationStr() const { return " smax "; }
486 /// Methods for support type inquiry through isa, cast, and dyn_cast:
487 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
488 static inline bool classof(const SCEV *S) {
489 return S->getSCEVType() == scSMaxExpr;
494 //===--------------------------------------------------------------------===//
495 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
497 class SCEVUMaxExpr : public SCEVCommutativeExpr {
498 friend class ScalarEvolution;
500 SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
501 const SCEV *const *O, size_t N)
502 : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
503 // Max never overflows.
504 setHasNoUnsignedWrap(true);
505 setHasNoSignedWrap(true);
509 virtual const char *getOperationStr() const { return " umax "; }
511 /// Methods for support type inquiry through isa, cast, and dyn_cast:
512 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
513 static inline bool classof(const SCEV *S) {
514 return S->getSCEVType() == scUMaxExpr;
518 //===--------------------------------------------------------------------===//
519 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
520 /// value, and only represent it as its LLVM Value. This is the "bottom"
521 /// value for the analysis.
523 class SCEVUnknown : public SCEV {
524 friend class ScalarEvolution;
525 friend class ScalarEvolution::SCEVCallbackVH;
527 // This should be an AssertingVH, however SCEVUnknowns are allocated in a
528 // BumpPtrAllocator so their destructors are never called.
530 SCEVUnknown(const FoldingSetNodeIDRef ID, Value *v) :
531 SCEV(ID, scUnknown), V(v) {}
534 Value *getValue() const { return V; }
536 /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
537 /// constant representing a type size, alignment, or field offset in
538 /// a target-independent manner, and hasn't happened to have been
539 /// folded with other operations into something unrecognizable. This
540 /// is mainly only useful for pretty-printing and other situations
541 /// where it isn't absolutely required for these to succeed.
542 bool isSizeOf(const Type *&AllocTy) const;
543 bool isAlignOf(const Type *&AllocTy) const;
544 bool isOffsetOf(const Type *&STy, Constant *&FieldNo) const;
546 virtual bool isLoopInvariant(const Loop *L) const;
547 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
548 return false; // not computable
551 virtual bool hasOperand(const SCEV *) const {
555 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
557 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
559 virtual const Type *getType() const;
561 virtual void print(raw_ostream &OS) const;
563 /// Methods for support type inquiry through isa, cast, and dyn_cast:
564 static inline bool classof(const SCEVUnknown *S) { return true; }
565 static inline bool classof(const SCEV *S) {
566 return S->getSCEVType() == scUnknown;
570 /// SCEVVisitor - This class defines a simple visitor class that may be used
571 /// for various SCEV analysis purposes.
572 template<typename SC, typename RetVal=void>
574 RetVal visit(const SCEV *S) {
575 switch (S->getSCEVType()) {
577 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
579 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
581 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
583 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
585 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
587 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
589 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
591 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
593 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
595 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
597 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
598 case scCouldNotCompute:
599 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
601 llvm_unreachable("Unknown SCEV type!");
605 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
606 llvm_unreachable("Invalid use of SCEVCouldNotCompute!");