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 scFieldOffset, scAllocSize, scUnknown, scCouldNotCompute
33 //===--------------------------------------------------------------------===//
34 /// SCEVConstant - This class represents a constant integer value.
36 class SCEVConstant : public SCEV {
37 friend class ScalarEvolution;
40 SCEVConstant(const FoldingSetNodeID &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 FoldingSetNodeID &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 FoldingSetNodeID &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 FoldingSetNodeID &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 FoldingSetNodeID &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 SmallVector<const SCEV *, 8> Operands;
185 SCEVNAryExpr(const FoldingSetNodeID &ID,
186 enum SCEVTypes T, const SmallVectorImpl<const SCEV *> &ops)
187 : SCEV(ID, T), Operands(ops.begin(), ops.end()) {}
190 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
191 const SCEV *getOperand(unsigned i) const {
192 assert(i < Operands.size() && "Operand index out of range!");
196 const SmallVectorImpl<const SCEV *> &getOperands() const {
199 typedef SmallVectorImpl<const SCEV *>::const_iterator op_iterator;
200 op_iterator op_begin() const { return Operands.begin(); }
201 op_iterator op_end() const { return Operands.end(); }
203 virtual bool isLoopInvariant(const Loop *L) const {
204 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
205 if (!getOperand(i)->isLoopInvariant(L)) return false;
209 // hasComputableLoopEvolution - N-ary expressions have computable loop
210 // evolutions iff they have at least one operand that varies with the loop,
211 // but that all varying operands are computable.
212 virtual bool hasComputableLoopEvolution(const Loop *L) const {
213 bool HasVarying = false;
214 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
215 if (!getOperand(i)->isLoopInvariant(L)) {
216 if (getOperand(i)->hasComputableLoopEvolution(L))
224 virtual bool hasOperand(const SCEV *O) const {
225 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
226 if (O == getOperand(i) || getOperand(i)->hasOperand(O))
231 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
233 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
235 virtual const Type *getType() const { return getOperand(0)->getType(); }
237 /// Methods for support type inquiry through isa, cast, and dyn_cast:
238 static inline bool classof(const SCEVNAryExpr *S) { return true; }
239 static inline bool classof(const SCEV *S) {
240 return S->getSCEVType() == scAddExpr ||
241 S->getSCEVType() == scMulExpr ||
242 S->getSCEVType() == scSMaxExpr ||
243 S->getSCEVType() == scUMaxExpr ||
244 S->getSCEVType() == scAddRecExpr;
248 //===--------------------------------------------------------------------===//
249 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
252 class SCEVCommutativeExpr : public SCEVNAryExpr {
254 SCEVCommutativeExpr(const FoldingSetNodeID &ID,
256 const SmallVectorImpl<const SCEV *> &ops)
257 : SCEVNAryExpr(ID, T, ops) {}
260 virtual const char *getOperationStr() const = 0;
262 virtual void print(raw_ostream &OS) const;
264 /// Methods for support type inquiry through isa, cast, and dyn_cast:
265 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
266 static inline bool classof(const SCEV *S) {
267 return S->getSCEVType() == scAddExpr ||
268 S->getSCEVType() == scMulExpr ||
269 S->getSCEVType() == scSMaxExpr ||
270 S->getSCEVType() == scUMaxExpr;
275 //===--------------------------------------------------------------------===//
276 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
278 class SCEVAddExpr : public SCEVCommutativeExpr {
279 friend class ScalarEvolution;
281 SCEVAddExpr(const FoldingSetNodeID &ID,
282 const SmallVectorImpl<const SCEV *> &ops)
283 : SCEVCommutativeExpr(ID, scAddExpr, ops) {
287 virtual const char *getOperationStr() const { return " + "; }
289 /// Methods for support type inquiry through isa, cast, and dyn_cast:
290 static inline bool classof(const SCEVAddExpr *S) { return true; }
291 static inline bool classof(const SCEV *S) {
292 return S->getSCEVType() == scAddExpr;
296 //===--------------------------------------------------------------------===//
297 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
299 class SCEVMulExpr : public SCEVCommutativeExpr {
300 friend class ScalarEvolution;
302 SCEVMulExpr(const FoldingSetNodeID &ID,
303 const SmallVectorImpl<const SCEV *> &ops)
304 : SCEVCommutativeExpr(ID, scMulExpr, ops) {
308 virtual const char *getOperationStr() const { return " * "; }
310 /// Methods for support type inquiry through isa, cast, and dyn_cast:
311 static inline bool classof(const SCEVMulExpr *S) { return true; }
312 static inline bool classof(const SCEV *S) {
313 return S->getSCEVType() == scMulExpr;
318 //===--------------------------------------------------------------------===//
319 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
321 class SCEVUDivExpr : public SCEV {
322 friend class ScalarEvolution;
326 SCEVUDivExpr(const FoldingSetNodeID &ID, const SCEV *lhs, const SCEV *rhs)
327 : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
330 const SCEV *getLHS() const { return LHS; }
331 const SCEV *getRHS() const { return RHS; }
333 virtual bool isLoopInvariant(const Loop *L) const {
334 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
337 virtual bool hasComputableLoopEvolution(const Loop *L) const {
338 return LHS->hasComputableLoopEvolution(L) &&
339 RHS->hasComputableLoopEvolution(L);
342 virtual bool hasOperand(const SCEV *O) const {
343 return O == LHS || O == RHS || LHS->hasOperand(O) || RHS->hasOperand(O);
346 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
348 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
350 virtual const Type *getType() const;
352 void print(raw_ostream &OS) const;
354 /// Methods for support type inquiry through isa, cast, and dyn_cast:
355 static inline bool classof(const SCEVUDivExpr *S) { return true; }
356 static inline bool classof(const SCEV *S) {
357 return S->getSCEVType() == scUDivExpr;
362 //===--------------------------------------------------------------------===//
363 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
364 /// count of the specified loop. This is the primary focus of the
365 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
366 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
367 /// created and analyzed.
369 /// All operands of an AddRec are required to be loop invariant.
371 class SCEVAddRecExpr : public SCEVNAryExpr {
372 friend class ScalarEvolution;
376 SCEVAddRecExpr(const FoldingSetNodeID &ID,
377 const SmallVectorImpl<const SCEV *> &ops, const Loop *l)
378 : SCEVNAryExpr(ID, scAddRecExpr, ops), L(l) {
379 for (size_t i = 0, e = Operands.size(); i != e; ++i)
380 assert(Operands[i]->isLoopInvariant(l) &&
381 "Operands of AddRec must be loop-invariant!");
385 const SCEV *getStart() const { return Operands[0]; }
386 const Loop *getLoop() const { return L; }
388 /// getStepRecurrence - This method constructs and returns the recurrence
389 /// indicating how much this expression steps by. If this is a polynomial
390 /// of degree N, it returns a chrec of degree N-1.
391 const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
392 if (isAffine()) return getOperand(1);
393 return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
398 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
399 if (L == QL) return true;
403 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
405 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
406 /// an expressions A+B*x where A and B are loop invariant values.
407 bool isAffine() const {
408 // We know that the start value is invariant. This expression is thus
409 // affine iff the step is also invariant.
410 return getNumOperands() == 2;
413 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
414 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
415 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
416 bool isQuadratic() const {
417 return getNumOperands() == 3;
420 /// evaluateAtIteration - Return the value of this chain of recurrences at
421 /// the specified iteration number.
422 const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
424 /// getNumIterationsInRange - Return the number of iterations of this loop
425 /// that produce values in the specified constant range. Another way of
426 /// looking at this is that it returns the first iteration number where the
427 /// value is not in the condition, thus computing the exit count. If the
428 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
430 const SCEV *getNumIterationsInRange(ConstantRange Range,
431 ScalarEvolution &SE) const;
433 /// getPostIncExpr - Return an expression representing the value of
434 /// this expression one iteration of the loop ahead.
435 const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
436 return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
439 bool hasNoUnsignedWrap() const { return SubclassData & (1 << 0); }
440 void setHasNoUnsignedWrap(bool B) {
441 SubclassData = (SubclassData & ~(1 << 0)) | (B << 0);
443 bool hasNoSignedWrap() const { return SubclassData & (1 << 1); }
444 void setHasNoSignedWrap(bool B) {
445 SubclassData = (SubclassData & ~(1 << 1)) | (B << 1);
448 virtual void print(raw_ostream &OS) const;
450 /// Methods for support type inquiry through isa, cast, and dyn_cast:
451 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
452 static inline bool classof(const SCEV *S) {
453 return S->getSCEVType() == scAddRecExpr;
458 //===--------------------------------------------------------------------===//
459 /// SCEVSMaxExpr - This class represents a signed maximum selection.
461 class SCEVSMaxExpr : public SCEVCommutativeExpr {
462 friend class ScalarEvolution;
464 SCEVSMaxExpr(const FoldingSetNodeID &ID,
465 const SmallVectorImpl<const SCEV *> &ops)
466 : SCEVCommutativeExpr(ID, scSMaxExpr, ops) {
470 virtual const char *getOperationStr() const { return " smax "; }
472 /// Methods for support type inquiry through isa, cast, and dyn_cast:
473 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
474 static inline bool classof(const SCEV *S) {
475 return S->getSCEVType() == scSMaxExpr;
480 //===--------------------------------------------------------------------===//
481 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
483 class SCEVUMaxExpr : public SCEVCommutativeExpr {
484 friend class ScalarEvolution;
486 SCEVUMaxExpr(const FoldingSetNodeID &ID,
487 const SmallVectorImpl<const SCEV *> &ops)
488 : SCEVCommutativeExpr(ID, scUMaxExpr, ops) {
492 virtual const char *getOperationStr() const { return " umax "; }
494 /// Methods for support type inquiry through isa, cast, and dyn_cast:
495 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
496 static inline bool classof(const SCEV *S) {
497 return S->getSCEVType() == scUMaxExpr;
501 //===--------------------------------------------------------------------===//
502 /// SCEVTargetDataConstant - This node is the base class for representing
503 /// target-dependent values in a target-independent way.
505 class SCEVTargetDataConstant : public SCEV {
508 SCEVTargetDataConstant(const FoldingSetNodeID &ID, enum SCEVTypes T,
510 SCEV(ID, T), Ty(ty) {}
513 virtual bool isLoopInvariant(const Loop *) const { return true; }
514 virtual bool hasComputableLoopEvolution(const Loop *) const {
515 return false; // not computable
518 virtual bool hasOperand(const SCEV *) const {
522 bool dominates(BasicBlock *, DominatorTree *) const {
526 bool properlyDominates(BasicBlock *, DominatorTree *) const {
530 virtual const Type *getType() const { return Ty; }
532 /// Methods for support type inquiry through isa, cast, and dyn_cast:
533 static inline bool classof(const SCEVTargetDataConstant *S) { return true; }
534 static inline bool classof(const SCEV *S) {
535 return S->getSCEVType() == scFieldOffset ||
536 S->getSCEVType() == scAllocSize;
540 //===--------------------------------------------------------------------===//
541 /// SCEVFieldOffsetExpr - This node represents an offsetof expression.
543 class SCEVFieldOffsetExpr : public SCEVTargetDataConstant {
544 friend class ScalarEvolution;
546 const StructType *STy;
548 SCEVFieldOffsetExpr(const FoldingSetNodeID &ID, const Type *ty,
549 const StructType *sty, unsigned fieldno) :
550 SCEVTargetDataConstant(ID, scFieldOffset, ty),
551 STy(sty), FieldNo(fieldno) {}
554 const StructType *getStructType() const { return STy; }
555 unsigned getFieldNo() const { return FieldNo; }
557 virtual void print(raw_ostream &OS) const;
559 /// Methods for support type inquiry through isa, cast, and dyn_cast:
560 static inline bool classof(const SCEVFieldOffsetExpr *S) { return true; }
561 static inline bool classof(const SCEV *S) {
562 return S->getSCEVType() == scFieldOffset;
566 //===--------------------------------------------------------------------===//
567 /// SCEVAllocSize - This node represents a sizeof expression.
569 class SCEVAllocSizeExpr : public SCEVTargetDataConstant {
570 friend class ScalarEvolution;
573 SCEVAllocSizeExpr(const FoldingSetNodeID &ID,
574 const Type *ty, const Type *allocty) :
575 SCEVTargetDataConstant(ID, scAllocSize, ty),
579 const Type *getAllocType() const { return AllocTy; }
581 virtual void print(raw_ostream &OS) const;
583 /// Methods for support type inquiry through isa, cast, and dyn_cast:
584 static inline bool classof(const SCEVAllocSizeExpr *S) { return true; }
585 static inline bool classof(const SCEV *S) {
586 return S->getSCEVType() == scAllocSize;
590 //===--------------------------------------------------------------------===//
591 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
592 /// value, and only represent it as its LLVM Value. This is the "bottom"
593 /// value for the analysis.
595 class SCEVUnknown : public SCEV {
596 friend class ScalarEvolution;
599 SCEVUnknown(const FoldingSetNodeID &ID, Value *v) :
600 SCEV(ID, scUnknown), V(v) {}
603 Value *getValue() const { return V; }
605 virtual bool isLoopInvariant(const Loop *L) const;
606 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
607 return false; // not computable
610 virtual bool hasOperand(const SCEV *) const {
614 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
616 bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const;
618 virtual const Type *getType() const;
620 virtual void print(raw_ostream &OS) const;
622 /// Methods for support type inquiry through isa, cast, and dyn_cast:
623 static inline bool classof(const SCEVUnknown *S) { return true; }
624 static inline bool classof(const SCEV *S) {
625 return S->getSCEVType() == scUnknown;
629 /// SCEVVisitor - This class defines a simple visitor class that may be used
630 /// for various SCEV analysis purposes.
631 template<typename SC, typename RetVal=void>
633 RetVal visit(const SCEV *S) {
634 switch (S->getSCEVType()) {
636 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
638 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
640 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
642 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
644 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
646 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
648 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
650 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
652 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
654 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
656 return ((SC*)this)->visitFieldOffsetExpr((const SCEVFieldOffsetExpr*)S);
658 return ((SC*)this)->visitAllocSizeExpr((const SCEVAllocSizeExpr*)S);
660 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
661 case scCouldNotCompute:
662 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
664 llvm_unreachable("Unknown SCEV type!");
668 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
669 llvm_unreachable("Invalid use of SCEVCouldNotCompute!");