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, scSMaxExpr, scUnknown, scCouldNotCompute
31 //===--------------------------------------------------------------------===//
32 /// SCEVConstant - This class represents a constant integer value.
34 class SCEVConstant : public SCEV {
35 friend class ScalarEvolution;
38 explicit SCEVConstant(ConstantInt *v) : SCEV(scConstant), V(v) {}
40 virtual ~SCEVConstant();
42 ConstantInt *getValue() const { return V; }
44 /// getValueRange - Return the tightest constant bounds that this value is
45 /// known to have. This method is only valid on integer SCEV objects.
46 virtual ConstantRange getValueRange() const;
48 virtual bool isLoopInvariant(const Loop *L) const {
52 virtual bool hasComputableLoopEvolution(const Loop *L) const {
53 return false; // Not loop variant
56 virtual const Type *getType() const;
58 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
59 const SCEVHandle &Conc,
60 ScalarEvolution &SE) 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 {
79 friend class ScalarEvolution;
83 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
84 virtual ~SCEVTruncateExpr();
86 const SCEVHandle &getOperand() const { return Op; }
87 virtual const Type *getType() const { return Ty; }
89 virtual bool isLoopInvariant(const Loop *L) const {
90 return Op->isLoopInvariant(L);
93 virtual bool hasComputableLoopEvolution(const Loop *L) const {
94 return Op->hasComputableLoopEvolution(L);
97 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
98 const SCEVHandle &Conc,
99 ScalarEvolution &SE) const {
100 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
103 return SE.getTruncateExpr(H, Ty);
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;
111 void print(std::ostream *OS) const { if (OS) print(*OS); }
113 /// Methods for support type inquiry through isa, cast, and dyn_cast:
114 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
115 static inline bool classof(const SCEV *S) {
116 return S->getSCEVType() == scTruncate;
120 //===--------------------------------------------------------------------===//
121 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
122 /// integer value to a larger integer value.
124 class SCEVZeroExtendExpr : public SCEV {
125 friend class ScalarEvolution;
129 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
130 virtual ~SCEVZeroExtendExpr();
132 const SCEVHandle &getOperand() const { return Op; }
133 virtual const Type *getType() const { return Ty; }
135 virtual bool isLoopInvariant(const Loop *L) const {
136 return Op->isLoopInvariant(L);
139 virtual bool hasComputableLoopEvolution(const Loop *L) const {
140 return Op->hasComputableLoopEvolution(L);
143 /// getValueRange - Return the tightest constant bounds that this value is
144 /// known to have. This method is only valid on integer SCEV objects.
145 virtual ConstantRange getValueRange() const;
147 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
148 const SCEVHandle &Conc,
149 ScalarEvolution &SE) const {
150 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
153 return SE.getZeroExtendExpr(H, Ty);
156 virtual void print(std::ostream &OS) const;
157 void print(std::ostream *OS) const { if (OS) print(*OS); }
159 /// Methods for support type inquiry through isa, cast, and dyn_cast:
160 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
161 static inline bool classof(const SCEV *S) {
162 return S->getSCEVType() == scZeroExtend;
166 //===--------------------------------------------------------------------===//
167 /// SCEVSignExtendExpr - This class represents a sign extension of a small
168 /// integer value to a larger integer value.
170 class SCEVSignExtendExpr : public SCEV {
171 friend class ScalarEvolution;
175 SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty);
176 virtual ~SCEVSignExtendExpr();
178 const SCEVHandle &getOperand() const { return Op; }
179 virtual const Type *getType() const { return Ty; }
181 virtual bool isLoopInvariant(const Loop *L) const {
182 return Op->isLoopInvariant(L);
185 virtual bool hasComputableLoopEvolution(const Loop *L) const {
186 return Op->hasComputableLoopEvolution(L);
189 /// getValueRange - Return the tightest constant bounds that this value is
190 /// known to have. This method is only valid on integer SCEV objects.
191 virtual ConstantRange getValueRange() const;
193 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
194 const SCEVHandle &Conc,
195 ScalarEvolution &SE) const {
196 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
199 return SE.getSignExtendExpr(H, Ty);
202 virtual void print(std::ostream &OS) const;
203 void print(std::ostream *OS) const { if (OS) print(*OS); }
205 /// Methods for support type inquiry through isa, cast, and dyn_cast:
206 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
207 static inline bool classof(const SCEV *S) {
208 return S->getSCEVType() == scSignExtend;
213 //===--------------------------------------------------------------------===//
214 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
217 class SCEVCommutativeExpr : public SCEV {
218 friend class ScalarEvolution;
220 std::vector<SCEVHandle> Operands;
223 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
225 Operands.reserve(ops.size());
226 Operands.insert(Operands.end(), ops.begin(), ops.end());
228 ~SCEVCommutativeExpr();
231 unsigned getNumOperands() const { return Operands.size(); }
232 const SCEVHandle &getOperand(unsigned i) const {
233 assert(i < Operands.size() && "Operand index out of range!");
237 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
238 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
239 op_iterator op_begin() const { return Operands.begin(); }
240 op_iterator op_end() const { return Operands.end(); }
243 virtual bool isLoopInvariant(const Loop *L) const {
244 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
245 if (!getOperand(i)->isLoopInvariant(L)) return false;
249 // hasComputableLoopEvolution - Commutative expressions have computable loop
250 // evolutions iff they have at least one operand that varies with the loop,
251 // but that all varying operands are computable.
252 virtual bool hasComputableLoopEvolution(const Loop *L) const {
253 bool HasVarying = false;
254 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
255 if (!getOperand(i)->isLoopInvariant(L))
256 if (getOperand(i)->hasComputableLoopEvolution(L))
263 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
264 const SCEVHandle &Conc,
265 ScalarEvolution &SE) const;
267 virtual const char *getOperationStr() const = 0;
269 virtual const Type *getType() const { return getOperand(0)->getType(); }
270 virtual void print(std::ostream &OS) const;
271 void print(std::ostream *OS) const { if (OS) print(*OS); }
273 /// Methods for support type inquiry through isa, cast, and dyn_cast:
274 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
275 static inline bool classof(const SCEV *S) {
276 return S->getSCEVType() == scAddExpr ||
277 S->getSCEVType() == scMulExpr ||
278 S->getSCEVType() == scSMaxExpr;
283 //===--------------------------------------------------------------------===//
284 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
286 class SCEVAddExpr : public SCEVCommutativeExpr {
287 friend class ScalarEvolution;
289 explicit SCEVAddExpr(const std::vector<SCEVHandle> &ops)
290 : SCEVCommutativeExpr(scAddExpr, ops) {
294 virtual const char *getOperationStr() const { return " + "; }
296 /// Methods for support type inquiry through isa, cast, and dyn_cast:
297 static inline bool classof(const SCEVAddExpr *S) { return true; }
298 static inline bool classof(const SCEV *S) {
299 return S->getSCEVType() == scAddExpr;
303 //===--------------------------------------------------------------------===//
304 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
306 class SCEVMulExpr : public SCEVCommutativeExpr {
307 friend class ScalarEvolution;
309 explicit SCEVMulExpr(const std::vector<SCEVHandle> &ops)
310 : SCEVCommutativeExpr(scMulExpr, ops) {
314 virtual const char *getOperationStr() const { return " * "; }
316 /// Methods for support type inquiry through isa, cast, and dyn_cast:
317 static inline bool classof(const SCEVMulExpr *S) { return true; }
318 static inline bool classof(const SCEV *S) {
319 return S->getSCEVType() == scMulExpr;
324 //===--------------------------------------------------------------------===//
325 /// SCEVSDivExpr - This class represents a binary signed division operation.
327 class SCEVSDivExpr : public SCEV {
328 friend class ScalarEvolution;
331 SCEVSDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
332 : SCEV(scSDivExpr), LHS(lhs), RHS(rhs) {}
334 virtual ~SCEVSDivExpr();
336 const SCEVHandle &getLHS() const { return LHS; }
337 const SCEVHandle &getRHS() const { return RHS; }
339 virtual bool isLoopInvariant(const Loop *L) const {
340 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
343 virtual bool hasComputableLoopEvolution(const Loop *L) const {
344 return LHS->hasComputableLoopEvolution(L) &&
345 RHS->hasComputableLoopEvolution(L);
348 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
349 const SCEVHandle &Conc,
350 ScalarEvolution &SE) const {
351 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
352 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
353 if (L == LHS && R == RHS)
356 return SE.getSDivExpr(L, R);
360 virtual const Type *getType() const;
362 void print(std::ostream &OS) const;
363 void print(std::ostream *OS) const { if (OS) print(*OS); }
365 /// Methods for support type inquiry through isa, cast, and dyn_cast:
366 static inline bool classof(const SCEVSDivExpr *S) { return true; }
367 static inline bool classof(const SCEV *S) {
368 return S->getSCEVType() == scSDivExpr;
373 //===--------------------------------------------------------------------===//
374 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
375 /// count of the specified loop.
377 /// All operands of an AddRec are required to be loop invariant.
379 class SCEVAddRecExpr : public SCEV {
380 friend class ScalarEvolution;
382 std::vector<SCEVHandle> Operands;
385 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
386 : SCEV(scAddRecExpr), Operands(ops), L(l) {
387 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
388 assert(Operands[i]->isLoopInvariant(l) &&
389 "Operands of AddRec must be loop-invariant!");
393 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
394 op_iterator op_begin() const { return Operands.begin(); }
395 op_iterator op_end() const { return Operands.end(); }
397 unsigned getNumOperands() const { return Operands.size(); }
398 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
399 const SCEVHandle &getStart() const { return Operands[0]; }
400 const Loop *getLoop() const { return L; }
403 /// getStepRecurrence - This method constructs and returns the recurrence
404 /// indicating how much this expression steps by. If this is a polynomial
405 /// of degree N, it returns a chrec of degree N-1.
406 SCEVHandle getStepRecurrence(ScalarEvolution &SE) const {
407 if (getNumOperands() == 2) return getOperand(1);
408 return SE.getAddRecExpr(std::vector<SCEVHandle>(op_begin()+1,op_end()),
412 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
413 if (L == QL) return true;
417 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
419 virtual const Type *getType() const { return Operands[0]->getType(); }
421 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
422 /// an expressions A+B*x where A and B are loop invariant values.
423 bool isAffine() const {
424 // We know that the start value is invariant. This expression is thus
425 // affine iff the step is also invariant.
426 return getNumOperands() == 2;
429 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
430 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
431 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
432 bool isQuadratic() const {
433 return getNumOperands() == 3;
436 /// evaluateAtIteration - Return the value of this chain of recurrences at
437 /// the specified iteration number.
438 SCEVHandle evaluateAtIteration(SCEVHandle It, ScalarEvolution &SE) const;
440 /// getNumIterationsInRange - Return the number of iterations of this loop
441 /// that produce values in the specified constant range. Another way of
442 /// looking at this is that it returns the first iteration number where the
443 /// value is not in the condition, thus computing the exit count. If the
444 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
446 SCEVHandle getNumIterationsInRange(ConstantRange Range,
447 ScalarEvolution &SE) const;
449 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
450 const SCEVHandle &Conc,
451 ScalarEvolution &SE) const;
453 virtual void print(std::ostream &OS) const;
454 void print(std::ostream *OS) const { if (OS) print(*OS); }
456 /// Methods for support type inquiry through isa, cast, and dyn_cast:
457 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
458 static inline bool classof(const SCEV *S) {
459 return S->getSCEVType() == scAddRecExpr;
464 //===--------------------------------------------------------------------===//
465 /// SCEVSMaxExpr - This class represents a signed maximum selection.
467 class SCEVSMaxExpr : public SCEVCommutativeExpr {
468 friend class ScalarEvolution;
470 explicit SCEVSMaxExpr(const std::vector<SCEVHandle> &ops)
471 : SCEVCommutativeExpr(scSMaxExpr, ops) {
475 virtual const char *getOperationStr() const { return " smax "; }
477 /// Methods for support type inquiry through isa, cast, and dyn_cast:
478 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
479 static inline bool classof(const SCEV *S) {
480 return S->getSCEVType() == scSMaxExpr;
485 //===--------------------------------------------------------------------===//
486 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
487 /// value, and only represent it as it's LLVM Value. This is the "bottom"
488 /// value for the analysis.
490 class SCEVUnknown : public SCEV {
491 friend class ScalarEvolution;
494 explicit SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
499 Value *getValue() const { return V; }
501 virtual bool isLoopInvariant(const Loop *L) const;
502 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
503 return false; // not computable
506 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
507 const SCEVHandle &Conc,
508 ScalarEvolution &SE) const {
509 if (&*Sym == this) return Conc;
513 virtual const Type *getType() const;
515 virtual void print(std::ostream &OS) const;
516 void print(std::ostream *OS) const { if (OS) print(*OS); }
518 /// Methods for support type inquiry through isa, cast, and dyn_cast:
519 static inline bool classof(const SCEVUnknown *S) { return true; }
520 static inline bool classof(const SCEV *S) {
521 return S->getSCEVType() == scUnknown;
525 /// SCEVVisitor - This class defines a simple visitor class that may be used
526 /// for various SCEV analysis purposes.
527 template<typename SC, typename RetVal=void>
529 RetVal visit(SCEV *S) {
530 switch (S->getSCEVType()) {
532 return ((SC*)this)->visitConstant((SCEVConstant*)S);
534 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
536 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
538 return ((SC*)this)->visitSignExtendExpr((SCEVSignExtendExpr*)S);
540 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
542 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
544 return ((SC*)this)->visitSDivExpr((SCEVSDivExpr*)S);
546 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
548 return ((SC*)this)->visitSMaxExpr((SCEVSMaxExpr*)S);
550 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
551 case scCouldNotCompute:
552 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
554 assert(0 && "Unknown SCEV type!");
559 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
560 assert(0 && "Invalid use of SCEVCouldNotCompute!");