1 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
24 // These should be ordered in terms of increasing complexity to make the
26 scConstant, scTruncate, scZeroExtend, scAddExpr, scMulExpr, scUDivExpr,
27 scAddRecExpr, scUnknown, scCouldNotCompute
30 //===--------------------------------------------------------------------===//
31 /// SCEVConstant - This class represents a constant integer value.
33 class SCEVConstant : public SCEV {
35 SCEVConstant(ConstantInt *v) : SCEV(scConstant), V(v) {}
37 virtual ~SCEVConstant();
39 /// get method - This just gets and returns a new SCEVConstant object.
41 static SCEVHandle get(ConstantInt *V);
43 ConstantInt *getValue() const { return V; }
45 /// getValueRange - Return the tightest constant bounds that this value is
46 /// known to have. This method is only valid on integer SCEV objects.
47 virtual ConstantRange getValueRange() const;
49 virtual bool isLoopInvariant(const Loop *L) const {
53 virtual bool hasComputableLoopEvolution(const Loop *L) const {
54 return false; // Not loop variant
57 virtual const Type *getType() const;
59 virtual void print(std::ostream &OS) const;
61 /// Methods for support type inquiry through isa, cast, and dyn_cast:
62 static inline bool classof(const SCEVConstant *S) { return true; }
63 static inline bool classof(const SCEV *S) {
64 return S->getSCEVType() == scConstant;
68 //===--------------------------------------------------------------------===//
69 /// SCEVTruncateExpr - This class represents a truncation of an integer value
70 /// to a smaller integer value.
72 class SCEVTruncateExpr : public SCEV {
75 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
76 virtual ~SCEVTruncateExpr();
78 /// get method - This just gets and returns a new SCEVTruncate object
80 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
82 const SCEVHandle &getOperand() const { return Op; }
83 virtual const Type *getType() const { return Ty; }
85 virtual bool isLoopInvariant(const Loop *L) const {
86 return Op->isLoopInvariant(L);
89 virtual bool hasComputableLoopEvolution(const Loop *L) const {
90 return Op->hasComputableLoopEvolution(L);
93 /// getValueRange - Return the tightest constant bounds that this value is
94 /// known to have. This method is only valid on integer SCEV objects.
95 virtual ConstantRange getValueRange() const;
97 virtual void print(std::ostream &OS) const;
99 /// Methods for support type inquiry through isa, cast, and dyn_cast:
100 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
101 static inline bool classof(const SCEV *S) {
102 return S->getSCEVType() == scTruncate;
106 //===--------------------------------------------------------------------===//
107 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
108 /// integer value to a larger integer value.
110 class SCEVZeroExtendExpr : public SCEV {
113 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
114 virtual ~SCEVZeroExtendExpr();
116 /// get method - This just gets and returns a new SCEVZeroExtend object
118 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
120 const SCEVHandle &getOperand() const { return Op; }
121 virtual const Type *getType() const { return Ty; }
123 virtual bool isLoopInvariant(const Loop *L) const {
124 return Op->isLoopInvariant(L);
127 virtual bool hasComputableLoopEvolution(const Loop *L) const {
128 return Op->hasComputableLoopEvolution(L);
131 /// getValueRange - Return the tightest constant bounds that this value is
132 /// known to have. This method is only valid on integer SCEV objects.
133 virtual ConstantRange getValueRange() const;
135 virtual void print(std::ostream &OS) const;
137 /// Methods for support type inquiry through isa, cast, and dyn_cast:
138 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
139 static inline bool classof(const SCEV *S) {
140 return S->getSCEVType() == scZeroExtend;
145 //===--------------------------------------------------------------------===//
146 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
149 class SCEVCommutativeExpr : public SCEV {
150 std::vector<SCEVHandle> Operands;
153 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
155 Operands.reserve(ops.size());
156 Operands.insert(Operands.end(), ops.begin(), ops.end());
158 ~SCEVCommutativeExpr();
161 unsigned getNumOperands() const { return Operands.size(); }
162 const SCEVHandle &getOperand(unsigned i) const {
163 assert(i < Operands.size() && "Operand index out of range!");
167 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
168 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
169 op_iterator op_begin() const { return Operands.begin(); }
170 op_iterator op_end() const { return Operands.end(); }
173 virtual bool isLoopInvariant(const Loop *L) const {
174 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
175 if (!getOperand(i)->isLoopInvariant(L)) return false;
179 virtual bool hasComputableLoopEvolution(const Loop *L) const {
180 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
181 if (getOperand(i)->hasComputableLoopEvolution(L)) return true;
185 virtual const char *getOperationStr() const = 0;
187 virtual const Type *getType() const { return getOperand(0)->getType(); }
188 virtual void print(std::ostream &OS) const;
190 /// Methods for support type inquiry through isa, cast, and dyn_cast:
191 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
192 static inline bool classof(const SCEV *S) {
193 return S->getSCEVType() == scAddExpr ||
194 S->getSCEVType() == scMulExpr;
199 //===--------------------------------------------------------------------===//
200 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
202 class SCEVAddExpr : public SCEVCommutativeExpr {
203 SCEVAddExpr(const std::vector<SCEVHandle> &ops)
204 : SCEVCommutativeExpr(scAddExpr, ops) {
208 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
210 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
211 std::vector<SCEVHandle> Ops;
217 static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
218 const SCEVHandle &Op2) {
219 std::vector<SCEVHandle> Ops;
226 virtual const char *getOperationStr() const { return " + "; }
228 /// Methods for support type inquiry through isa, cast, and dyn_cast:
229 static inline bool classof(const SCEVAddExpr *S) { return true; }
230 static inline bool classof(const SCEV *S) {
231 return S->getSCEVType() == scAddExpr;
235 //===--------------------------------------------------------------------===//
236 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
238 class SCEVMulExpr : public SCEVCommutativeExpr {
239 SCEVMulExpr(const std::vector<SCEVHandle> &ops)
240 : SCEVCommutativeExpr(scMulExpr, ops) {
244 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
246 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
247 std::vector<SCEVHandle> Ops;
253 virtual const char *getOperationStr() const { return " * "; }
255 /// Methods for support type inquiry through isa, cast, and dyn_cast:
256 static inline bool classof(const SCEVMulExpr *S) { return true; }
257 static inline bool classof(const SCEV *S) {
258 return S->getSCEVType() == scMulExpr;
263 //===--------------------------------------------------------------------===//
264 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
266 class SCEVUDivExpr : public SCEV {
268 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
269 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
271 virtual ~SCEVUDivExpr();
273 /// get method - This just gets and returns a new SCEVUDiv object.
275 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
277 const SCEVHandle &getLHS() const { return LHS; }
278 const SCEVHandle &getRHS() const { return RHS; }
280 virtual bool isLoopInvariant(const Loop *L) const {
281 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
284 virtual bool hasComputableLoopEvolution(const Loop *L) const {
285 return LHS->hasComputableLoopEvolution(L) &&
286 RHS->hasComputableLoopEvolution(L);
289 virtual const Type *getType() const;
291 void print(std::ostream &OS) const;
293 /// Methods for support type inquiry through isa, cast, and dyn_cast:
294 static inline bool classof(const SCEVUDivExpr *S) { return true; }
295 static inline bool classof(const SCEV *S) {
296 return S->getSCEVType() == scUDivExpr;
301 //===--------------------------------------------------------------------===//
302 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
303 /// count of the specified loop.
305 /// All operands of an AddRec are required to be loop invariant.
307 class SCEVAddRecExpr : public SCEV {
308 std::vector<SCEVHandle> Operands;
311 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
312 : SCEV(scAddRecExpr), Operands(ops), L(l) {
313 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
314 assert(Operands[i]->isLoopInvariant(l) &&
315 "Operands of AddRec must be loop-invariant!");
319 static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
321 static SCEVHandle get(std::vector<SCEVHandle> &Operands,
323 static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
325 std::vector<SCEVHandle> NewOp(Operands);
326 return get(NewOp, L);
329 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
330 op_iterator op_begin() const { return Operands.begin(); }
331 op_iterator op_end() const { return Operands.end(); }
333 unsigned getNumOperands() const { return Operands.size(); }
334 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
335 const SCEVHandle &getStart() const { return Operands[0]; }
336 const Loop *getLoop() const { return L; }
339 /// getStepRecurrence - This method constructs and returns the recurrence
340 /// indicating how much this expression steps by. If this is a polynomial
341 /// of degree N, it returns a chrec of degree N-1.
342 SCEVHandle getStepRecurrence() const {
343 if (getNumOperands() == 2) return getOperand(1);
344 return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
348 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
349 if (L == QL) return true;
350 /// FIXME: What if the start or step value a recurrence for the specified
355 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
357 virtual const Type *getType() const { return Operands[0]->getType(); }
359 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
360 /// an expressions A+B*x where A and B are loop invariant values.
361 bool isAffine() const {
362 // We know that the start value is invariant. This expression is thus
363 // affine iff the step is also invariant.
364 return getNumOperands() == 2;
367 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
368 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
369 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
370 bool isQuadratic() const {
371 return getNumOperands() == 3;
374 /// evaluateAtIteration - Return the value of this chain of recurrences at
375 /// the specified iteration number.
376 SCEVHandle evaluateAtIteration(SCEVHandle It) const;
378 /// getNumIterationsInRange - Return the number of iterations of this loop
379 /// that produce values in the specified constant range. Another way of
380 /// looking at this is that it returns the first iteration number where the
381 /// value is not in the condition, thus computing the exit count. If the
382 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
384 SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
387 virtual void print(std::ostream &OS) const;
389 /// Methods for support type inquiry through isa, cast, and dyn_cast:
390 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
391 static inline bool classof(const SCEV *S) {
392 return S->getSCEVType() == scAddRecExpr;
396 //===--------------------------------------------------------------------===//
397 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
398 /// value, and only represent it as it's LLVM Value. This is the "bottom"
399 /// value for the analysis.
401 class SCEVUnknown : public SCEV {
403 SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
408 /// get method - For SCEVUnknown, this just gets and returns a new
410 static SCEVHandle get(Value *V);
412 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
413 /// specified signed integer value and return a SCEV for the constant.
414 static SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
416 Value *getValue() const { return V; }
418 virtual bool isLoopInvariant(const Loop *L) const;
419 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
420 return false; // not computable
423 virtual const Type *getType() const;
425 virtual void print(std::ostream &OS) const;
427 /// Methods for support type inquiry through isa, cast, and dyn_cast:
428 static inline bool classof(const SCEVUnknown *S) { return true; }
429 static inline bool classof(const SCEV *S) {
430 return S->getSCEVType() == scUnknown;
434 /// SCEVVisitor - This class defines a simple visitor class that may be used
435 /// for various SCEV analysis purposes.
436 template<typename SC, typename RetVal=void>
438 RetVal visit(SCEV *S) {
439 switch (S->getSCEVType()) {
441 return ((SC*)this)->visitConstant((SCEVConstant*)S);
443 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
445 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
447 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
449 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
451 return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S);
453 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
455 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
456 case scCouldNotCompute:
457 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
459 assert(0 && "Unknown SCEV type!");
464 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
465 assert(0 && "Invalid use of SCEVCouldNotCompute!");