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"
26 // These should be ordered in terms of increasing complexity to make the
28 scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
29 scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr, scUnknown,
33 //===--------------------------------------------------------------------===//
34 /// SCEVConstant - This class represents a constant integer value.
36 class SCEVConstant : public SCEV {
37 friend class ScalarEvolution;
40 explicit SCEVConstant(ConstantInt *v) : SCEV(scConstant), V(v) {}
42 virtual ~SCEVConstant();
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 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
57 const SCEVHandle &Conc,
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 SCEVHandle &op, const Type *ty);
84 virtual ~SCEVCastExpr();
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 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
100 /// Methods for support type inquiry through isa, cast, and dyn_cast:
101 static inline bool classof(const SCEVCastExpr *S) { return true; }
102 static inline bool classof(const SCEV *S) {
103 return S->getSCEVType() == scTruncate ||
104 S->getSCEVType() == scZeroExtend ||
105 S->getSCEVType() == scSignExtend;
109 //===--------------------------------------------------------------------===//
110 /// SCEVTruncateExpr - This class represents a truncation of an integer value
111 /// to a smaller integer value.
113 class SCEVTruncateExpr : public SCEVCastExpr {
114 friend class ScalarEvolution;
116 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
117 virtual ~SCEVTruncateExpr();
120 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
121 const SCEVHandle &Conc,
122 ScalarEvolution &SE) const {
123 SCEVHandle 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 SCEVHandle &op, const Type *ty);
146 virtual ~SCEVZeroExtendExpr();
149 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
150 const SCEVHandle &Conc,
151 ScalarEvolution &SE) const {
152 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
155 return SE.getZeroExtendExpr(H, Ty);
158 virtual void print(raw_ostream &OS) const;
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 SCEVCastExpr {
172 friend class ScalarEvolution;
174 SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty);
175 virtual ~SCEVSignExtendExpr();
178 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
179 const SCEVHandle &Conc,
180 ScalarEvolution &SE) const {
181 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
184 return SE.getSignExtendExpr(H, Ty);
187 virtual void print(raw_ostream &OS) const;
189 /// Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
191 static inline bool classof(const SCEV *S) {
192 return S->getSCEVType() == scSignExtend;
197 //===--------------------------------------------------------------------===//
198 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
201 class SCEVCommutativeExpr : public SCEV {
202 std::vector<SCEVHandle> Operands;
205 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
207 Operands.reserve(ops.size());
208 Operands.insert(Operands.end(), ops.begin(), ops.end());
210 ~SCEVCommutativeExpr();
213 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
214 const SCEVHandle &getOperand(unsigned i) const {
215 assert(i < Operands.size() && "Operand index out of range!");
219 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
220 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
221 op_iterator op_begin() const { return Operands.begin(); }
222 op_iterator op_end() const { return Operands.end(); }
225 virtual bool isLoopInvariant(const Loop *L) const {
226 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
227 if (!getOperand(i)->isLoopInvariant(L)) return false;
231 // hasComputableLoopEvolution - Commutative expressions have computable loop
232 // evolutions iff they have at least one operand that varies with the loop,
233 // but that all varying operands are computable.
234 virtual bool hasComputableLoopEvolution(const Loop *L) const {
235 bool HasVarying = false;
236 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
237 if (!getOperand(i)->isLoopInvariant(L)) {
238 if (getOperand(i)->hasComputableLoopEvolution(L))
246 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
247 const SCEVHandle &Conc,
248 ScalarEvolution &SE) const;
250 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
252 virtual const char *getOperationStr() const = 0;
254 virtual const Type *getType() const { return getOperand(0)->getType(); }
255 virtual void print(raw_ostream &OS) const;
257 /// Methods for support type inquiry through isa, cast, and dyn_cast:
258 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
259 static inline bool classof(const SCEV *S) {
260 return S->getSCEVType() == scAddExpr ||
261 S->getSCEVType() == scMulExpr ||
262 S->getSCEVType() == scSMaxExpr ||
263 S->getSCEVType() == scUMaxExpr;
268 //===--------------------------------------------------------------------===//
269 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
271 class SCEVAddExpr : public SCEVCommutativeExpr {
272 friend class ScalarEvolution;
274 explicit SCEVAddExpr(const std::vector<SCEVHandle> &ops)
275 : SCEVCommutativeExpr(scAddExpr, ops) {
279 virtual const char *getOperationStr() const { return " + "; }
281 /// Methods for support type inquiry through isa, cast, and dyn_cast:
282 static inline bool classof(const SCEVAddExpr *S) { return true; }
283 static inline bool classof(const SCEV *S) {
284 return S->getSCEVType() == scAddExpr;
288 //===--------------------------------------------------------------------===//
289 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
291 class SCEVMulExpr : public SCEVCommutativeExpr {
292 friend class ScalarEvolution;
294 explicit SCEVMulExpr(const std::vector<SCEVHandle> &ops)
295 : SCEVCommutativeExpr(scMulExpr, ops) {
299 virtual const char *getOperationStr() const { return " * "; }
301 /// Methods for support type inquiry through isa, cast, and dyn_cast:
302 static inline bool classof(const SCEVMulExpr *S) { return true; }
303 static inline bool classof(const SCEV *S) {
304 return S->getSCEVType() == scMulExpr;
309 //===--------------------------------------------------------------------===//
310 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
312 class SCEVUDivExpr : public SCEV {
313 friend class ScalarEvolution;
316 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
317 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
319 virtual ~SCEVUDivExpr();
321 const SCEVHandle &getLHS() const { return LHS; }
322 const SCEVHandle &getRHS() const { return RHS; }
324 virtual bool isLoopInvariant(const Loop *L) const {
325 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
328 virtual bool hasComputableLoopEvolution(const Loop *L) const {
329 return LHS->hasComputableLoopEvolution(L) &&
330 RHS->hasComputableLoopEvolution(L);
333 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
334 const SCEVHandle &Conc,
335 ScalarEvolution &SE) const {
336 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
337 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
338 if (L == LHS && R == RHS)
341 return SE.getUDivExpr(L, R);
344 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
346 virtual const Type *getType() const;
348 void print(raw_ostream &OS) const;
350 /// Methods for support type inquiry through isa, cast, and dyn_cast:
351 static inline bool classof(const SCEVUDivExpr *S) { return true; }
352 static inline bool classof(const SCEV *S) {
353 return S->getSCEVType() == scUDivExpr;
358 //===--------------------------------------------------------------------===//
359 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
360 /// count of the specified loop.
362 /// All operands of an AddRec are required to be loop invariant.
364 class SCEVAddRecExpr : public SCEV {
365 friend class ScalarEvolution;
367 std::vector<SCEVHandle> Operands;
370 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
371 : SCEV(scAddRecExpr), Operands(ops), L(l) {
372 for (size_t i = 0, e = Operands.size(); i != e; ++i)
373 assert(Operands[i]->isLoopInvariant(l) &&
374 "Operands of AddRec must be loop-invariant!");
378 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
379 op_iterator op_begin() const { return Operands.begin(); }
380 op_iterator op_end() const { return Operands.end(); }
382 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
383 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
384 const SCEVHandle &getStart() const { return Operands[0]; }
385 const Loop *getLoop() const { return L; }
388 /// getStepRecurrence - This method constructs and returns the recurrence
389 /// indicating how much this expression steps by. If this is a polynomial
390 /// of degree N, it returns a chrec of degree N-1.
391 SCEVHandle getStepRecurrence(ScalarEvolution &SE) const {
392 if (isAffine()) return getOperand(1);
393 return SE.getAddRecExpr(std::vector<SCEVHandle>(op_begin()+1,op_end()),
397 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
398 if (L == QL) return true;
402 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
404 virtual const Type *getType() const { return Operands[0]->getType(); }
406 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
407 /// an expressions A+B*x where A and B are loop invariant values.
408 bool isAffine() const {
409 // We know that the start value is invariant. This expression is thus
410 // affine iff the step is also invariant.
411 return getNumOperands() == 2;
414 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
415 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
416 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
417 bool isQuadratic() const {
418 return getNumOperands() == 3;
421 /// evaluateAtIteration - Return the value of this chain of recurrences at
422 /// the specified iteration number.
423 SCEVHandle evaluateAtIteration(SCEVHandle It, ScalarEvolution &SE) const;
425 /// getNumIterationsInRange - Return the number of iterations of this loop
426 /// that produce values in the specified constant range. Another way of
427 /// looking at this is that it returns the first iteration number where the
428 /// value is not in the condition, thus computing the exit count. If the
429 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
431 SCEVHandle getNumIterationsInRange(ConstantRange Range,
432 ScalarEvolution &SE) const;
434 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
435 const SCEVHandle &Conc,
436 ScalarEvolution &SE) const;
438 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
440 virtual void print(raw_ostream &OS) const;
442 /// Methods for support type inquiry through isa, cast, and dyn_cast:
443 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
444 static inline bool classof(const SCEV *S) {
445 return S->getSCEVType() == scAddRecExpr;
450 //===--------------------------------------------------------------------===//
451 /// SCEVSMaxExpr - This class represents a signed maximum selection.
453 class SCEVSMaxExpr : public SCEVCommutativeExpr {
454 friend class ScalarEvolution;
456 explicit SCEVSMaxExpr(const std::vector<SCEVHandle> &ops)
457 : SCEVCommutativeExpr(scSMaxExpr, ops) {
461 virtual const char *getOperationStr() const { return " smax "; }
463 /// Methods for support type inquiry through isa, cast, and dyn_cast:
464 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
465 static inline bool classof(const SCEV *S) {
466 return S->getSCEVType() == scSMaxExpr;
471 //===--------------------------------------------------------------------===//
472 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
474 class SCEVUMaxExpr : public SCEVCommutativeExpr {
475 friend class ScalarEvolution;
477 explicit SCEVUMaxExpr(const std::vector<SCEVHandle> &ops)
478 : SCEVCommutativeExpr(scUMaxExpr, ops) {
482 virtual const char *getOperationStr() const { return " umax "; }
484 /// Methods for support type inquiry through isa, cast, and dyn_cast:
485 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
486 static inline bool classof(const SCEV *S) {
487 return S->getSCEVType() == scUMaxExpr;
492 //===--------------------------------------------------------------------===//
493 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
494 /// value, and only represent it as it's LLVM Value. This is the "bottom"
495 /// value for the analysis.
497 class SCEVUnknown : public SCEV {
498 friend class ScalarEvolution;
501 explicit SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
506 Value *getValue() const { return V; }
508 virtual bool isLoopInvariant(const Loop *L) const;
509 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
510 return false; // not computable
513 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
514 const SCEVHandle &Conc,
515 ScalarEvolution &SE) const {
516 if (&*Sym == this) return Conc;
520 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
522 virtual const Type *getType() const;
524 virtual void print(raw_ostream &OS) const;
526 /// Methods for support type inquiry through isa, cast, and dyn_cast:
527 static inline bool classof(const SCEVUnknown *S) { return true; }
528 static inline bool classof(const SCEV *S) {
529 return S->getSCEVType() == scUnknown;
533 /// SCEVVisitor - This class defines a simple visitor class that may be used
534 /// for various SCEV analysis purposes.
535 template<typename SC, typename RetVal=void>
537 RetVal visit(const SCEV *S) {
538 switch (S->getSCEVType()) {
540 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
542 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
544 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
546 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
548 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
550 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
552 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
554 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
556 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
558 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
560 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
561 case scCouldNotCompute:
562 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
564 assert(0 && "Unknown SCEV type!");
569 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
570 assert(0 && "Invalid use of SCEVCouldNotCompute!");