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, scSignExtend, scAddExpr, scMulExpr,
28 scSDivExpr, scAddRecExpr, scUnknown, scCouldNotCompute
31 //===--------------------------------------------------------------------===//
32 /// SCEVConstant - This class represents a constant integer value.
34 class SCEVConstant : public SCEV {
36 explicit 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);
43 static SCEVHandle get(const APInt& Val);
45 ConstantInt *getValue() const { return V; }
47 /// getValueRange - Return the tightest constant bounds that this value is
48 /// known to have. This method is only valid on integer SCEV objects.
49 virtual ConstantRange getValueRange() const;
51 virtual bool isLoopInvariant(const Loop *L) const {
55 virtual bool hasComputableLoopEvolution(const Loop *L) const {
56 return false; // Not loop variant
59 virtual const Type *getType() const;
61 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
62 const SCEVHandle &Conc) const {
66 virtual void print(std::ostream &OS) const;
67 void print(std::ostream *OS) const { if (OS) print(*OS); }
69 /// Methods for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const SCEVConstant *S) { return true; }
71 static inline bool classof(const SCEV *S) {
72 return S->getSCEVType() == scConstant;
76 //===--------------------------------------------------------------------===//
77 /// SCEVTruncateExpr - This class represents a truncation of an integer value
78 /// to a smaller integer value.
80 class SCEVTruncateExpr : public SCEV {
83 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
84 virtual ~SCEVTruncateExpr();
86 /// get method - This just gets and returns a new SCEVTruncate object
88 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
90 const SCEVHandle &getOperand() const { return Op; }
91 virtual const Type *getType() const { return Ty; }
93 virtual bool isLoopInvariant(const Loop *L) const {
94 return Op->isLoopInvariant(L);
97 virtual bool hasComputableLoopEvolution(const Loop *L) const {
98 return Op->hasComputableLoopEvolution(L);
101 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
102 const SCEVHandle &Conc) const {
103 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
109 /// getValueRange - Return the tightest constant bounds that this value is
110 /// known to have. This method is only valid on integer SCEV objects.
111 virtual ConstantRange getValueRange() const;
113 virtual void print(std::ostream &OS) const;
114 void print(std::ostream *OS) const { if (OS) print(*OS); }
116 /// Methods for support type inquiry through isa, cast, and dyn_cast:
117 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
118 static inline bool classof(const SCEV *S) {
119 return S->getSCEVType() == scTruncate;
123 //===--------------------------------------------------------------------===//
124 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
125 /// integer value to a larger integer value.
127 class SCEVZeroExtendExpr : public SCEV {
130 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
131 virtual ~SCEVZeroExtendExpr();
133 /// get method - This just gets and returns a new SCEVZeroExtend object
135 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
137 const SCEVHandle &getOperand() const { return Op; }
138 virtual const Type *getType() const { return Ty; }
140 virtual bool isLoopInvariant(const Loop *L) const {
141 return Op->isLoopInvariant(L);
144 virtual bool hasComputableLoopEvolution(const Loop *L) const {
145 return Op->hasComputableLoopEvolution(L);
148 /// getValueRange - Return the tightest constant bounds that this value is
149 /// known to have. This method is only valid on integer SCEV objects.
150 virtual ConstantRange getValueRange() const;
152 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
153 const SCEVHandle &Conc) const {
154 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
160 virtual void print(std::ostream &OS) const;
161 void print(std::ostream *OS) const { if (OS) print(*OS); }
163 /// Methods for support type inquiry through isa, cast, and dyn_cast:
164 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
165 static inline bool classof(const SCEV *S) {
166 return S->getSCEVType() == scZeroExtend;
170 //===--------------------------------------------------------------------===//
171 /// SCEVSignExtendExpr - This class represents a sign extension of a small
172 /// integer value to a larger integer value.
174 class SCEVSignExtendExpr : public SCEV {
177 SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty);
178 virtual ~SCEVSignExtendExpr();
180 /// get method - This just gets and returns a new SCEVSignExtend object
182 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
184 const SCEVHandle &getOperand() const { return Op; }
185 virtual const Type *getType() const { return Ty; }
187 virtual bool isLoopInvariant(const Loop *L) const {
188 return Op->isLoopInvariant(L);
191 virtual bool hasComputableLoopEvolution(const Loop *L) const {
192 return Op->hasComputableLoopEvolution(L);
195 /// getValueRange - Return the tightest constant bounds that this value is
196 /// known to have. This method is only valid on integer SCEV objects.
197 virtual ConstantRange getValueRange() const;
199 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
200 const SCEVHandle &Conc) const {
201 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc);
207 virtual void print(std::ostream &OS) const;
208 void print(std::ostream *OS) const { if (OS) print(*OS); }
210 /// Methods for support type inquiry through isa, cast, and dyn_cast:
211 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
212 static inline bool classof(const SCEV *S) {
213 return S->getSCEVType() == scSignExtend;
218 //===--------------------------------------------------------------------===//
219 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
222 class SCEVCommutativeExpr : public SCEV {
223 std::vector<SCEVHandle> Operands;
226 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
228 Operands.reserve(ops.size());
229 Operands.insert(Operands.end(), ops.begin(), ops.end());
231 ~SCEVCommutativeExpr();
234 unsigned getNumOperands() const { return Operands.size(); }
235 const SCEVHandle &getOperand(unsigned i) const {
236 assert(i < Operands.size() && "Operand index out of range!");
240 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
241 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
242 op_iterator op_begin() const { return Operands.begin(); }
243 op_iterator op_end() const { return Operands.end(); }
246 virtual bool isLoopInvariant(const Loop *L) const {
247 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
248 if (!getOperand(i)->isLoopInvariant(L)) return false;
252 // hasComputableLoopEvolution - Commutative expressions have computable loop
253 // evolutions iff they have at least one operand that varies with the loop,
254 // but that all varying operands are computable.
255 virtual bool hasComputableLoopEvolution(const Loop *L) const {
256 bool HasVarying = false;
257 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
258 if (!getOperand(i)->isLoopInvariant(L))
259 if (getOperand(i)->hasComputableLoopEvolution(L))
266 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
267 const SCEVHandle &Conc) const;
269 virtual const char *getOperationStr() const = 0;
271 virtual const Type *getType() const { return getOperand(0)->getType(); }
272 virtual void print(std::ostream &OS) const;
273 void print(std::ostream *OS) const { if (OS) print(*OS); }
275 /// Methods for support type inquiry through isa, cast, and dyn_cast:
276 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
277 static inline bool classof(const SCEV *S) {
278 return S->getSCEVType() == scAddExpr ||
279 S->getSCEVType() == scMulExpr;
284 //===--------------------------------------------------------------------===//
285 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
287 class SCEVAddExpr : public SCEVCommutativeExpr {
288 SCEVAddExpr(const std::vector<SCEVHandle> &ops)
289 : SCEVCommutativeExpr(scAddExpr, ops) {
293 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
295 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
296 std::vector<SCEVHandle> Ops;
302 static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
303 const SCEVHandle &Op2) {
304 std::vector<SCEVHandle> Ops;
311 virtual const char *getOperationStr() const { return " + "; }
313 /// Methods for support type inquiry through isa, cast, and dyn_cast:
314 static inline bool classof(const SCEVAddExpr *S) { return true; }
315 static inline bool classof(const SCEV *S) {
316 return S->getSCEVType() == scAddExpr;
320 //===--------------------------------------------------------------------===//
321 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
323 class SCEVMulExpr : public SCEVCommutativeExpr {
324 SCEVMulExpr(const std::vector<SCEVHandle> &ops)
325 : SCEVCommutativeExpr(scMulExpr, ops) {
329 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
331 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
332 std::vector<SCEVHandle> Ops;
338 virtual const char *getOperationStr() const { return " * "; }
340 /// Methods for support type inquiry through isa, cast, and dyn_cast:
341 static inline bool classof(const SCEVMulExpr *S) { return true; }
342 static inline bool classof(const SCEV *S) {
343 return S->getSCEVType() == scMulExpr;
348 //===--------------------------------------------------------------------===//
349 /// SCEVSDivExpr - This class represents a binary signed division operation.
351 class SCEVSDivExpr : public SCEV {
353 SCEVSDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
354 : SCEV(scSDivExpr), LHS(lhs), RHS(rhs) {}
356 virtual ~SCEVSDivExpr();
358 /// get method - This just gets and returns a new SCEVSDiv object.
360 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
362 const SCEVHandle &getLHS() const { return LHS; }
363 const SCEVHandle &getRHS() const { return RHS; }
365 virtual bool isLoopInvariant(const Loop *L) const {
366 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
369 virtual bool hasComputableLoopEvolution(const Loop *L) const {
370 return LHS->hasComputableLoopEvolution(L) &&
371 RHS->hasComputableLoopEvolution(L);
374 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
375 const SCEVHandle &Conc) const {
376 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
377 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc);
378 if (L == LHS && R == RHS)
385 virtual const Type *getType() const;
387 void print(std::ostream &OS) const;
388 void print(std::ostream *OS) const { if (OS) print(*OS); }
390 /// Methods for support type inquiry through isa, cast, and dyn_cast:
391 static inline bool classof(const SCEVSDivExpr *S) { return true; }
392 static inline bool classof(const SCEV *S) {
393 return S->getSCEVType() == scSDivExpr;
398 //===--------------------------------------------------------------------===//
399 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
400 /// count of the specified loop.
402 /// All operands of an AddRec are required to be loop invariant.
404 class SCEVAddRecExpr : public SCEV {
405 std::vector<SCEVHandle> Operands;
408 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
409 : SCEV(scAddRecExpr), Operands(ops), L(l) {
410 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
411 assert(Operands[i]->isLoopInvariant(l) &&
412 "Operands of AddRec must be loop-invariant!");
416 static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
418 static SCEVHandle get(std::vector<SCEVHandle> &Operands,
420 static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
422 std::vector<SCEVHandle> NewOp(Operands);
423 return get(NewOp, L);
426 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
427 op_iterator op_begin() const { return Operands.begin(); }
428 op_iterator op_end() const { return Operands.end(); }
430 unsigned getNumOperands() const { return Operands.size(); }
431 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
432 const SCEVHandle &getStart() const { return Operands[0]; }
433 const Loop *getLoop() const { return L; }
436 /// getStepRecurrence - This method constructs and returns the recurrence
437 /// indicating how much this expression steps by. If this is a polynomial
438 /// of degree N, it returns a chrec of degree N-1.
439 SCEVHandle getStepRecurrence() const {
440 if (getNumOperands() == 2) return getOperand(1);
441 return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
445 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
446 if (L == QL) return true;
450 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
452 virtual const Type *getType() const { return Operands[0]->getType(); }
454 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
455 /// an expressions A+B*x where A and B are loop invariant values.
456 bool isAffine() const {
457 // We know that the start value is invariant. This expression is thus
458 // affine iff the step is also invariant.
459 return getNumOperands() == 2;
462 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
463 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
464 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
465 bool isQuadratic() const {
466 return getNumOperands() == 3;
469 /// evaluateAtIteration - Return the value of this chain of recurrences at
470 /// the specified iteration number.
471 SCEVHandle evaluateAtIteration(SCEVHandle It) const;
473 /// getNumIterationsInRange - Return the number of iterations of this loop
474 /// that produce values in the specified constant range. Another way of
475 /// looking at this is that it returns the first iteration number where the
476 /// value is not in the condition, thus computing the exit count. If the
477 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
478 /// returned. The isSigned parameter indicates whether the ConstantRange
479 /// should be treated as signed or unsigned.
480 SCEVHandle getNumIterationsInRange(ConstantRange Range,
481 bool isSigned) const;
483 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
484 const SCEVHandle &Conc) const;
486 virtual void print(std::ostream &OS) const;
487 void print(std::ostream *OS) const { if (OS) print(*OS); }
489 /// Methods for support type inquiry through isa, cast, and dyn_cast:
490 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
491 static inline bool classof(const SCEV *S) {
492 return S->getSCEVType() == scAddRecExpr;
496 //===--------------------------------------------------------------------===//
497 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
498 /// value, and only represent it as it's LLVM Value. This is the "bottom"
499 /// value for the analysis.
501 class SCEVUnknown : public SCEV {
503 SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
508 /// get method - For SCEVUnknown, this just gets and returns a new
510 static SCEVHandle get(Value *V);
512 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
513 /// specified signed integer value and return a SCEV for the constant.
514 static SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
516 Value *getValue() const { return V; }
518 virtual bool isLoopInvariant(const Loop *L) const;
519 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
520 return false; // not computable
523 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
524 const SCEVHandle &Conc) const {
525 if (&*Sym == this) return Conc;
529 virtual const Type *getType() const;
531 virtual void print(std::ostream &OS) const;
532 void print(std::ostream *OS) const { if (OS) print(*OS); }
534 /// Methods for support type inquiry through isa, cast, and dyn_cast:
535 static inline bool classof(const SCEVUnknown *S) { return true; }
536 static inline bool classof(const SCEV *S) {
537 return S->getSCEVType() == scUnknown;
541 /// SCEVVisitor - This class defines a simple visitor class that may be used
542 /// for various SCEV analysis purposes.
543 template<typename SC, typename RetVal=void>
545 RetVal visit(SCEV *S) {
546 switch (S->getSCEVType()) {
548 return ((SC*)this)->visitConstant((SCEVConstant*)S);
550 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
552 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
554 return ((SC*)this)->visitSignExtendExpr((SCEVSignExtendExpr*)S);
556 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
558 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
560 return ((SC*)this)->visitSDivExpr((SCEVSDivExpr*)S);
562 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
564 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
565 case scCouldNotCompute:
566 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
568 assert(0 && "Unknown SCEV type!");
573 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
574 assert(0 && "Invalid use of SCEVCouldNotCompute!");