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) :
40 SCEV(scConstant), V(v) {}
42 ConstantInt *getValue() const { return V; }
44 virtual bool isLoopInvariant(const Loop *L) const {
48 virtual bool hasComputableLoopEvolution(const Loop *L) const {
49 return false; // Not loop variant
52 virtual const Type *getType() const;
54 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
56 ScalarEvolution &SE) const {
60 bool dominates(BasicBlock *BB, DominatorTree *DT) const {
64 virtual void print(raw_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 /// SCEVCastExpr - This is the base class for unary cast operator classes.
76 class SCEVCastExpr : public SCEV {
81 SCEVCastExpr(unsigned SCEVTy, const SCEV* op, const Type *ty);
84 const SCEV* getOperand() const { return Op; }
85 virtual const Type *getType() const { return Ty; }
87 virtual bool isLoopInvariant(const Loop *L) const {
88 return Op->isLoopInvariant(L);
91 virtual bool hasComputableLoopEvolution(const Loop *L) const {
92 return Op->hasComputableLoopEvolution(L);
95 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
97 /// Methods for support type inquiry through isa, cast, and dyn_cast:
98 static inline bool classof(const SCEVCastExpr *S) { return true; }
99 static inline bool classof(const SCEV *S) {
100 return S->getSCEVType() == scTruncate ||
101 S->getSCEVType() == scZeroExtend ||
102 S->getSCEVType() == scSignExtend;
106 //===--------------------------------------------------------------------===//
107 /// SCEVTruncateExpr - This class represents a truncation of an integer value
108 /// to a smaller integer value.
110 class SCEVTruncateExpr : public SCEVCastExpr {
111 friend class ScalarEvolution;
113 SCEVTruncateExpr(const SCEV* op, const Type *ty);
116 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
118 ScalarEvolution &SE) const {
119 const SCEV* H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
122 return SE.getTruncateExpr(H, Ty);
125 virtual void print(raw_ostream &OS) const;
127 /// Methods for support type inquiry through isa, cast, and dyn_cast:
128 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
129 static inline bool classof(const SCEV *S) {
130 return S->getSCEVType() == scTruncate;
134 //===--------------------------------------------------------------------===//
135 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
136 /// integer value to a larger integer value.
138 class SCEVZeroExtendExpr : public SCEVCastExpr {
139 friend class ScalarEvolution;
141 SCEVZeroExtendExpr(const SCEV* op, const Type *ty);
144 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
146 ScalarEvolution &SE) const {
147 const SCEV* H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
150 return SE.getZeroExtendExpr(H, Ty);
153 virtual void print(raw_ostream &OS) const;
155 /// Methods for support type inquiry through isa, cast, and dyn_cast:
156 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
157 static inline bool classof(const SCEV *S) {
158 return S->getSCEVType() == scZeroExtend;
162 //===--------------------------------------------------------------------===//
163 /// SCEVSignExtendExpr - This class represents a sign extension of a small
164 /// integer value to a larger integer value.
166 class SCEVSignExtendExpr : public SCEVCastExpr {
167 friend class ScalarEvolution;
169 SCEVSignExtendExpr(const SCEV* op, const Type *ty);
172 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
174 ScalarEvolution &SE) const {
175 const SCEV* H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
178 return SE.getSignExtendExpr(H, Ty);
181 virtual void print(raw_ostream &OS) const;
183 /// Methods for support type inquiry through isa, cast, and dyn_cast:
184 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
185 static inline bool classof(const SCEV *S) {
186 return S->getSCEVType() == scSignExtend;
191 //===--------------------------------------------------------------------===//
192 /// SCEVNAryExpr - This node is a base class providing common
193 /// functionality for n'ary operators.
195 class SCEVNAryExpr : public SCEV {
197 SmallVector<const SCEV*, 8> Operands;
199 SCEVNAryExpr(enum SCEVTypes T, const SmallVectorImpl<const SCEV*> &ops)
200 : SCEV(T), Operands(ops.begin(), ops.end()) {}
203 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
204 const SCEV* getOperand(unsigned i) const {
205 assert(i < Operands.size() && "Operand index out of range!");
209 const SmallVectorImpl<const SCEV*> &getOperands() const { return Operands; }
210 typedef SmallVectorImpl<const SCEV*>::const_iterator op_iterator;
211 op_iterator op_begin() const { return Operands.begin(); }
212 op_iterator op_end() const { return Operands.end(); }
214 virtual bool isLoopInvariant(const Loop *L) const {
215 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
216 if (!getOperand(i)->isLoopInvariant(L)) return false;
220 // hasComputableLoopEvolution - N-ary expressions have computable loop
221 // evolutions iff they have at least one operand that varies with the loop,
222 // but that all varying operands are computable.
223 virtual bool hasComputableLoopEvolution(const Loop *L) const {
224 bool HasVarying = false;
225 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
226 if (!getOperand(i)->isLoopInvariant(L)) {
227 if (getOperand(i)->hasComputableLoopEvolution(L))
235 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
237 virtual const Type *getType() const { return getOperand(0)->getType(); }
239 /// Methods for support type inquiry through isa, cast, and dyn_cast:
240 static inline bool classof(const SCEVNAryExpr *S) { return true; }
241 static inline bool classof(const SCEV *S) {
242 return S->getSCEVType() == scAddExpr ||
243 S->getSCEVType() == scMulExpr ||
244 S->getSCEVType() == scSMaxExpr ||
245 S->getSCEVType() == scUMaxExpr ||
246 S->getSCEVType() == scAddRecExpr;
250 //===--------------------------------------------------------------------===//
251 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
254 class SCEVCommutativeExpr : public SCEVNAryExpr {
256 SCEVCommutativeExpr(enum SCEVTypes T,
257 const SmallVectorImpl<const SCEV*> &ops)
258 : SCEVNAryExpr(T, ops) {}
261 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
263 ScalarEvolution &SE) const;
265 virtual const char *getOperationStr() const = 0;
267 virtual void print(raw_ostream &OS) const;
269 /// Methods for support type inquiry through isa, cast, and dyn_cast:
270 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
271 static inline bool classof(const SCEV *S) {
272 return S->getSCEVType() == scAddExpr ||
273 S->getSCEVType() == scMulExpr ||
274 S->getSCEVType() == scSMaxExpr ||
275 S->getSCEVType() == scUMaxExpr;
280 //===--------------------------------------------------------------------===//
281 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
283 class SCEVAddExpr : public SCEVCommutativeExpr {
284 friend class ScalarEvolution;
286 explicit SCEVAddExpr(const SmallVectorImpl<const SCEV*> &ops)
287 : SCEVCommutativeExpr(scAddExpr, ops) {
291 virtual const char *getOperationStr() const { return " + "; }
293 /// Methods for support type inquiry through isa, cast, and dyn_cast:
294 static inline bool classof(const SCEVAddExpr *S) { return true; }
295 static inline bool classof(const SCEV *S) {
296 return S->getSCEVType() == scAddExpr;
300 //===--------------------------------------------------------------------===//
301 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
303 class SCEVMulExpr : public SCEVCommutativeExpr {
304 friend class ScalarEvolution;
306 explicit SCEVMulExpr(const SmallVectorImpl<const SCEV*> &ops)
307 : SCEVCommutativeExpr(scMulExpr, 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 SCEVMulExpr *S) { return true; }
315 static inline bool classof(const SCEV *S) {
316 return S->getSCEVType() == scMulExpr;
321 //===--------------------------------------------------------------------===//
322 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
324 class SCEVUDivExpr : public SCEV {
325 friend class ScalarEvolution;
329 SCEVUDivExpr(const SCEV* lhs, const SCEV* rhs)
330 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
333 const SCEV* getLHS() const { return LHS; }
334 const SCEV* getRHS() const { return RHS; }
336 virtual bool isLoopInvariant(const Loop *L) const {
337 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
340 virtual bool hasComputableLoopEvolution(const Loop *L) const {
341 return LHS->hasComputableLoopEvolution(L) &&
342 RHS->hasComputableLoopEvolution(L);
345 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
347 ScalarEvolution &SE) const {
348 const SCEV* L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
349 const SCEV* R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
350 if (L == LHS && R == RHS)
353 return SE.getUDivExpr(L, R);
356 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
358 virtual const Type *getType() const;
360 void print(raw_ostream &OS) const;
362 /// Methods for support type inquiry through isa, cast, and dyn_cast:
363 static inline bool classof(const SCEVUDivExpr *S) { return true; }
364 static inline bool classof(const SCEV *S) {
365 return S->getSCEVType() == scUDivExpr;
370 //===--------------------------------------------------------------------===//
371 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
372 /// count of the specified loop. This is the primary focus of the
373 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
374 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
375 /// created and analyzed.
377 /// All operands of an AddRec are required to be loop invariant.
379 class SCEVAddRecExpr : public SCEVNAryExpr {
380 friend class ScalarEvolution;
384 SCEVAddRecExpr(const SmallVectorImpl<const SCEV*> &ops, const Loop *l)
385 : SCEVNAryExpr(scAddRecExpr, ops), L(l) {
386 for (size_t i = 0, e = Operands.size(); i != e; ++i)
387 assert(Operands[i]->isLoopInvariant(l) &&
388 "Operands of AddRec must be loop-invariant!");
392 const SCEV* getStart() const { return Operands[0]; }
393 const Loop *getLoop() const { return L; }
395 /// getStepRecurrence - This method constructs and returns the recurrence
396 /// indicating how much this expression steps by. If this is a polynomial
397 /// of degree N, it returns a chrec of degree N-1.
398 const SCEV* getStepRecurrence(ScalarEvolution &SE) const {
399 if (isAffine()) return getOperand(1);
400 return SE.getAddRecExpr(SmallVector<const SCEV*, 3>(op_begin()+1,op_end()),
404 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
405 if (L == QL) return true;
409 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
411 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
412 /// an expressions A+B*x where A and B are loop invariant values.
413 bool isAffine() const {
414 // We know that the start value is invariant. This expression is thus
415 // affine iff the step is also invariant.
416 return getNumOperands() == 2;
419 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
420 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
421 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
422 bool isQuadratic() const {
423 return getNumOperands() == 3;
426 /// evaluateAtIteration - Return the value of this chain of recurrences at
427 /// the specified iteration number.
428 const SCEV* evaluateAtIteration(const SCEV* It, ScalarEvolution &SE) const;
430 /// getNumIterationsInRange - Return the number of iterations of this loop
431 /// that produce values in the specified constant range. Another way of
432 /// looking at this is that it returns the first iteration number where the
433 /// value is not in the condition, thus computing the exit count. If the
434 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
436 const SCEV* getNumIterationsInRange(ConstantRange Range,
437 ScalarEvolution &SE) const;
439 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
441 ScalarEvolution &SE) const;
443 virtual void print(raw_ostream &OS) const;
445 /// Methods for support type inquiry through isa, cast, and dyn_cast:
446 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
447 static inline bool classof(const SCEV *S) {
448 return S->getSCEVType() == scAddRecExpr;
453 //===--------------------------------------------------------------------===//
454 /// SCEVSMaxExpr - This class represents a signed maximum selection.
456 class SCEVSMaxExpr : public SCEVCommutativeExpr {
457 friend class ScalarEvolution;
459 explicit SCEVSMaxExpr(const SmallVectorImpl<const SCEV*> &ops)
460 : SCEVCommutativeExpr(scSMaxExpr, ops) {
464 virtual const char *getOperationStr() const { return " smax "; }
466 /// Methods for support type inquiry through isa, cast, and dyn_cast:
467 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
468 static inline bool classof(const SCEV *S) {
469 return S->getSCEVType() == scSMaxExpr;
474 //===--------------------------------------------------------------------===//
475 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
477 class SCEVUMaxExpr : public SCEVCommutativeExpr {
478 friend class ScalarEvolution;
480 explicit SCEVUMaxExpr(const SmallVectorImpl<const SCEV*> &ops)
481 : SCEVCommutativeExpr(scUMaxExpr, ops) {
485 virtual const char *getOperationStr() const { return " umax "; }
487 /// Methods for support type inquiry through isa, cast, and dyn_cast:
488 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
489 static inline bool classof(const SCEV *S) {
490 return S->getSCEVType() == scUMaxExpr;
495 //===--------------------------------------------------------------------===//
496 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
497 /// value, and only represent it as it's LLVM Value. This is the "bottom"
498 /// value for the analysis.
500 class SCEVUnknown : public SCEV {
501 friend class ScalarEvolution;
504 explicit SCEVUnknown(Value *v) :
505 SCEV(scUnknown), V(v) {}
508 Value *getValue() const { return V; }
510 virtual bool isLoopInvariant(const Loop *L) const;
511 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
512 return false; // not computable
515 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
517 ScalarEvolution &SE) const {
518 if (&*Sym == this) return Conc;
522 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
524 virtual const Type *getType() const;
526 virtual void print(raw_ostream &OS) const;
528 /// Methods for support type inquiry through isa, cast, and dyn_cast:
529 static inline bool classof(const SCEVUnknown *S) { return true; }
530 static inline bool classof(const SCEV *S) {
531 return S->getSCEVType() == scUnknown;
535 /// SCEVVisitor - This class defines a simple visitor class that may be used
536 /// for various SCEV analysis purposes.
537 template<typename SC, typename RetVal=void>
539 RetVal visit(const SCEV *S) {
540 switch (S->getSCEVType()) {
542 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
544 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
546 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
548 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
550 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
552 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
554 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
556 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
558 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
560 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
562 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
563 case scCouldNotCompute:
564 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
566 assert(0 && "Unknown SCEV type!");
571 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
572 assert(0 && "Invalid use of SCEVCouldNotCompute!");