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, scUnknown,
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 virtual void print(raw_ostream &OS) const;
65 /// Methods for support type inquiry through isa, cast, and dyn_cast:
66 static inline bool classof(const SCEVConstant *S) { return true; }
67 static inline bool classof(const SCEV *S) {
68 return S->getSCEVType() == scConstant;
72 //===--------------------------------------------------------------------===//
73 /// SCEVCastExpr - This is the base class for unary cast operator classes.
75 class SCEVCastExpr : public SCEV {
80 SCEVCastExpr(const FoldingSetNodeID &ID,
81 unsigned SCEVTy, const SCEV *op, const Type *ty);
84 const SCEV *getOperand() const { return Op; }
85 virtual const Type *getType() const { return Ty; }
87 virtual bool isLoopInvariant(const Loop *L) const {
88 return Op->isLoopInvariant(L);
91 virtual bool hasComputableLoopEvolution(const Loop *L) const {
92 return Op->hasComputableLoopEvolution(L);
95 virtual bool hasOperand(const SCEV *O) const {
96 return Op == O || Op->hasOperand(O);
99 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
101 /// Methods for support type inquiry through isa, cast, and dyn_cast:
102 static inline bool classof(const SCEVCastExpr *S) { return true; }
103 static inline bool classof(const SCEV *S) {
104 return S->getSCEVType() == scTruncate ||
105 S->getSCEVType() == scZeroExtend ||
106 S->getSCEVType() == scSignExtend;
110 //===--------------------------------------------------------------------===//
111 /// SCEVTruncateExpr - This class represents a truncation of an integer value
112 /// to a smaller integer value.
114 class SCEVTruncateExpr : public SCEVCastExpr {
115 friend class ScalarEvolution;
117 SCEVTruncateExpr(const FoldingSetNodeID &ID,
118 const SCEV *op, const Type *ty);
121 virtual void print(raw_ostream &OS) const;
123 /// Methods for support type inquiry through isa, cast, and dyn_cast:
124 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
125 static inline bool classof(const SCEV *S) {
126 return S->getSCEVType() == scTruncate;
130 //===--------------------------------------------------------------------===//
131 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
132 /// integer value to a larger integer value.
134 class SCEVZeroExtendExpr : public SCEVCastExpr {
135 friend class ScalarEvolution;
137 SCEVZeroExtendExpr(const FoldingSetNodeID &ID,
138 const SCEV *op, const Type *ty);
141 virtual void print(raw_ostream &OS) const;
143 /// Methods for support type inquiry through isa, cast, and dyn_cast:
144 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
145 static inline bool classof(const SCEV *S) {
146 return S->getSCEVType() == scZeroExtend;
150 //===--------------------------------------------------------------------===//
151 /// SCEVSignExtendExpr - This class represents a sign extension of a small
152 /// integer value to a larger integer value.
154 class SCEVSignExtendExpr : public SCEVCastExpr {
155 friend class ScalarEvolution;
157 SCEVSignExtendExpr(const FoldingSetNodeID &ID,
158 const SCEV *op, const Type *ty);
161 virtual void print(raw_ostream &OS) const;
163 /// Methods for support type inquiry through isa, cast, and dyn_cast:
164 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
165 static inline bool classof(const SCEV *S) {
166 return S->getSCEVType() == scSignExtend;
171 //===--------------------------------------------------------------------===//
172 /// SCEVNAryExpr - This node is a base class providing common
173 /// functionality for n'ary operators.
175 class SCEVNAryExpr : public SCEV {
177 SmallVector<const SCEV *, 8> Operands;
179 SCEVNAryExpr(const FoldingSetNodeID &ID,
180 enum SCEVTypes T, const SmallVectorImpl<const SCEV *> &ops)
181 : SCEV(ID, T), Operands(ops.begin(), ops.end()) {}
184 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
185 const SCEV *getOperand(unsigned i) const {
186 assert(i < Operands.size() && "Operand index out of range!");
190 const SmallVectorImpl<const SCEV *> &getOperands() const {
193 typedef SmallVectorImpl<const SCEV *>::const_iterator op_iterator;
194 op_iterator op_begin() const { return Operands.begin(); }
195 op_iterator op_end() const { return Operands.end(); }
197 virtual bool isLoopInvariant(const Loop *L) const {
198 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
199 if (!getOperand(i)->isLoopInvariant(L)) return false;
203 // hasComputableLoopEvolution - N-ary expressions have computable loop
204 // evolutions iff they have at least one operand that varies with the loop,
205 // but that all varying operands are computable.
206 virtual bool hasComputableLoopEvolution(const Loop *L) const {
207 bool HasVarying = false;
208 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
209 if (!getOperand(i)->isLoopInvariant(L)) {
210 if (getOperand(i)->hasComputableLoopEvolution(L))
218 virtual bool hasOperand(const SCEV *O) const {
219 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
220 if (O == getOperand(i) || getOperand(i)->hasOperand(O))
225 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
227 virtual const Type *getType() const { return getOperand(0)->getType(); }
229 /// Methods for support type inquiry through isa, cast, and dyn_cast:
230 static inline bool classof(const SCEVNAryExpr *S) { return true; }
231 static inline bool classof(const SCEV *S) {
232 return S->getSCEVType() == scAddExpr ||
233 S->getSCEVType() == scMulExpr ||
234 S->getSCEVType() == scSMaxExpr ||
235 S->getSCEVType() == scUMaxExpr ||
236 S->getSCEVType() == scAddRecExpr;
240 //===--------------------------------------------------------------------===//
241 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
244 class SCEVCommutativeExpr : public SCEVNAryExpr {
246 SCEVCommutativeExpr(const FoldingSetNodeID &ID,
248 const SmallVectorImpl<const SCEV *> &ops)
249 : SCEVNAryExpr(ID, T, ops) {}
252 virtual const char *getOperationStr() const = 0;
254 virtual void print(raw_ostream &OS) const;
256 /// Methods for support type inquiry through isa, cast, and dyn_cast:
257 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
258 static inline bool classof(const SCEV *S) {
259 return S->getSCEVType() == scAddExpr ||
260 S->getSCEVType() == scMulExpr ||
261 S->getSCEVType() == scSMaxExpr ||
262 S->getSCEVType() == scUMaxExpr;
267 //===--------------------------------------------------------------------===//
268 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
270 class SCEVAddExpr : public SCEVCommutativeExpr {
271 friend class ScalarEvolution;
273 SCEVAddExpr(const FoldingSetNodeID &ID,
274 const SmallVectorImpl<const SCEV *> &ops)
275 : SCEVCommutativeExpr(ID, scAddExpr, ops) {
279 virtual const char *getOperationStr() const { return " + "; }
281 /// Methods for support type inquiry through isa, cast, and dyn_cast:
282 static inline bool classof(const SCEVAddExpr *S) { return true; }
283 static inline bool classof(const SCEV *S) {
284 return S->getSCEVType() == scAddExpr;
288 //===--------------------------------------------------------------------===//
289 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
291 class SCEVMulExpr : public SCEVCommutativeExpr {
292 friend class ScalarEvolution;
294 SCEVMulExpr(const FoldingSetNodeID &ID,
295 const SmallVectorImpl<const SCEV *> &ops)
296 : SCEVCommutativeExpr(ID, scMulExpr, ops) {
300 virtual const char *getOperationStr() const { return " * "; }
302 /// Methods for support type inquiry through isa, cast, and dyn_cast:
303 static inline bool classof(const SCEVMulExpr *S) { return true; }
304 static inline bool classof(const SCEV *S) {
305 return S->getSCEVType() == scMulExpr;
310 //===--------------------------------------------------------------------===//
311 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
313 class SCEVUDivExpr : public SCEV {
314 friend class ScalarEvolution;
318 SCEVUDivExpr(const FoldingSetNodeID &ID, const SCEV *lhs, const SCEV *rhs)
319 : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
322 const SCEV *getLHS() const { return LHS; }
323 const SCEV *getRHS() const { return RHS; }
325 virtual bool isLoopInvariant(const Loop *L) const {
326 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
329 virtual bool hasComputableLoopEvolution(const Loop *L) const {
330 return LHS->hasComputableLoopEvolution(L) &&
331 RHS->hasComputableLoopEvolution(L);
334 virtual bool hasOperand(const SCEV *O) const {
335 return O == LHS || O == RHS || LHS->hasOperand(O) || RHS->hasOperand(O);
338 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
340 virtual const Type *getType() const;
342 void print(raw_ostream &OS) const;
344 /// Methods for support type inquiry through isa, cast, and dyn_cast:
345 static inline bool classof(const SCEVUDivExpr *S) { return true; }
346 static inline bool classof(const SCEV *S) {
347 return S->getSCEVType() == scUDivExpr;
352 //===--------------------------------------------------------------------===//
353 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
354 /// count of the specified loop. This is the primary focus of the
355 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
356 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
357 /// created and analyzed.
359 /// All operands of an AddRec are required to be loop invariant.
361 class SCEVAddRecExpr : public SCEVNAryExpr {
362 friend class ScalarEvolution;
366 SCEVAddRecExpr(const FoldingSetNodeID &ID,
367 const SmallVectorImpl<const SCEV *> &ops, const Loop *l)
368 : SCEVNAryExpr(ID, scAddRecExpr, ops), L(l) {
369 for (size_t i = 0, e = Operands.size(); i != e; ++i)
370 assert(Operands[i]->isLoopInvariant(l) &&
371 "Operands of AddRec must be loop-invariant!");
375 const SCEV *getStart() const { return Operands[0]; }
376 const Loop *getLoop() const { return L; }
378 /// getStepRecurrence - This method constructs and returns the recurrence
379 /// indicating how much this expression steps by. If this is a polynomial
380 /// of degree N, it returns a chrec of degree N-1.
381 const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
382 if (isAffine()) return getOperand(1);
383 return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
388 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
389 if (L == QL) return true;
393 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
395 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
396 /// an expressions A+B*x where A and B are loop invariant values.
397 bool isAffine() const {
398 // We know that the start value is invariant. This expression is thus
399 // affine iff the step is also invariant.
400 return getNumOperands() == 2;
403 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
404 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
405 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
406 bool isQuadratic() const {
407 return getNumOperands() == 3;
410 /// evaluateAtIteration - Return the value of this chain of recurrences at
411 /// the specified iteration number.
412 const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
414 /// getNumIterationsInRange - Return the number of iterations of this loop
415 /// that produce values in the specified constant range. Another way of
416 /// looking at this is that it returns the first iteration number where the
417 /// value is not in the condition, thus computing the exit count. If the
418 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
420 const SCEV *getNumIterationsInRange(ConstantRange Range,
421 ScalarEvolution &SE) const;
423 /// getPostIncExpr - Return an expression representing the value of
424 /// this expression one iteration of the loop ahead.
425 const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
426 return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
429 bool hasNoUnsignedOverflow() const { return SubclassData & (1 << 0); }
430 void setHasNoUnsignedOverflow(bool B) {
431 SubclassData = (SubclassData & ~(1 << 0)) | (B << 0);
433 bool hasNoSignedOverflow() const { return SubclassData & (1 << 1); }
434 void setHasNoSignedOverflow(bool B) {
435 SubclassData = (SubclassData & ~(1 << 1)) | (B << 1);
438 virtual void print(raw_ostream &OS) const;
440 /// Methods for support type inquiry through isa, cast, and dyn_cast:
441 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
442 static inline bool classof(const SCEV *S) {
443 return S->getSCEVType() == scAddRecExpr;
448 //===--------------------------------------------------------------------===//
449 /// SCEVSMaxExpr - This class represents a signed maximum selection.
451 class SCEVSMaxExpr : public SCEVCommutativeExpr {
452 friend class ScalarEvolution;
454 SCEVSMaxExpr(const FoldingSetNodeID &ID,
455 const SmallVectorImpl<const SCEV *> &ops)
456 : SCEVCommutativeExpr(ID, scSMaxExpr, ops) {
460 virtual const char *getOperationStr() const { return " smax "; }
462 /// Methods for support type inquiry through isa, cast, and dyn_cast:
463 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
464 static inline bool classof(const SCEV *S) {
465 return S->getSCEVType() == scSMaxExpr;
470 //===--------------------------------------------------------------------===//
471 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
473 class SCEVUMaxExpr : public SCEVCommutativeExpr {
474 friend class ScalarEvolution;
476 SCEVUMaxExpr(const FoldingSetNodeID &ID,
477 const SmallVectorImpl<const SCEV *> &ops)
478 : SCEVCommutativeExpr(ID, scUMaxExpr, ops) {
482 virtual const char *getOperationStr() const { return " umax "; }
484 /// Methods for support type inquiry through isa, cast, and dyn_cast:
485 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
486 static inline bool classof(const SCEV *S) {
487 return S->getSCEVType() == scUMaxExpr;
492 //===--------------------------------------------------------------------===//
493 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
494 /// value, and only represent it as it's LLVM Value. This is the "bottom"
495 /// value for the analysis.
497 class SCEVUnknown : public SCEV {
498 friend class ScalarEvolution;
501 SCEVUnknown(const FoldingSetNodeID &ID, Value *v) :
502 SCEV(ID, scUnknown), V(v) {}
505 Value *getValue() const { return V; }
507 virtual bool isLoopInvariant(const Loop *L) const;
508 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
509 return false; // not computable
512 virtual bool hasOperand(const SCEV *) const {
516 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
518 virtual const Type *getType() const;
520 virtual void print(raw_ostream &OS) const;
522 /// Methods for support type inquiry through isa, cast, and dyn_cast:
523 static inline bool classof(const SCEVUnknown *S) { return true; }
524 static inline bool classof(const SCEV *S) {
525 return S->getSCEVType() == scUnknown;
529 /// SCEVVisitor - This class defines a simple visitor class that may be used
530 /// for various SCEV analysis purposes.
531 template<typename SC, typename RetVal=void>
533 RetVal visit(const SCEV *S) {
534 switch (S->getSCEVType()) {
536 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
538 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
540 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
542 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
544 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
546 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
548 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
550 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
552 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
554 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
556 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
557 case scCouldNotCompute:
558 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
560 llvm_unreachable("Unknown SCEV type!");
564 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
565 llvm_unreachable("Invalid use of SCEVCouldNotCompute!");