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 virtual bool isLoopInvariant(const Loop *L) const {
49 virtual bool hasComputableLoopEvolution(const Loop *L) const {
50 return false; // Not loop variant
53 virtual const Type *getType() const;
55 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
56 const SCEVHandle &Conc,
57 ScalarEvolution &SE) const {
61 bool dominates(BasicBlock *BB, DominatorTree *DT) const {
65 virtual void print(raw_ostream &OS) const;
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 /// SCEVCastExpr - This is the base class for unary cast operator classes.
77 class SCEVCastExpr : public SCEV {
82 SCEVCastExpr(unsigned SCEVTy, const SCEVHandle &op, const Type *ty);
83 virtual ~SCEVCastExpr();
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 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
99 /// Methods for support type inquiry through isa, cast, and dyn_cast:
100 static inline bool classof(const SCEVCastExpr *S) { return true; }
101 static inline bool classof(const SCEV *S) {
102 return S->getSCEVType() == scTruncate ||
103 S->getSCEVType() == scZeroExtend ||
104 S->getSCEVType() == scSignExtend;
108 //===--------------------------------------------------------------------===//
109 /// SCEVTruncateExpr - This class represents a truncation of an integer value
110 /// to a smaller integer value.
112 class SCEVTruncateExpr : public SCEVCastExpr {
113 friend class ScalarEvolution;
115 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
116 virtual ~SCEVTruncateExpr();
119 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
120 const SCEVHandle &Conc,
121 ScalarEvolution &SE) const {
122 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
125 return SE.getTruncateExpr(H, Ty);
128 virtual void print(raw_ostream &OS) const;
130 /// Methods for support type inquiry through isa, cast, and dyn_cast:
131 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
132 static inline bool classof(const SCEV *S) {
133 return S->getSCEVType() == scTruncate;
137 //===--------------------------------------------------------------------===//
138 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
139 /// integer value to a larger integer value.
141 class SCEVZeroExtendExpr : public SCEVCastExpr {
142 friend class ScalarEvolution;
144 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
145 virtual ~SCEVZeroExtendExpr();
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(raw_ostream &OS) const;
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 SCEVCastExpr {
171 friend class ScalarEvolution;
173 SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty);
174 virtual ~SCEVSignExtendExpr();
177 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
178 const SCEVHandle &Conc,
179 ScalarEvolution &SE) const {
180 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
183 return SE.getSignExtendExpr(H, Ty);
186 virtual void print(raw_ostream &OS) const;
188 /// Methods for support type inquiry through isa, cast, and dyn_cast:
189 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
190 static inline bool classof(const SCEV *S) {
191 return S->getSCEVType() == scSignExtend;
196 //===--------------------------------------------------------------------===//
197 /// SCEVNAryExpr - This node is a base class providing common
198 /// functionality for n'ary operators.
200 class SCEVNAryExpr : public SCEV {
202 SmallVector<SCEVHandle, 8> Operands;
204 SCEVNAryExpr(enum SCEVTypes T, const SmallVectorImpl<SCEVHandle> &ops)
205 : SCEV(T), Operands(ops.begin(), ops.end()) {}
206 virtual ~SCEVNAryExpr() {}
209 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
210 const SCEVHandle &getOperand(unsigned i) const {
211 assert(i < Operands.size() && "Operand index out of range!");
215 const SmallVectorImpl<SCEVHandle> &getOperands() const { return Operands; }
216 typedef SmallVectorImpl<SCEVHandle>::const_iterator op_iterator;
217 op_iterator op_begin() const { return Operands.begin(); }
218 op_iterator op_end() const { return Operands.end(); }
220 virtual bool isLoopInvariant(const Loop *L) const {
221 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
222 if (!getOperand(i)->isLoopInvariant(L)) return false;
226 // hasComputableLoopEvolution - N-ary expressions have computable loop
227 // evolutions iff they have at least one operand that varies with the loop,
228 // but that all varying operands are computable.
229 virtual bool hasComputableLoopEvolution(const Loop *L) const {
230 bool HasVarying = false;
231 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
232 if (!getOperand(i)->isLoopInvariant(L)) {
233 if (getOperand(i)->hasComputableLoopEvolution(L))
241 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
243 virtual const Type *getType() const { return getOperand(0)->getType(); }
245 /// Methods for support type inquiry through isa, cast, and dyn_cast:
246 static inline bool classof(const SCEVNAryExpr *S) { return true; }
247 static inline bool classof(const SCEV *S) {
248 return S->getSCEVType() == scAddExpr ||
249 S->getSCEVType() == scMulExpr ||
250 S->getSCEVType() == scSMaxExpr ||
251 S->getSCEVType() == scUMaxExpr ||
252 S->getSCEVType() == scAddRecExpr;
256 //===--------------------------------------------------------------------===//
257 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
260 class SCEVCommutativeExpr : public SCEVNAryExpr {
262 SCEVCommutativeExpr(enum SCEVTypes T, const SmallVectorImpl<SCEVHandle> &ops)
263 : SCEVNAryExpr(T, ops) {}
264 ~SCEVCommutativeExpr();
267 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
268 const SCEVHandle &Conc,
269 ScalarEvolution &SE) const;
271 virtual const char *getOperationStr() const = 0;
273 virtual void print(raw_ostream &OS) const;
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 ||
280 S->getSCEVType() == scSMaxExpr ||
281 S->getSCEVType() == scUMaxExpr;
286 //===--------------------------------------------------------------------===//
287 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
289 class SCEVAddExpr : public SCEVCommutativeExpr {
290 friend class ScalarEvolution;
292 explicit SCEVAddExpr(const SmallVectorImpl<SCEVHandle> &ops)
293 : SCEVCommutativeExpr(scAddExpr, ops) {
297 virtual const char *getOperationStr() const { return " + "; }
299 /// Methods for support type inquiry through isa, cast, and dyn_cast:
300 static inline bool classof(const SCEVAddExpr *S) { return true; }
301 static inline bool classof(const SCEV *S) {
302 return S->getSCEVType() == scAddExpr;
306 //===--------------------------------------------------------------------===//
307 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
309 class SCEVMulExpr : public SCEVCommutativeExpr {
310 friend class ScalarEvolution;
312 explicit SCEVMulExpr(const SmallVectorImpl<SCEVHandle> &ops)
313 : SCEVCommutativeExpr(scMulExpr, ops) {
317 virtual const char *getOperationStr() const { return " * "; }
319 /// Methods for support type inquiry through isa, cast, and dyn_cast:
320 static inline bool classof(const SCEVMulExpr *S) { return true; }
321 static inline bool classof(const SCEV *S) {
322 return S->getSCEVType() == scMulExpr;
327 //===--------------------------------------------------------------------===//
328 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
330 class SCEVUDivExpr : public SCEV {
331 friend class ScalarEvolution;
334 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
335 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
337 virtual ~SCEVUDivExpr();
339 const SCEVHandle &getLHS() const { return LHS; }
340 const SCEVHandle &getRHS() const { return RHS; }
342 virtual bool isLoopInvariant(const Loop *L) const {
343 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
346 virtual bool hasComputableLoopEvolution(const Loop *L) const {
347 return LHS->hasComputableLoopEvolution(L) &&
348 RHS->hasComputableLoopEvolution(L);
351 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
352 const SCEVHandle &Conc,
353 ScalarEvolution &SE) const {
354 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
355 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
356 if (L == LHS && R == RHS)
359 return SE.getUDivExpr(L, R);
362 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
364 virtual const Type *getType() const;
366 void print(raw_ostream &OS) const;
368 /// Methods for support type inquiry through isa, cast, and dyn_cast:
369 static inline bool classof(const SCEVUDivExpr *S) { return true; }
370 static inline bool classof(const SCEV *S) {
371 return S->getSCEVType() == scUDivExpr;
376 //===--------------------------------------------------------------------===//
377 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
378 /// count of the specified loop. This is the primary focus of the
379 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
380 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
381 /// created and analyzed.
383 /// All operands of an AddRec are required to be loop invariant.
385 class SCEVAddRecExpr : public SCEVNAryExpr {
386 friend class ScalarEvolution;
390 SCEVAddRecExpr(const SmallVectorImpl<SCEVHandle> &ops, const Loop *l)
391 : SCEVNAryExpr(scAddRecExpr, ops), L(l) {
392 for (size_t i = 0, e = Operands.size(); i != e; ++i)
393 assert(Operands[i]->isLoopInvariant(l) &&
394 "Operands of AddRec must be loop-invariant!");
399 const SCEVHandle &getStart() const { return Operands[0]; }
400 const Loop *getLoop() const { return L; }
402 /// getStepRecurrence - This method constructs and returns the recurrence
403 /// indicating how much this expression steps by. If this is a polynomial
404 /// of degree N, it returns a chrec of degree N-1.
405 SCEVHandle getStepRecurrence(ScalarEvolution &SE) const {
406 if (isAffine()) return getOperand(1);
407 return SE.getAddRecExpr(SmallVector<SCEVHandle, 3>(op_begin()+1,op_end()),
411 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
412 if (L == QL) return true;
416 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
418 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
419 /// an expressions A+B*x where A and B are loop invariant values.
420 bool isAffine() const {
421 // We know that the start value is invariant. This expression is thus
422 // affine iff the step is also invariant.
423 return getNumOperands() == 2;
426 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
427 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
428 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
429 bool isQuadratic() const {
430 return getNumOperands() == 3;
433 /// evaluateAtIteration - Return the value of this chain of recurrences at
434 /// the specified iteration number.
435 SCEVHandle evaluateAtIteration(SCEVHandle It, ScalarEvolution &SE) const;
437 /// getNumIterationsInRange - Return the number of iterations of this loop
438 /// that produce values in the specified constant range. Another way of
439 /// looking at this is that it returns the first iteration number where the
440 /// value is not in the condition, thus computing the exit count. If the
441 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
443 SCEVHandle getNumIterationsInRange(ConstantRange Range,
444 ScalarEvolution &SE) const;
446 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
447 const SCEVHandle &Conc,
448 ScalarEvolution &SE) const;
450 virtual void print(raw_ostream &OS) const;
452 /// Methods for support type inquiry through isa, cast, and dyn_cast:
453 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
454 static inline bool classof(const SCEV *S) {
455 return S->getSCEVType() == scAddRecExpr;
460 //===--------------------------------------------------------------------===//
461 /// SCEVSMaxExpr - This class represents a signed maximum selection.
463 class SCEVSMaxExpr : public SCEVCommutativeExpr {
464 friend class ScalarEvolution;
466 explicit SCEVSMaxExpr(const SmallVectorImpl<SCEVHandle> &ops)
467 : SCEVCommutativeExpr(scSMaxExpr, ops) {
471 virtual const char *getOperationStr() const { return " smax "; }
473 /// Methods for support type inquiry through isa, cast, and dyn_cast:
474 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
475 static inline bool classof(const SCEV *S) {
476 return S->getSCEVType() == scSMaxExpr;
481 //===--------------------------------------------------------------------===//
482 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
484 class SCEVUMaxExpr : public SCEVCommutativeExpr {
485 friend class ScalarEvolution;
487 explicit SCEVUMaxExpr(const SmallVectorImpl<SCEVHandle> &ops)
488 : SCEVCommutativeExpr(scUMaxExpr, ops) {
492 virtual const char *getOperationStr() const { return " umax "; }
494 /// Methods for support type inquiry through isa, cast, and dyn_cast:
495 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
496 static inline bool classof(const SCEV *S) {
497 return S->getSCEVType() == scUMaxExpr;
502 //===--------------------------------------------------------------------===//
503 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
504 /// value, and only represent it as it's LLVM Value. This is the "bottom"
505 /// value for the analysis.
507 class SCEVUnknown : public SCEV {
508 friend class ScalarEvolution;
511 explicit SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
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,
525 ScalarEvolution &SE) const {
526 if (&*Sym == this) return Conc;
530 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
532 virtual const Type *getType() const;
534 virtual void print(raw_ostream &OS) const;
536 /// Methods for support type inquiry through isa, cast, and dyn_cast:
537 static inline bool classof(const SCEVUnknown *S) { return true; }
538 static inline bool classof(const SCEV *S) {
539 return S->getSCEVType() == scUnknown;
543 /// SCEVVisitor - This class defines a simple visitor class that may be used
544 /// for various SCEV analysis purposes.
545 template<typename SC, typename RetVal=void>
547 RetVal visit(const SCEV *S) {
548 switch (S->getSCEVType()) {
550 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
552 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
554 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
556 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
558 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
560 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
562 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
564 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
566 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
568 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
570 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
571 case scCouldNotCompute:
572 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
574 assert(0 && "Unknown SCEV type!");
579 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
580 assert(0 && "Invalid use of SCEVCouldNotCompute!");