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"
25 // These should be ordered in terms of increasing complexity to make the
27 scConstant, scTruncate, scZeroExtend, scAddExpr, scMulExpr, scSDivExpr,
28 scAddRecExpr, scUnknown, scCouldNotCompute
31 //===--------------------------------------------------------------------===//
32 /// SCEVConstant - This class represents a constant integer value.
34 class SCEVConstant : public SCEV {
36 SCEVConstant(ConstantInt *v) : SCEV(scConstant), V(v) {}
38 virtual ~SCEVConstant();
40 /// get method - This just gets and returns a new SCEVConstant object.
42 static SCEVHandle get(ConstantInt *V);
44 ConstantInt *getValue() const { return V; }
46 /// getValueRange - Return the tightest constant bounds that this value is
47 /// known to have. This method is only valid on integer SCEV objects.
48 virtual ConstantRange getValueRange() const;
50 virtual bool isLoopInvariant(const Loop *L) const {
54 virtual bool hasComputableLoopEvolution(const Loop *L) const {
55 return false; // Not loop variant
58 virtual const Type *getType() const;
60 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
61 const SCEVHandle &Conc) const {
65 virtual void print(std::ostream &OS) const;
66 void print(std::ostream *OS) const { if (OS) print(*OS); }
68 /// Methods for support type inquiry through isa, cast, and dyn_cast:
69 static inline bool classof(const SCEVConstant *S) { return true; }
70 static inline bool classof(const SCEV *S) {
71 return S->getSCEVType() == scConstant;
75 //===--------------------------------------------------------------------===//
76 /// SCEVTruncateExpr - This class represents a truncation of an integer value
77 /// to a smaller integer value.
79 class SCEVTruncateExpr : public SCEV {
82 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
83 virtual ~SCEVTruncateExpr();
85 /// get method - This just gets and returns a new SCEVTruncate object
87 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
89 const SCEVHandle &getOperand() const { return Op; }
90 virtual const Type *getType() const { return Ty; }
92 virtual bool isLoopInvariant(const Loop *L) const {
93 return Op->isLoopInvariant(L);
96 virtual bool hasComputableLoopEvolution(const Loop *L) const {
97 return Op->hasComputableLoopEvolution(L);
100 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
101 const SCEVHandle &Conc) const {
102 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
108 /// getValueRange - Return the tightest constant bounds that this value is
109 /// known to have. This method is only valid on integer SCEV objects.
110 virtual ConstantRange getValueRange() const;
112 virtual void print(std::ostream &OS) const;
113 void print(std::ostream *OS) const { if (OS) print(*OS); }
115 /// Methods for support type inquiry through isa, cast, and dyn_cast:
116 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
117 static inline bool classof(const SCEV *S) {
118 return S->getSCEVType() == scTruncate;
122 //===--------------------------------------------------------------------===//
123 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
124 /// integer value to a larger integer value.
126 class SCEVZeroExtendExpr : public SCEV {
129 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
130 virtual ~SCEVZeroExtendExpr();
132 /// get method - This just gets and returns a new SCEVZeroExtend object
134 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
136 const SCEVHandle &getOperand() const { return Op; }
137 virtual const Type *getType() const { return Ty; }
139 virtual bool isLoopInvariant(const Loop *L) const {
140 return Op->isLoopInvariant(L);
143 virtual bool hasComputableLoopEvolution(const Loop *L) const {
144 return Op->hasComputableLoopEvolution(L);
147 /// getValueRange - Return the tightest constant bounds that this value is
148 /// known to have. This method is only valid on integer SCEV objects.
149 virtual ConstantRange getValueRange() const;
151 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
152 const SCEVHandle &Conc) const {
153 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
159 virtual void print(std::ostream &OS) const;
160 void print(std::ostream *OS) const { if (OS) print(*OS); }
162 /// Methods for support type inquiry through isa, cast, and dyn_cast:
163 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
164 static inline bool classof(const SCEV *S) {
165 return S->getSCEVType() == scZeroExtend;
170 //===--------------------------------------------------------------------===//
171 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
174 class SCEVCommutativeExpr : public SCEV {
175 std::vector<SCEVHandle> Operands;
178 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
180 Operands.reserve(ops.size());
181 Operands.insert(Operands.end(), ops.begin(), ops.end());
183 ~SCEVCommutativeExpr();
186 unsigned getNumOperands() const { return Operands.size(); }
187 const SCEVHandle &getOperand(unsigned i) const {
188 assert(i < Operands.size() && "Operand index out of range!");
192 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
193 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
194 op_iterator op_begin() const { return Operands.begin(); }
195 op_iterator op_end() const { return Operands.end(); }
198 virtual bool isLoopInvariant(const Loop *L) const {
199 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
200 if (!getOperand(i)->isLoopInvariant(L)) return false;
204 // hasComputableLoopEvolution - Commutative expressions have computable loop
205 // evolutions iff they have at least one operand that varies with the loop,
206 // but that all varying operands are computable.
207 virtual bool hasComputableLoopEvolution(const Loop *L) const {
208 bool HasVarying = false;
209 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
210 if (!getOperand(i)->isLoopInvariant(L))
211 if (getOperand(i)->hasComputableLoopEvolution(L))
218 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
219 const SCEVHandle &Conc) const;
221 virtual const char *getOperationStr() const = 0;
223 virtual const Type *getType() const { return getOperand(0)->getType(); }
224 virtual void print(std::ostream &OS) const;
225 void print(std::ostream *OS) const { if (OS) print(*OS); }
227 /// Methods for support type inquiry through isa, cast, and dyn_cast:
228 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
229 static inline bool classof(const SCEV *S) {
230 return S->getSCEVType() == scAddExpr ||
231 S->getSCEVType() == scMulExpr;
236 //===--------------------------------------------------------------------===//
237 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
239 class SCEVAddExpr : public SCEVCommutativeExpr {
240 SCEVAddExpr(const std::vector<SCEVHandle> &ops)
241 : SCEVCommutativeExpr(scAddExpr, ops) {
245 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
247 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
248 std::vector<SCEVHandle> Ops;
254 static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
255 const SCEVHandle &Op2) {
256 std::vector<SCEVHandle> Ops;
263 virtual const char *getOperationStr() const { return " + "; }
265 /// Methods for support type inquiry through isa, cast, and dyn_cast:
266 static inline bool classof(const SCEVAddExpr *S) { return true; }
267 static inline bool classof(const SCEV *S) {
268 return S->getSCEVType() == scAddExpr;
272 //===--------------------------------------------------------------------===//
273 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
275 class SCEVMulExpr : public SCEVCommutativeExpr {
276 SCEVMulExpr(const std::vector<SCEVHandle> &ops)
277 : SCEVCommutativeExpr(scMulExpr, ops) {
281 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
283 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
284 std::vector<SCEVHandle> Ops;
290 virtual const char *getOperationStr() const { return " * "; }
292 /// Methods for support type inquiry through isa, cast, and dyn_cast:
293 static inline bool classof(const SCEVMulExpr *S) { return true; }
294 static inline bool classof(const SCEV *S) {
295 return S->getSCEVType() == scMulExpr;
300 //===--------------------------------------------------------------------===//
301 /// SCEVSDivExpr - This class represents a binary signed division operation.
303 class SCEVSDivExpr : public SCEV {
305 SCEVSDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
306 : SCEV(scSDivExpr), LHS(lhs), RHS(rhs) {}
308 virtual ~SCEVSDivExpr();
310 /// get method - This just gets and returns a new SCEVSDiv object.
312 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
314 const SCEVHandle &getLHS() const { return LHS; }
315 const SCEVHandle &getRHS() const { return RHS; }
317 virtual bool isLoopInvariant(const Loop *L) const {
318 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
321 virtual bool hasComputableLoopEvolution(const Loop *L) const {
322 return LHS->hasComputableLoopEvolution(L) &&
323 RHS->hasComputableLoopEvolution(L);
326 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
327 const SCEVHandle &Conc) const {
328 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
329 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
330 if (L == LHS && R == RHS)
337 virtual const Type *getType() const;
339 void print(std::ostream &OS) const;
340 void print(std::ostream *OS) const { if (OS) print(*OS); }
342 /// Methods for support type inquiry through isa, cast, and dyn_cast:
343 static inline bool classof(const SCEVSDivExpr *S) { return true; }
344 static inline bool classof(const SCEV *S) {
345 return S->getSCEVType() == scSDivExpr;
350 //===--------------------------------------------------------------------===//
351 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
352 /// count of the specified loop.
354 /// All operands of an AddRec are required to be loop invariant.
356 class SCEVAddRecExpr : public SCEV {
357 std::vector<SCEVHandle> Operands;
360 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
361 : SCEV(scAddRecExpr), Operands(ops), L(l) {
362 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
363 assert(Operands[i]->isLoopInvariant(l) &&
364 "Operands of AddRec must be loop-invariant!");
368 static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
370 static SCEVHandle get(std::vector<SCEVHandle> &Operands,
372 static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
374 std::vector<SCEVHandle> NewOp(Operands);
375 return get(NewOp, L);
378 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
379 op_iterator op_begin() const { return Operands.begin(); }
380 op_iterator op_end() const { return Operands.end(); }
382 unsigned getNumOperands() const { return Operands.size(); }
383 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
384 const SCEVHandle &getStart() const { return Operands[0]; }
385 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 SCEVHandle getStepRecurrence() const {
392 if (getNumOperands() == 2) return getOperand(1);
393 return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
397 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
398 if (L == QL) return true;
402 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
404 virtual const Type *getType() const { return Operands[0]->getType(); }
406 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
407 /// an expressions A+B*x where A and B are loop invariant values.
408 bool isAffine() const {
409 // We know that the start value is invariant. This expression is thus
410 // affine iff the step is also invariant.
411 return getNumOperands() == 2;
414 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
415 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
416 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
417 bool isQuadratic() const {
418 return getNumOperands() == 3;
421 /// evaluateAtIteration - Return the value of this chain of recurrences at
422 /// the specified iteration number.
423 SCEVHandle evaluateAtIteration(SCEVHandle It) const;
425 /// getNumIterationsInRange - Return the number of iterations of this loop
426 /// that produce values in the specified constant range. Another way of
427 /// looking at this is that it returns the first iteration number where the
428 /// value is not in the condition, thus computing the exit count. If the
429 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
430 /// returned. The isSigned parameter indicates whether the ConstantRange
431 /// should be treated as signed or unsigned.
432 SCEVHandle getNumIterationsInRange(ConstantRange Range,
433 bool isSigned) const;
435 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
436 const SCEVHandle &Conc) const;
438 virtual void print(std::ostream &OS) const;
439 void print(std::ostream *OS) const { if (OS) print(*OS); }
441 /// Methods for support type inquiry through isa, cast, and dyn_cast:
442 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
443 static inline bool classof(const SCEV *S) {
444 return S->getSCEVType() == scAddRecExpr;
448 //===--------------------------------------------------------------------===//
449 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
450 /// value, and only represent it as it's LLVM Value. This is the "bottom"
451 /// value for the analysis.
453 class SCEVUnknown : public SCEV {
455 SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
460 /// get method - For SCEVUnknown, this just gets and returns a new
462 static SCEVHandle get(Value *V);
464 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
465 /// specified signed integer value and return a SCEV for the constant.
466 static SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
467 static SCEVHandle getIntegerSCEV(const APInt& Val);
469 Value *getValue() const { return V; }
471 virtual bool isLoopInvariant(const Loop *L) const;
472 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
473 return false; // not computable
476 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
477 const SCEVHandle &Conc) const {
478 if (&*Sym == this) return Conc;
482 virtual const Type *getType() const;
484 virtual void print(std::ostream &OS) const;
485 void print(std::ostream *OS) const { if (OS) print(*OS); }
487 /// Methods for support type inquiry through isa, cast, and dyn_cast:
488 static inline bool classof(const SCEVUnknown *S) { return true; }
489 static inline bool classof(const SCEV *S) {
490 return S->getSCEVType() == scUnknown;
494 /// SCEVVisitor - This class defines a simple visitor class that may be used
495 /// for various SCEV analysis purposes.
496 template<typename SC, typename RetVal=void>
498 RetVal visit(SCEV *S) {
499 switch (S->getSCEVType()) {
501 return ((SC*)this)->visitConstant((SCEVConstant*)S);
503 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
505 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
507 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
509 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
511 return ((SC*)this)->visitSDivExpr((SCEVSDivExpr*)S);
513 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
515 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
516 case scCouldNotCompute:
517 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
519 assert(0 && "Unknown SCEV type!");
524 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
525 assert(0 && "Invalid use of SCEVCouldNotCompute!");