1 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr, scUnknown,
32 //===--------------------------------------------------------------------===//
33 /// SCEVConstant - This class represents a constant integer value.
35 class SCEVConstant : public SCEV {
36 friend class ScalarEvolution;
39 explicit SCEVConstant(ConstantInt *v) : SCEV(scConstant), V(v) {}
41 virtual ~SCEVConstant();
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,
61 ScalarEvolution &SE) 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 {
80 friend class ScalarEvolution;
84 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
85 virtual ~SCEVTruncateExpr();
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,
100 ScalarEvolution &SE) const {
101 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
104 return SE.getTruncateExpr(H, Ty);
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 {
126 friend class ScalarEvolution;
130 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
131 virtual ~SCEVZeroExtendExpr();
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,
150 ScalarEvolution &SE) const {
151 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
154 return SE.getZeroExtendExpr(H, Ty);
157 virtual void print(std::ostream &OS) const;
158 void print(std::ostream *OS) const { if (OS) print(*OS); }
160 /// Methods for support type inquiry through isa, cast, and dyn_cast:
161 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
162 static inline bool classof(const SCEV *S) {
163 return S->getSCEVType() == scZeroExtend;
167 //===--------------------------------------------------------------------===//
168 /// SCEVSignExtendExpr - This class represents a sign extension of a small
169 /// integer value to a larger integer value.
171 class SCEVSignExtendExpr : public SCEV {
172 friend class ScalarEvolution;
176 SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty);
177 virtual ~SCEVSignExtendExpr();
179 const SCEVHandle &getOperand() const { return Op; }
180 virtual const Type *getType() const { return Ty; }
182 virtual bool isLoopInvariant(const Loop *L) const {
183 return Op->isLoopInvariant(L);
186 virtual bool hasComputableLoopEvolution(const Loop *L) const {
187 return Op->hasComputableLoopEvolution(L);
190 /// getValueRange - Return the tightest constant bounds that this value is
191 /// known to have. This method is only valid on integer SCEV objects.
192 virtual ConstantRange getValueRange() const;
194 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
195 const SCEVHandle &Conc,
196 ScalarEvolution &SE) const {
197 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
200 return SE.getSignExtendExpr(H, Ty);
203 virtual void print(std::ostream &OS) const;
204 void print(std::ostream *OS) const { if (OS) print(*OS); }
206 /// Methods for support type inquiry through isa, cast, and dyn_cast:
207 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
208 static inline bool classof(const SCEV *S) {
209 return S->getSCEVType() == scSignExtend;
214 //===--------------------------------------------------------------------===//
215 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
218 class SCEVCommutativeExpr : public SCEV {
219 friend class ScalarEvolution;
221 std::vector<SCEVHandle> Operands;
224 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
226 Operands.reserve(ops.size());
227 Operands.insert(Operands.end(), ops.begin(), ops.end());
229 ~SCEVCommutativeExpr();
232 unsigned getNumOperands() const { return Operands.size(); }
233 const SCEVHandle &getOperand(unsigned i) const {
234 assert(i < Operands.size() && "Operand index out of range!");
238 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
239 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
240 op_iterator op_begin() const { return Operands.begin(); }
241 op_iterator op_end() const { return Operands.end(); }
244 virtual bool isLoopInvariant(const Loop *L) const {
245 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
246 if (!getOperand(i)->isLoopInvariant(L)) return false;
250 // hasComputableLoopEvolution - Commutative expressions have computable loop
251 // evolutions iff they have at least one operand that varies with the loop,
252 // but that all varying operands are computable.
253 virtual bool hasComputableLoopEvolution(const Loop *L) const {
254 bool HasVarying = false;
255 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
256 if (!getOperand(i)->isLoopInvariant(L))
257 if (getOperand(i)->hasComputableLoopEvolution(L))
264 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
265 const SCEVHandle &Conc,
266 ScalarEvolution &SE) const;
268 virtual const char *getOperationStr() const = 0;
270 virtual const Type *getType() const { return getOperand(0)->getType(); }
271 virtual void print(std::ostream &OS) const;
272 void print(std::ostream *OS) const { if (OS) print(*OS); }
274 /// Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
276 static inline bool classof(const SCEV *S) {
277 return S->getSCEVType() == scAddExpr ||
278 S->getSCEVType() == scMulExpr ||
279 S->getSCEVType() == scSMaxExpr ||
280 S->getSCEVType() == scUMaxExpr;
285 //===--------------------------------------------------------------------===//
286 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
288 class SCEVAddExpr : public SCEVCommutativeExpr {
289 friend class ScalarEvolution;
291 explicit SCEVAddExpr(const std::vector<SCEVHandle> &ops)
292 : SCEVCommutativeExpr(scAddExpr, ops) {
296 virtual const char *getOperationStr() const { return " + "; }
298 /// Methods for support type inquiry through isa, cast, and dyn_cast:
299 static inline bool classof(const SCEVAddExpr *S) { return true; }
300 static inline bool classof(const SCEV *S) {
301 return S->getSCEVType() == scAddExpr;
305 //===--------------------------------------------------------------------===//
306 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
308 class SCEVMulExpr : public SCEVCommutativeExpr {
309 friend class ScalarEvolution;
311 explicit SCEVMulExpr(const std::vector<SCEVHandle> &ops)
312 : SCEVCommutativeExpr(scMulExpr, ops) {
316 virtual const char *getOperationStr() const { return " * "; }
318 /// Methods for support type inquiry through isa, cast, and dyn_cast:
319 static inline bool classof(const SCEVMulExpr *S) { return true; }
320 static inline bool classof(const SCEV *S) {
321 return S->getSCEVType() == scMulExpr;
326 //===--------------------------------------------------------------------===//
327 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
329 class SCEVUDivExpr : public SCEV {
330 friend class ScalarEvolution;
333 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
334 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
336 virtual ~SCEVUDivExpr();
338 const SCEVHandle &getLHS() const { return LHS; }
339 const SCEVHandle &getRHS() const { return RHS; }
341 virtual bool isLoopInvariant(const Loop *L) const {
342 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
345 virtual bool hasComputableLoopEvolution(const Loop *L) const {
346 return LHS->hasComputableLoopEvolution(L) &&
347 RHS->hasComputableLoopEvolution(L);
350 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
351 const SCEVHandle &Conc,
352 ScalarEvolution &SE) const {
353 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
354 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
355 if (L == LHS && R == RHS)
358 return SE.getUDivExpr(L, R);
362 virtual const Type *getType() const;
364 void print(std::ostream &OS) const;
365 void print(std::ostream *OS) const { if (OS) print(*OS); }
367 /// Methods for support type inquiry through isa, cast, and dyn_cast:
368 static inline bool classof(const SCEVUDivExpr *S) { return true; }
369 static inline bool classof(const SCEV *S) {
370 return S->getSCEVType() == scUDivExpr;
375 //===--------------------------------------------------------------------===//
376 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
377 /// count of the specified loop.
379 /// All operands of an AddRec are required to be loop invariant.
381 class SCEVAddRecExpr : public SCEV {
382 friend class ScalarEvolution;
384 std::vector<SCEVHandle> Operands;
387 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
388 : SCEV(scAddRecExpr), Operands(ops), L(l) {
389 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
390 assert(Operands[i]->isLoopInvariant(l) &&
391 "Operands of AddRec must be loop-invariant!");
395 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
396 op_iterator op_begin() const { return Operands.begin(); }
397 op_iterator op_end() const { return Operands.end(); }
399 unsigned getNumOperands() const { return Operands.size(); }
400 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
401 const SCEVHandle &getStart() const { return Operands[0]; }
402 const Loop *getLoop() const { return L; }
405 /// getStepRecurrence - This method constructs and returns the recurrence
406 /// indicating how much this expression steps by. If this is a polynomial
407 /// of degree N, it returns a chrec of degree N-1.
408 SCEVHandle getStepRecurrence(ScalarEvolution &SE) const {
409 if (getNumOperands() == 2) return getOperand(1);
410 return SE.getAddRecExpr(std::vector<SCEVHandle>(op_begin()+1,op_end()),
414 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
415 if (L == QL) return true;
419 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
421 virtual const Type *getType() const { return Operands[0]->getType(); }
423 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
424 /// an expressions A+B*x where A and B are loop invariant values.
425 bool isAffine() const {
426 // We know that the start value is invariant. This expression is thus
427 // affine iff the step is also invariant.
428 return getNumOperands() == 2;
431 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
432 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
433 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
434 bool isQuadratic() const {
435 return getNumOperands() == 3;
438 /// evaluateAtIteration - Return the value of this chain of recurrences at
439 /// the specified iteration number.
440 SCEVHandle evaluateAtIteration(SCEVHandle It, ScalarEvolution &SE) const;
442 /// getNumIterationsInRange - Return the number of iterations of this loop
443 /// that produce values in the specified constant range. Another way of
444 /// looking at this is that it returns the first iteration number where the
445 /// value is not in the condition, thus computing the exit count. If the
446 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
448 SCEVHandle getNumIterationsInRange(ConstantRange Range,
449 ScalarEvolution &SE) const;
451 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
452 const SCEVHandle &Conc,
453 ScalarEvolution &SE) const;
455 virtual void print(std::ostream &OS) const;
456 void print(std::ostream *OS) const { if (OS) print(*OS); }
458 /// Methods for support type inquiry through isa, cast, and dyn_cast:
459 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
460 static inline bool classof(const SCEV *S) {
461 return S->getSCEVType() == scAddRecExpr;
466 //===--------------------------------------------------------------------===//
467 /// SCEVSMaxExpr - This class represents a signed maximum selection.
469 class SCEVSMaxExpr : public SCEVCommutativeExpr {
470 friend class ScalarEvolution;
472 explicit SCEVSMaxExpr(const std::vector<SCEVHandle> &ops)
473 : SCEVCommutativeExpr(scSMaxExpr, ops) {
477 virtual const char *getOperationStr() const { return " smax "; }
479 /// Methods for support type inquiry through isa, cast, and dyn_cast:
480 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
481 static inline bool classof(const SCEV *S) {
482 return S->getSCEVType() == scSMaxExpr;
487 //===--------------------------------------------------------------------===//
488 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
490 class SCEVUMaxExpr : public SCEVCommutativeExpr {
491 friend class ScalarEvolution;
493 explicit SCEVUMaxExpr(const std::vector<SCEVHandle> &ops)
494 : SCEVCommutativeExpr(scUMaxExpr, ops) {
498 virtual const char *getOperationStr() const { return " umax "; }
500 /// Methods for support type inquiry through isa, cast, and dyn_cast:
501 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
502 static inline bool classof(const SCEV *S) {
503 return S->getSCEVType() == scUMaxExpr;
508 //===--------------------------------------------------------------------===//
509 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
510 /// value, and only represent it as it's LLVM Value. This is the "bottom"
511 /// value for the analysis.
513 class SCEVUnknown : public SCEV {
514 friend class ScalarEvolution;
517 explicit SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
522 Value *getValue() const { return V; }
524 virtual bool isLoopInvariant(const Loop *L) const;
525 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
526 return false; // not computable
529 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
530 const SCEVHandle &Conc,
531 ScalarEvolution &SE) const {
532 if (&*Sym == this) return Conc;
536 virtual const Type *getType() const;
538 virtual void print(std::ostream &OS) const;
539 void print(std::ostream *OS) const { if (OS) print(*OS); }
541 /// Methods for support type inquiry through isa, cast, and dyn_cast:
542 static inline bool classof(const SCEVUnknown *S) { return true; }
543 static inline bool classof(const SCEV *S) {
544 return S->getSCEVType() == scUnknown;
548 /// SCEVVisitor - This class defines a simple visitor class that may be used
549 /// for various SCEV analysis purposes.
550 template<typename SC, typename RetVal=void>
552 RetVal visit(SCEV *S) {
553 switch (S->getSCEVType()) {
555 return ((SC*)this)->visitConstant((SCEVConstant*)S);
557 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
559 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
561 return ((SC*)this)->visitSignExtendExpr((SCEVSignExtendExpr*)S);
563 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
565 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
567 return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S);
569 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
571 return ((SC*)this)->visitSMaxExpr((SCEVSMaxExpr*)S);
573 return ((SC*)this)->visitUMaxExpr((SCEVUMaxExpr*)S);
575 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
576 case scCouldNotCompute:
577 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
579 assert(0 && "Unknown SCEV type!");
584 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
585 assert(0 && "Invalid use of SCEVCouldNotCompute!");