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, scSDivExpr,
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 // hasComputableLoopEvolution - Commutative expressions have computable loop
201 // evolutions iff they have at least one operand that varies with the loop,
202 // but that all varying operands are computable.
203 virtual bool hasComputableLoopEvolution(const Loop *L) const {
204 bool HasVarying = false;
205 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
206 if (!getOperand(i)->isLoopInvariant(L))
207 if (getOperand(i)->hasComputableLoopEvolution(L))
214 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
215 const SCEVHandle &Conc) const;
217 virtual const char *getOperationStr() const = 0;
219 virtual const Type *getType() const { return getOperand(0)->getType(); }
220 virtual void print(std::ostream &OS) const;
222 /// Methods for support type inquiry through isa, cast, and dyn_cast:
223 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
224 static inline bool classof(const SCEV *S) {
225 return S->getSCEVType() == scAddExpr ||
226 S->getSCEVType() == scMulExpr;
231 //===--------------------------------------------------------------------===//
232 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
234 class SCEVAddExpr : public SCEVCommutativeExpr {
235 SCEVAddExpr(const std::vector<SCEVHandle> &ops)
236 : SCEVCommutativeExpr(scAddExpr, ops) {
240 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
242 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
243 std::vector<SCEVHandle> Ops;
249 static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
250 const SCEVHandle &Op2) {
251 std::vector<SCEVHandle> Ops;
258 virtual const char *getOperationStr() const { return " + "; }
260 /// Methods for support type inquiry through isa, cast, and dyn_cast:
261 static inline bool classof(const SCEVAddExpr *S) { return true; }
262 static inline bool classof(const SCEV *S) {
263 return S->getSCEVType() == scAddExpr;
267 //===--------------------------------------------------------------------===//
268 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
270 class SCEVMulExpr : public SCEVCommutativeExpr {
271 SCEVMulExpr(const std::vector<SCEVHandle> &ops)
272 : SCEVCommutativeExpr(scMulExpr, ops) {
276 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
278 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
279 std::vector<SCEVHandle> Ops;
285 virtual const char *getOperationStr() const { return " * "; }
287 /// Methods for support type inquiry through isa, cast, and dyn_cast:
288 static inline bool classof(const SCEVMulExpr *S) { return true; }
289 static inline bool classof(const SCEV *S) {
290 return S->getSCEVType() == scMulExpr;
295 //===--------------------------------------------------------------------===//
296 /// SCEVSDivExpr - This class represents a binary unsigned division operation.
298 class SCEVSDivExpr : public SCEV {
300 SCEVSDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
301 : SCEV(scSDivExpr), LHS(lhs), RHS(rhs) {}
303 virtual ~SCEVSDivExpr();
305 /// get method - This just gets and returns a new SCEVSDiv object.
307 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
309 const SCEVHandle &getLHS() const { return LHS; }
310 const SCEVHandle &getRHS() const { return RHS; }
312 virtual bool isLoopInvariant(const Loop *L) const {
313 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
316 virtual bool hasComputableLoopEvolution(const Loop *L) const {
317 return LHS->hasComputableLoopEvolution(L) &&
318 RHS->hasComputableLoopEvolution(L);
321 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
322 const SCEVHandle &Conc) const {
323 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
324 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
325 if (L == LHS && R == RHS)
332 virtual const Type *getType() const;
334 void print(std::ostream &OS) const;
336 /// Methods for support type inquiry through isa, cast, and dyn_cast:
337 static inline bool classof(const SCEVSDivExpr *S) { return true; }
338 static inline bool classof(const SCEV *S) {
339 return S->getSCEVType() == scSDivExpr;
344 //===--------------------------------------------------------------------===//
345 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
346 /// count of the specified loop.
348 /// All operands of an AddRec are required to be loop invariant.
350 class SCEVAddRecExpr : public SCEV {
351 std::vector<SCEVHandle> Operands;
354 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
355 : SCEV(scAddRecExpr), Operands(ops), L(l) {
356 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
357 assert(Operands[i]->isLoopInvariant(l) &&
358 "Operands of AddRec must be loop-invariant!");
362 static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
364 static SCEVHandle get(std::vector<SCEVHandle> &Operands,
366 static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
368 std::vector<SCEVHandle> NewOp(Operands);
369 return get(NewOp, L);
372 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
373 op_iterator op_begin() const { return Operands.begin(); }
374 op_iterator op_end() const { return Operands.end(); }
376 unsigned getNumOperands() const { return Operands.size(); }
377 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
378 const SCEVHandle &getStart() const { return Operands[0]; }
379 const Loop *getLoop() const { return L; }
382 /// getStepRecurrence - This method constructs and returns the recurrence
383 /// indicating how much this expression steps by. If this is a polynomial
384 /// of degree N, it returns a chrec of degree N-1.
385 SCEVHandle getStepRecurrence() const {
386 if (getNumOperands() == 2) return getOperand(1);
387 return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
391 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
392 if (L == QL) return true;
396 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
398 virtual const Type *getType() const { return Operands[0]->getType(); }
400 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
401 /// an expressions A+B*x where A and B are loop invariant values.
402 bool isAffine() const {
403 // We know that the start value is invariant. This expression is thus
404 // affine iff the step is also invariant.
405 return getNumOperands() == 2;
408 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
409 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
410 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
411 bool isQuadratic() const {
412 return getNumOperands() == 3;
415 /// evaluateAtIteration - Return the value of this chain of recurrences at
416 /// the specified iteration number.
417 SCEVHandle evaluateAtIteration(SCEVHandle It) const;
419 /// getNumIterationsInRange - Return the number of iterations of this loop
420 /// that produce values in the specified constant range. Another way of
421 /// looking at this is that it returns the first iteration number where the
422 /// value is not in the condition, thus computing the exit count. If the
423 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
425 SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
427 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
428 const SCEVHandle &Conc) const;
430 virtual void print(std::ostream &OS) const;
432 /// Methods for support type inquiry through isa, cast, and dyn_cast:
433 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
434 static inline bool classof(const SCEV *S) {
435 return S->getSCEVType() == scAddRecExpr;
439 //===--------------------------------------------------------------------===//
440 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
441 /// value, and only represent it as it's LLVM Value. This is the "bottom"
442 /// value for the analysis.
444 class SCEVUnknown : public SCEV {
446 SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
451 /// get method - For SCEVUnknown, this just gets and returns a new
453 static SCEVHandle get(Value *V);
455 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
456 /// specified signed integer value and return a SCEV for the constant.
457 static SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
459 Value *getValue() const { return V; }
461 virtual bool isLoopInvariant(const Loop *L) const;
462 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
463 return false; // not computable
466 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
467 const SCEVHandle &Conc) const {
468 if (&*Sym == this) return Conc;
472 virtual const Type *getType() const;
474 virtual void print(std::ostream &OS) const;
476 /// Methods for support type inquiry through isa, cast, and dyn_cast:
477 static inline bool classof(const SCEVUnknown *S) { return true; }
478 static inline bool classof(const SCEV *S) {
479 return S->getSCEVType() == scUnknown;
483 /// SCEVVisitor - This class defines a simple visitor class that may be used
484 /// for various SCEV analysis purposes.
485 template<typename SC, typename RetVal=void>
487 RetVal visit(SCEV *S) {
488 switch (S->getSCEVType()) {
490 return ((SC*)this)->visitConstant((SCEVConstant*)S);
492 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
494 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
496 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
498 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
500 return ((SC*)this)->visitSDivExpr((SCEVSDivExpr*)S);
502 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
504 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
505 case scCouldNotCompute:
506 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
508 assert(0 && "Unknown SCEV type!");
513 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
514 assert(0 && "Invalid use of SCEVCouldNotCompute!");