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;
65 void print(std::ostream *OS) const { if (OS) print(*OS); }
67 /// Methods for support type inquiry through isa, cast, and dyn_cast:
68 static inline bool classof(const SCEVConstant *S) { return true; }
69 static inline bool classof(const SCEV *S) {
70 return S->getSCEVType() == scConstant;
74 //===--------------------------------------------------------------------===//
75 /// SCEVTruncateExpr - This class represents a truncation of an integer value
76 /// to a smaller integer value.
78 class SCEVTruncateExpr : public SCEV {
81 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
82 virtual ~SCEVTruncateExpr();
84 /// get method - This just gets and returns a new SCEVTruncate object
86 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
88 const SCEVHandle &getOperand() const { return Op; }
89 virtual const Type *getType() const { return Ty; }
91 virtual bool isLoopInvariant(const Loop *L) const {
92 return Op->isLoopInvariant(L);
95 virtual bool hasComputableLoopEvolution(const Loop *L) const {
96 return Op->hasComputableLoopEvolution(L);
99 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
100 const SCEVHandle &Conc) const {
101 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
107 /// getValueRange - Return the tightest constant bounds that this value is
108 /// known to have. This method is only valid on integer SCEV objects.
109 virtual ConstantRange getValueRange() const;
111 virtual void print(std::ostream &OS) const;
112 void print(std::ostream *OS) const { if (OS) print(*OS); }
114 /// Methods for support type inquiry through isa, cast, and dyn_cast:
115 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
116 static inline bool classof(const SCEV *S) {
117 return S->getSCEVType() == scTruncate;
121 //===--------------------------------------------------------------------===//
122 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
123 /// integer value to a larger integer value.
125 class SCEVZeroExtendExpr : public SCEV {
128 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
129 virtual ~SCEVZeroExtendExpr();
131 /// get method - This just gets and returns a new SCEVZeroExtend object
133 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
135 const SCEVHandle &getOperand() const { return Op; }
136 virtual const Type *getType() const { return Ty; }
138 virtual bool isLoopInvariant(const Loop *L) const {
139 return Op->isLoopInvariant(L);
142 virtual bool hasComputableLoopEvolution(const Loop *L) const {
143 return Op->hasComputableLoopEvolution(L);
146 /// getValueRange - Return the tightest constant bounds that this value is
147 /// known to have. This method is only valid on integer SCEV objects.
148 virtual ConstantRange getValueRange() const;
150 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
151 const SCEVHandle &Conc) const {
152 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
158 virtual void print(std::ostream &OS) const;
159 void print(std::ostream *OS) const { if (OS) print(*OS); }
161 /// Methods for support type inquiry through isa, cast, and dyn_cast:
162 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
163 static inline bool classof(const SCEV *S) {
164 return S->getSCEVType() == scZeroExtend;
169 //===--------------------------------------------------------------------===//
170 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
173 class SCEVCommutativeExpr : public SCEV {
174 std::vector<SCEVHandle> Operands;
177 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
179 Operands.reserve(ops.size());
180 Operands.insert(Operands.end(), ops.begin(), ops.end());
182 ~SCEVCommutativeExpr();
185 unsigned getNumOperands() const { return Operands.size(); }
186 const SCEVHandle &getOperand(unsigned i) const {
187 assert(i < Operands.size() && "Operand index out of range!");
191 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
192 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
193 op_iterator op_begin() const { return Operands.begin(); }
194 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 - Commutative 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))
217 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
218 const SCEVHandle &Conc) const;
220 virtual const char *getOperationStr() const = 0;
222 virtual const Type *getType() const { return getOperand(0)->getType(); }
223 virtual void print(std::ostream &OS) const;
224 void print(std::ostream *OS) const { if (OS) print(*OS); }
226 /// Methods for support type inquiry through isa, cast, and dyn_cast:
227 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
228 static inline bool classof(const SCEV *S) {
229 return S->getSCEVType() == scAddExpr ||
230 S->getSCEVType() == scMulExpr;
235 //===--------------------------------------------------------------------===//
236 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
238 class SCEVAddExpr : public SCEVCommutativeExpr {
239 SCEVAddExpr(const std::vector<SCEVHandle> &ops)
240 : SCEVCommutativeExpr(scAddExpr, 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 static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
254 const SCEVHandle &Op2) {
255 std::vector<SCEVHandle> Ops;
262 virtual const char *getOperationStr() const { return " + "; }
264 /// Methods for support type inquiry through isa, cast, and dyn_cast:
265 static inline bool classof(const SCEVAddExpr *S) { return true; }
266 static inline bool classof(const SCEV *S) {
267 return S->getSCEVType() == scAddExpr;
271 //===--------------------------------------------------------------------===//
272 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
274 class SCEVMulExpr : public SCEVCommutativeExpr {
275 SCEVMulExpr(const std::vector<SCEVHandle> &ops)
276 : SCEVCommutativeExpr(scMulExpr, ops) {
280 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
282 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
283 std::vector<SCEVHandle> Ops;
289 virtual const char *getOperationStr() const { return " * "; }
291 /// Methods for support type inquiry through isa, cast, and dyn_cast:
292 static inline bool classof(const SCEVMulExpr *S) { return true; }
293 static inline bool classof(const SCEV *S) {
294 return S->getSCEVType() == scMulExpr;
299 //===--------------------------------------------------------------------===//
300 /// SCEVSDivExpr - This class represents a binary signed division operation.
302 class SCEVSDivExpr : public SCEV {
304 SCEVSDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
305 : SCEV(scSDivExpr), LHS(lhs), RHS(rhs) {}
307 virtual ~SCEVSDivExpr();
309 /// get method - This just gets and returns a new SCEVSDiv object.
311 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
313 const SCEVHandle &getLHS() const { return LHS; }
314 const SCEVHandle &getRHS() const { return RHS; }
316 virtual bool isLoopInvariant(const Loop *L) const {
317 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
320 virtual bool hasComputableLoopEvolution(const Loop *L) const {
321 return LHS->hasComputableLoopEvolution(L) &&
322 RHS->hasComputableLoopEvolution(L);
325 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
326 const SCEVHandle &Conc) const {
327 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
328 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
329 if (L == LHS && R == RHS)
336 virtual const Type *getType() const;
338 void print(std::ostream &OS) const;
339 void print(std::ostream *OS) const { if (OS) print(*OS); }
341 /// Methods for support type inquiry through isa, cast, and dyn_cast:
342 static inline bool classof(const SCEVSDivExpr *S) { return true; }
343 static inline bool classof(const SCEV *S) {
344 return S->getSCEVType() == scSDivExpr;
349 //===--------------------------------------------------------------------===//
350 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
351 /// count of the specified loop.
353 /// All operands of an AddRec are required to be loop invariant.
355 class SCEVAddRecExpr : public SCEV {
356 std::vector<SCEVHandle> Operands;
359 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
360 : SCEV(scAddRecExpr), Operands(ops), L(l) {
361 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
362 assert(Operands[i]->isLoopInvariant(l) &&
363 "Operands of AddRec must be loop-invariant!");
367 static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
369 static SCEVHandle get(std::vector<SCEVHandle> &Operands,
371 static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
373 std::vector<SCEVHandle> NewOp(Operands);
374 return get(NewOp, L);
377 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
378 op_iterator op_begin() const { return Operands.begin(); }
379 op_iterator op_end() const { return Operands.end(); }
381 unsigned getNumOperands() const { return Operands.size(); }
382 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
383 const SCEVHandle &getStart() const { return Operands[0]; }
384 const Loop *getLoop() const { return L; }
387 /// getStepRecurrence - This method constructs and returns the recurrence
388 /// indicating how much this expression steps by. If this is a polynomial
389 /// of degree N, it returns a chrec of degree N-1.
390 SCEVHandle getStepRecurrence() const {
391 if (getNumOperands() == 2) return getOperand(1);
392 return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
396 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
397 if (L == QL) return true;
401 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
403 virtual const Type *getType() const { return Operands[0]->getType(); }
405 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
406 /// an expressions A+B*x where A and B are loop invariant values.
407 bool isAffine() const {
408 // We know that the start value is invariant. This expression is thus
409 // affine iff the step is also invariant.
410 return getNumOperands() == 2;
413 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
414 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
415 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
416 bool isQuadratic() const {
417 return getNumOperands() == 3;
420 /// evaluateAtIteration - Return the value of this chain of recurrences at
421 /// the specified iteration number.
422 SCEVHandle evaluateAtIteration(SCEVHandle It) const;
424 /// getNumIterationsInRange - Return the number of iterations of this loop
425 /// that produce values in the specified constant range. Another way of
426 /// looking at this is that it returns the first iteration number where the
427 /// value is not in the condition, thus computing the exit count. If the
428 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
430 SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
432 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
433 const SCEVHandle &Conc) const;
435 virtual void print(std::ostream &OS) const;
436 void print(std::ostream *OS) const { if (OS) print(*OS); }
438 /// Methods for support type inquiry through isa, cast, and dyn_cast:
439 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
440 static inline bool classof(const SCEV *S) {
441 return S->getSCEVType() == scAddRecExpr;
445 //===--------------------------------------------------------------------===//
446 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
447 /// value, and only represent it as it's LLVM Value. This is the "bottom"
448 /// value for the analysis.
450 class SCEVUnknown : public SCEV {
452 SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
457 /// get method - For SCEVUnknown, this just gets and returns a new
459 static SCEVHandle get(Value *V);
461 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
462 /// specified signed integer value and return a SCEV for the constant.
463 static SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
465 Value *getValue() const { return V; }
467 virtual bool isLoopInvariant(const Loop *L) const;
468 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
469 return false; // not computable
472 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
473 const SCEVHandle &Conc) const {
474 if (&*Sym == this) return Conc;
478 virtual const Type *getType() const;
480 virtual void print(std::ostream &OS) const;
481 void print(std::ostream *OS) const { if (OS) print(*OS); }
483 /// Methods for support type inquiry through isa, cast, and dyn_cast:
484 static inline bool classof(const SCEVUnknown *S) { return true; }
485 static inline bool classof(const SCEV *S) {
486 return S->getSCEVType() == scUnknown;
490 /// SCEVVisitor - This class defines a simple visitor class that may be used
491 /// for various SCEV analysis purposes.
492 template<typename SC, typename RetVal=void>
494 RetVal visit(SCEV *S) {
495 switch (S->getSCEVType()) {
497 return ((SC*)this)->visitConstant((SCEVConstant*)S);
499 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
501 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
503 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
505 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
507 return ((SC*)this)->visitSDivExpr((SCEVSDivExpr*)S);
509 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
511 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
512 case scCouldNotCompute:
513 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
515 assert(0 && "Unknown SCEV type!");
520 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
521 assert(0 && "Invalid use of SCEVCouldNotCompute!");