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 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
60 const SCEVHandle &Conc) const {
64 virtual void print(std::ostream &OS) const;
66 /// Methods for support type inquiry through isa, cast, and dyn_cast:
67 static inline bool classof(const SCEVConstant *S) { return true; }
68 static inline bool classof(const SCEV *S) {
69 return S->getSCEVType() == scConstant;
73 //===--------------------------------------------------------------------===//
74 /// SCEVTruncateExpr - This class represents a truncation of an integer value
75 /// to a smaller integer value.
77 class SCEVTruncateExpr : public SCEV {
80 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
81 virtual ~SCEVTruncateExpr();
83 /// get method - This just gets and returns a new SCEVTruncate object
85 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
87 const SCEVHandle &getOperand() const { return Op; }
88 virtual const Type *getType() const { return Ty; }
90 virtual bool isLoopInvariant(const Loop *L) const {
91 return Op->isLoopInvariant(L);
94 virtual bool hasComputableLoopEvolution(const Loop *L) const {
95 return Op->hasComputableLoopEvolution(L);
98 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
99 const SCEVHandle &Conc) const {
100 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
106 /// getValueRange - Return the tightest constant bounds that this value is
107 /// known to have. This method is only valid on integer SCEV objects.
108 virtual ConstantRange getValueRange() const;
110 virtual void print(std::ostream &OS) const;
112 /// Methods for support type inquiry through isa, cast, and dyn_cast:
113 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
114 static inline bool classof(const SCEV *S) {
115 return S->getSCEVType() == scTruncate;
119 //===--------------------------------------------------------------------===//
120 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
121 /// integer value to a larger integer value.
123 class SCEVZeroExtendExpr : public SCEV {
126 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
127 virtual ~SCEVZeroExtendExpr();
129 /// get method - This just gets and returns a new SCEVZeroExtend object
131 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
133 const SCEVHandle &getOperand() const { return Op; }
134 virtual const Type *getType() const { return Ty; }
136 virtual bool isLoopInvariant(const Loop *L) const {
137 return Op->isLoopInvariant(L);
140 virtual bool hasComputableLoopEvolution(const Loop *L) const {
141 return Op->hasComputableLoopEvolution(L);
144 /// getValueRange - Return the tightest constant bounds that this value is
145 /// known to have. This method is only valid on integer SCEV objects.
146 virtual ConstantRange getValueRange() const;
148 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
149 const SCEVHandle &Conc) const {
150 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
156 virtual void print(std::ostream &OS) const;
158 /// Methods for support type inquiry through isa, cast, and dyn_cast:
159 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
160 static inline bool classof(const SCEV *S) {
161 return S->getSCEVType() == scZeroExtend;
166 //===--------------------------------------------------------------------===//
167 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
170 class SCEVCommutativeExpr : public SCEV {
171 std::vector<SCEVHandle> Operands;
174 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
176 Operands.reserve(ops.size());
177 Operands.insert(Operands.end(), ops.begin(), ops.end());
179 ~SCEVCommutativeExpr();
182 unsigned getNumOperands() const { return Operands.size(); }
183 const SCEVHandle &getOperand(unsigned i) const {
184 assert(i < Operands.size() && "Operand index out of range!");
188 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
189 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
190 op_iterator op_begin() const { return Operands.begin(); }
191 op_iterator op_end() const { return Operands.end(); }
194 virtual bool isLoopInvariant(const Loop *L) const {
195 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
196 if (!getOperand(i)->isLoopInvariant(L)) return false;
200 virtual bool hasComputableLoopEvolution(const Loop *L) const {
201 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
202 if (getOperand(i)->hasComputableLoopEvolution(L)) return true;
206 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
207 const SCEVHandle &Conc) const;
209 virtual const char *getOperationStr() const = 0;
211 virtual const Type *getType() const { return getOperand(0)->getType(); }
212 virtual void print(std::ostream &OS) const;
214 /// Methods for support type inquiry through isa, cast, and dyn_cast:
215 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
216 static inline bool classof(const SCEV *S) {
217 return S->getSCEVType() == scAddExpr ||
218 S->getSCEVType() == scMulExpr;
223 //===--------------------------------------------------------------------===//
224 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
226 class SCEVAddExpr : public SCEVCommutativeExpr {
227 SCEVAddExpr(const std::vector<SCEVHandle> &ops)
228 : SCEVCommutativeExpr(scAddExpr, ops) {
232 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
234 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
235 std::vector<SCEVHandle> Ops;
241 static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
242 const SCEVHandle &Op2) {
243 std::vector<SCEVHandle> Ops;
250 virtual const char *getOperationStr() const { return " + "; }
252 /// Methods for support type inquiry through isa, cast, and dyn_cast:
253 static inline bool classof(const SCEVAddExpr *S) { return true; }
254 static inline bool classof(const SCEV *S) {
255 return S->getSCEVType() == scAddExpr;
259 //===--------------------------------------------------------------------===//
260 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
262 class SCEVMulExpr : public SCEVCommutativeExpr {
263 SCEVMulExpr(const std::vector<SCEVHandle> &ops)
264 : SCEVCommutativeExpr(scMulExpr, ops) {
268 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
270 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
271 std::vector<SCEVHandle> Ops;
277 virtual const char *getOperationStr() const { return " * "; }
279 /// Methods for support type inquiry through isa, cast, and dyn_cast:
280 static inline bool classof(const SCEVMulExpr *S) { return true; }
281 static inline bool classof(const SCEV *S) {
282 return S->getSCEVType() == scMulExpr;
287 //===--------------------------------------------------------------------===//
288 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
290 class SCEVUDivExpr : public SCEV {
292 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
293 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
295 virtual ~SCEVUDivExpr();
297 /// get method - This just gets and returns a new SCEVUDiv object.
299 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
301 const SCEVHandle &getLHS() const { return LHS; }
302 const SCEVHandle &getRHS() const { return RHS; }
304 virtual bool isLoopInvariant(const Loop *L) const {
305 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
308 virtual bool hasComputableLoopEvolution(const Loop *L) const {
309 return LHS->hasComputableLoopEvolution(L) &&
310 RHS->hasComputableLoopEvolution(L);
313 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
314 const SCEVHandle &Conc) const {
315 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
316 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
317 if (L == LHS && R == RHS)
324 virtual const Type *getType() const;
326 void print(std::ostream &OS) const;
328 /// Methods for support type inquiry through isa, cast, and dyn_cast:
329 static inline bool classof(const SCEVUDivExpr *S) { return true; }
330 static inline bool classof(const SCEV *S) {
331 return S->getSCEVType() == scUDivExpr;
336 //===--------------------------------------------------------------------===//
337 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
338 /// count of the specified loop.
340 /// All operands of an AddRec are required to be loop invariant.
342 class SCEVAddRecExpr : public SCEV {
343 std::vector<SCEVHandle> Operands;
346 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
347 : SCEV(scAddRecExpr), Operands(ops), L(l) {
348 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
349 assert(Operands[i]->isLoopInvariant(l) &&
350 "Operands of AddRec must be loop-invariant!");
354 static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
356 static SCEVHandle get(std::vector<SCEVHandle> &Operands,
358 static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
360 std::vector<SCEVHandle> NewOp(Operands);
361 return get(NewOp, L);
364 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
365 op_iterator op_begin() const { return Operands.begin(); }
366 op_iterator op_end() const { return Operands.end(); }
368 unsigned getNumOperands() const { return Operands.size(); }
369 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
370 const SCEVHandle &getStart() const { return Operands[0]; }
371 const Loop *getLoop() const { return L; }
374 /// getStepRecurrence - This method constructs and returns the recurrence
375 /// indicating how much this expression steps by. If this is a polynomial
376 /// of degree N, it returns a chrec of degree N-1.
377 SCEVHandle getStepRecurrence() const {
378 if (getNumOperands() == 2) return getOperand(1);
379 return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
383 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
384 if (L == QL) return true;
385 /// FIXME: What if the start or step value a recurrence for the specified
390 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
392 virtual const Type *getType() const { return Operands[0]->getType(); }
394 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
395 /// an expressions A+B*x where A and B are loop invariant values.
396 bool isAffine() const {
397 // We know that the start value is invariant. This expression is thus
398 // affine iff the step is also invariant.
399 return getNumOperands() == 2;
402 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
403 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
404 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
405 bool isQuadratic() const {
406 return getNumOperands() == 3;
409 /// evaluateAtIteration - Return the value of this chain of recurrences at
410 /// the specified iteration number.
411 SCEVHandle evaluateAtIteration(SCEVHandle It) const;
413 /// getNumIterationsInRange - Return the number of iterations of this loop
414 /// that produce values in the specified constant range. Another way of
415 /// looking at this is that it returns the first iteration number where the
416 /// value is not in the condition, thus computing the exit count. If the
417 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
419 SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
421 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
422 const SCEVHandle &Conc) const;
424 virtual void print(std::ostream &OS) const;
426 /// Methods for support type inquiry through isa, cast, and dyn_cast:
427 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
428 static inline bool classof(const SCEV *S) {
429 return S->getSCEVType() == scAddRecExpr;
433 //===--------------------------------------------------------------------===//
434 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
435 /// value, and only represent it as it's LLVM Value. This is the "bottom"
436 /// value for the analysis.
438 class SCEVUnknown : public SCEV {
440 SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
445 /// get method - For SCEVUnknown, this just gets and returns a new
447 static SCEVHandle get(Value *V);
449 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
450 /// specified signed integer value and return a SCEV for the constant.
451 static SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
453 Value *getValue() const { return V; }
455 virtual bool isLoopInvariant(const Loop *L) const;
456 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
457 return false; // not computable
460 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
461 const SCEVHandle &Conc) const {
462 if (&*Sym == this) return Conc;
466 virtual const Type *getType() const;
468 virtual void print(std::ostream &OS) const;
470 /// Methods for support type inquiry through isa, cast, and dyn_cast:
471 static inline bool classof(const SCEVUnknown *S) { return true; }
472 static inline bool classof(const SCEV *S) {
473 return S->getSCEVType() == scUnknown;
477 /// SCEVVisitor - This class defines a simple visitor class that may be used
478 /// for various SCEV analysis purposes.
479 template<typename SC, typename RetVal=void>
481 RetVal visit(SCEV *S) {
482 switch (S->getSCEVType()) {
484 return ((SC*)this)->visitConstant((SCEVConstant*)S);
486 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
488 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
490 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
492 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
494 return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S);
496 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
498 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
499 case scCouldNotCompute:
500 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
502 assert(0 && "Unknown SCEV type!");
507 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
508 assert(0 && "Invalid use of SCEVCouldNotCompute!");