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 virtual void Profile(FoldingSetNodeID &ID) const;
44 ConstantInt *getValue() const { return V; }
46 virtual bool isLoopInvariant(const Loop *L) const {
50 virtual bool hasComputableLoopEvolution(const Loop *L) const {
51 return false; // Not loop variant
54 virtual const Type *getType() const;
56 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
58 ScalarEvolution &SE) const {
62 bool dominates(BasicBlock *BB, DominatorTree *DT) const {
66 virtual void print(raw_ostream &OS) const;
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 /// SCEVCastExpr - This is the base class for unary cast operator classes.
78 class SCEVCastExpr : public SCEV {
83 SCEVCastExpr(unsigned SCEVTy, const SCEV* op, const Type *ty);
86 virtual void Profile(FoldingSetNodeID &ID) const;
88 const SCEV* getOperand() const { return Op; }
89 virtual const Type *getType() const { return Ty; }
91 virtual bool isLoopInvariant(const Loop *L) const {
92 return Op->isLoopInvariant(L);
95 virtual bool hasComputableLoopEvolution(const Loop *L) const {
96 return Op->hasComputableLoopEvolution(L);
99 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
101 /// Methods for support type inquiry through isa, cast, and dyn_cast:
102 static inline bool classof(const SCEVCastExpr *S) { return true; }
103 static inline bool classof(const SCEV *S) {
104 return S->getSCEVType() == scTruncate ||
105 S->getSCEVType() == scZeroExtend ||
106 S->getSCEVType() == scSignExtend;
110 //===--------------------------------------------------------------------===//
111 /// SCEVTruncateExpr - This class represents a truncation of an integer value
112 /// to a smaller integer value.
114 class SCEVTruncateExpr : public SCEVCastExpr {
115 friend class ScalarEvolution;
117 SCEVTruncateExpr(const SCEV* op, const Type *ty);
120 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
122 ScalarEvolution &SE) const {
123 const SCEV* H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
126 return SE.getTruncateExpr(H, Ty);
129 virtual void print(raw_ostream &OS) const;
131 /// Methods for support type inquiry through isa, cast, and dyn_cast:
132 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
133 static inline bool classof(const SCEV *S) {
134 return S->getSCEVType() == scTruncate;
138 //===--------------------------------------------------------------------===//
139 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
140 /// integer value to a larger integer value.
142 class SCEVZeroExtendExpr : public SCEVCastExpr {
143 friend class ScalarEvolution;
145 SCEVZeroExtendExpr(const SCEV* op, const Type *ty);
148 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
150 ScalarEvolution &SE) const {
151 const SCEV* 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 SCEV* op, const Type *ty);
176 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
178 ScalarEvolution &SE) const {
179 const SCEV* H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
182 return SE.getSignExtendExpr(H, Ty);
185 virtual void print(raw_ostream &OS) const;
187 /// Methods for support type inquiry through isa, cast, and dyn_cast:
188 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
189 static inline bool classof(const SCEV *S) {
190 return S->getSCEVType() == scSignExtend;
195 //===--------------------------------------------------------------------===//
196 /// SCEVNAryExpr - This node is a base class providing common
197 /// functionality for n'ary operators.
199 class SCEVNAryExpr : public SCEV {
201 SmallVector<const SCEV*, 8> Operands;
203 SCEVNAryExpr(enum SCEVTypes T, const SmallVectorImpl<const SCEV*> &ops)
204 : SCEV(T), Operands(ops.begin(), ops.end()) {}
207 virtual void Profile(FoldingSetNodeID &ID) const;
209 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
210 const SCEV* getOperand(unsigned i) const {
211 assert(i < Operands.size() && "Operand index out of range!");
215 const SmallVectorImpl<const SCEV*> &getOperands() const { return Operands; }
216 typedef SmallVectorImpl<const SCEV*>::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,
263 const SmallVectorImpl<const SCEV*> &ops)
264 : SCEVNAryExpr(T, ops) {}
267 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
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<const SCEV*> &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<const SCEV*> &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;
335 SCEVUDivExpr(const SCEV* lhs, const SCEV* rhs)
336 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
339 virtual void Profile(FoldingSetNodeID &ID) const;
341 const SCEV* getLHS() const { return LHS; }
342 const SCEV* getRHS() const { return RHS; }
344 virtual bool isLoopInvariant(const Loop *L) const {
345 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
348 virtual bool hasComputableLoopEvolution(const Loop *L) const {
349 return LHS->hasComputableLoopEvolution(L) &&
350 RHS->hasComputableLoopEvolution(L);
353 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
355 ScalarEvolution &SE) const {
356 const SCEV* L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
357 const SCEV* R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
358 if (L == LHS && R == RHS)
361 return SE.getUDivExpr(L, R);
364 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
366 virtual const Type *getType() const;
368 void print(raw_ostream &OS) const;
370 /// Methods for support type inquiry through isa, cast, and dyn_cast:
371 static inline bool classof(const SCEVUDivExpr *S) { return true; }
372 static inline bool classof(const SCEV *S) {
373 return S->getSCEVType() == scUDivExpr;
378 //===--------------------------------------------------------------------===//
379 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
380 /// count of the specified loop. This is the primary focus of the
381 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
382 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
383 /// created and analyzed.
385 /// All operands of an AddRec are required to be loop invariant.
387 class SCEVAddRecExpr : public SCEVNAryExpr {
388 friend class ScalarEvolution;
392 SCEVAddRecExpr(const SmallVectorImpl<const SCEV*> &ops, const Loop *l)
393 : SCEVNAryExpr(scAddRecExpr, ops), L(l) {
394 for (size_t i = 0, e = Operands.size(); i != e; ++i)
395 assert(Operands[i]->isLoopInvariant(l) &&
396 "Operands of AddRec must be loop-invariant!");
400 virtual void Profile(FoldingSetNodeID &ID) const;
402 const SCEV* getStart() const { return Operands[0]; }
403 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 const SCEV* getStepRecurrence(ScalarEvolution &SE) const {
409 if (isAffine()) return getOperand(1);
410 return SE.getAddRecExpr(SmallVector<const SCEV*, 3>(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 /// 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 const SCEV* evaluateAtIteration(const SCEV* 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 const SCEV* getNumIterationsInRange(ConstantRange Range,
447 ScalarEvolution &SE) const;
449 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
451 ScalarEvolution &SE) const;
453 virtual void print(raw_ostream &OS) const;
455 /// Methods for support type inquiry through isa, cast, and dyn_cast:
456 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
457 static inline bool classof(const SCEV *S) {
458 return S->getSCEVType() == scAddRecExpr;
463 //===--------------------------------------------------------------------===//
464 /// SCEVSMaxExpr - This class represents a signed maximum selection.
466 class SCEVSMaxExpr : public SCEVCommutativeExpr {
467 friend class ScalarEvolution;
469 explicit SCEVSMaxExpr(const SmallVectorImpl<const SCEV*> &ops)
470 : SCEVCommutativeExpr(scSMaxExpr, ops) {
474 virtual const char *getOperationStr() const { return " smax "; }
476 /// Methods for support type inquiry through isa, cast, and dyn_cast:
477 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
478 static inline bool classof(const SCEV *S) {
479 return S->getSCEVType() == scSMaxExpr;
484 //===--------------------------------------------------------------------===//
485 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
487 class SCEVUMaxExpr : public SCEVCommutativeExpr {
488 friend class ScalarEvolution;
490 explicit SCEVUMaxExpr(const SmallVectorImpl<const SCEV*> &ops)
491 : SCEVCommutativeExpr(scUMaxExpr, ops) {
495 virtual const char *getOperationStr() const { return " umax "; }
497 /// Methods for support type inquiry through isa, cast, and dyn_cast:
498 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
499 static inline bool classof(const SCEV *S) {
500 return S->getSCEVType() == scUMaxExpr;
505 //===--------------------------------------------------------------------===//
506 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
507 /// value, and only represent it as it's LLVM Value. This is the "bottom"
508 /// value for the analysis.
510 class SCEVUnknown : public SCEV {
511 friend class ScalarEvolution;
514 explicit SCEVUnknown(Value *v) :
515 SCEV(scUnknown), V(v) {}
518 virtual void Profile(FoldingSetNodeID &ID) const;
520 Value *getValue() const { return V; }
522 virtual bool isLoopInvariant(const Loop *L) const;
523 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
524 return false; // not computable
527 const SCEV* replaceSymbolicValuesWithConcrete(const SCEV* Sym,
529 ScalarEvolution &SE) const {
530 if (&*Sym == this) return Conc;
534 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
536 virtual const Type *getType() const;
538 virtual void print(raw_ostream &OS) const;
540 /// Methods for support type inquiry through isa, cast, and dyn_cast:
541 static inline bool classof(const SCEVUnknown *S) { return true; }
542 static inline bool classof(const SCEV *S) {
543 return S->getSCEVType() == scUnknown;
547 /// SCEVVisitor - This class defines a simple visitor class that may be used
548 /// for various SCEV analysis purposes.
549 template<typename SC, typename RetVal=void>
551 RetVal visit(const SCEV *S) {
552 switch (S->getSCEVType()) {
554 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
556 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
558 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
560 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
562 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
564 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
566 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
568 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
570 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
572 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
574 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
575 case scCouldNotCompute:
576 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
578 assert(0 && "Unknown SCEV type!");
583 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
584 assert(0 && "Invalid use of SCEVCouldNotCompute!");