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(std::ostream &OS) const;
67 void print(std::ostream *OS) const { if (OS) print(*OS); }
69 /// Methods for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const SCEVConstant *S) { return true; }
71 static inline bool classof(const SCEV *S) {
72 return S->getSCEVType() == scConstant;
76 //===--------------------------------------------------------------------===//
77 /// SCEVTruncateExpr - This class represents a truncation of an integer value
78 /// to a smaller integer value.
80 class SCEVTruncateExpr : public SCEV {
81 friend class ScalarEvolution;
85 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
86 virtual ~SCEVTruncateExpr();
88 const SCEVHandle &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 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
100 const SCEVHandle &Conc,
101 ScalarEvolution &SE) const {
102 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
105 return SE.getTruncateExpr(H, Ty);
108 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
110 virtual void print(std::ostream &OS) const;
111 void print(std::ostream *OS) const { if (OS) print(*OS); }
113 /// Methods for support type inquiry through isa, cast, and dyn_cast:
114 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
115 static inline bool classof(const SCEV *S) {
116 return S->getSCEVType() == scTruncate;
120 //===--------------------------------------------------------------------===//
121 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
122 /// integer value to a larger integer value.
124 class SCEVZeroExtendExpr : public SCEV {
125 friend class ScalarEvolution;
129 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
130 virtual ~SCEVZeroExtendExpr();
132 const SCEVHandle &getOperand() const { return Op; }
133 virtual const Type *getType() const { return Ty; }
135 virtual bool isLoopInvariant(const Loop *L) const {
136 return Op->isLoopInvariant(L);
139 virtual bool hasComputableLoopEvolution(const Loop *L) const {
140 return Op->hasComputableLoopEvolution(L);
143 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
144 const SCEVHandle &Conc,
145 ScalarEvolution &SE) const {
146 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
149 return SE.getZeroExtendExpr(H, Ty);
152 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
154 virtual void print(std::ostream &OS) const;
155 void print(std::ostream *OS) const { if (OS) print(*OS); }
157 /// Methods for support type inquiry through isa, cast, and dyn_cast:
158 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
159 static inline bool classof(const SCEV *S) {
160 return S->getSCEVType() == scZeroExtend;
164 //===--------------------------------------------------------------------===//
165 /// SCEVSignExtendExpr - This class represents a sign extension of a small
166 /// integer value to a larger integer value.
168 class SCEVSignExtendExpr : public SCEV {
169 friend class ScalarEvolution;
173 SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty);
174 virtual ~SCEVSignExtendExpr();
176 const SCEVHandle &getOperand() const { return Op; }
177 virtual const Type *getType() const { return Ty; }
179 virtual bool isLoopInvariant(const Loop *L) const {
180 return Op->isLoopInvariant(L);
183 virtual bool hasComputableLoopEvolution(const Loop *L) const {
184 return Op->hasComputableLoopEvolution(L);
187 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
188 const SCEVHandle &Conc,
189 ScalarEvolution &SE) const {
190 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
193 return SE.getSignExtendExpr(H, Ty);
196 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
198 virtual void print(std::ostream &OS) const;
199 void print(std::ostream *OS) const { if (OS) print(*OS); }
201 /// Methods for support type inquiry through isa, cast, and dyn_cast:
202 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
203 static inline bool classof(const SCEV *S) {
204 return S->getSCEVType() == scSignExtend;
209 //===--------------------------------------------------------------------===//
210 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
213 class SCEVCommutativeExpr : public SCEV {
214 friend class ScalarEvolution;
216 std::vector<SCEVHandle> Operands;
219 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
221 Operands.reserve(ops.size());
222 Operands.insert(Operands.end(), ops.begin(), ops.end());
224 ~SCEVCommutativeExpr();
227 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
228 const SCEVHandle &getOperand(unsigned i) const {
229 assert(i < Operands.size() && "Operand index out of range!");
233 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
234 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
235 op_iterator op_begin() const { return Operands.begin(); }
236 op_iterator op_end() const { return Operands.end(); }
239 virtual bool isLoopInvariant(const Loop *L) const {
240 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
241 if (!getOperand(i)->isLoopInvariant(L)) return false;
245 // hasComputableLoopEvolution - Commutative expressions have computable loop
246 // evolutions iff they have at least one operand that varies with the loop,
247 // but that all varying operands are computable.
248 virtual bool hasComputableLoopEvolution(const Loop *L) const {
249 bool HasVarying = false;
250 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
251 if (!getOperand(i)->isLoopInvariant(L)) {
252 if (getOperand(i)->hasComputableLoopEvolution(L))
260 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
261 const SCEVHandle &Conc,
262 ScalarEvolution &SE) const;
264 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
266 virtual const char *getOperationStr() const = 0;
268 virtual const Type *getType() const { return getOperand(0)->getType(); }
269 virtual void print(std::ostream &OS) const;
270 void print(std::ostream *OS) const { if (OS) print(*OS); }
272 /// Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
274 static inline bool classof(const SCEV *S) {
275 return S->getSCEVType() == scAddExpr ||
276 S->getSCEVType() == scMulExpr ||
277 S->getSCEVType() == scSMaxExpr ||
278 S->getSCEVType() == scUMaxExpr;
283 //===--------------------------------------------------------------------===//
284 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
286 class SCEVAddExpr : public SCEVCommutativeExpr {
287 friend class ScalarEvolution;
289 explicit SCEVAddExpr(const std::vector<SCEVHandle> &ops)
290 : SCEVCommutativeExpr(scAddExpr, ops) {
294 virtual const char *getOperationStr() const { return " + "; }
296 /// Methods for support type inquiry through isa, cast, and dyn_cast:
297 static inline bool classof(const SCEVAddExpr *S) { return true; }
298 static inline bool classof(const SCEV *S) {
299 return S->getSCEVType() == scAddExpr;
303 //===--------------------------------------------------------------------===//
304 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
306 class SCEVMulExpr : public SCEVCommutativeExpr {
307 friend class ScalarEvolution;
309 explicit SCEVMulExpr(const std::vector<SCEVHandle> &ops)
310 : SCEVCommutativeExpr(scMulExpr, ops) {
314 virtual const char *getOperationStr() const { return " * "; }
316 /// Methods for support type inquiry through isa, cast, and dyn_cast:
317 static inline bool classof(const SCEVMulExpr *S) { return true; }
318 static inline bool classof(const SCEV *S) {
319 return S->getSCEVType() == scMulExpr;
324 //===--------------------------------------------------------------------===//
325 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
327 class SCEVUDivExpr : public SCEV {
328 friend class ScalarEvolution;
331 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
332 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
334 virtual ~SCEVUDivExpr();
336 const SCEVHandle &getLHS() const { return LHS; }
337 const SCEVHandle &getRHS() const { return RHS; }
339 virtual bool isLoopInvariant(const Loop *L) const {
340 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
343 virtual bool hasComputableLoopEvolution(const Loop *L) const {
344 return LHS->hasComputableLoopEvolution(L) &&
345 RHS->hasComputableLoopEvolution(L);
348 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
349 const SCEVHandle &Conc,
350 ScalarEvolution &SE) const {
351 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
352 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
353 if (L == LHS && R == RHS)
356 return SE.getUDivExpr(L, R);
359 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
361 virtual const Type *getType() const;
363 void print(std::ostream &OS) const;
364 void print(std::ostream *OS) const { if (OS) print(*OS); }
366 /// Methods for support type inquiry through isa, cast, and dyn_cast:
367 static inline bool classof(const SCEVUDivExpr *S) { return true; }
368 static inline bool classof(const SCEV *S) {
369 return S->getSCEVType() == scUDivExpr;
374 //===--------------------------------------------------------------------===//
375 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
376 /// count of the specified loop.
378 /// All operands of an AddRec are required to be loop invariant.
380 class SCEVAddRecExpr : public SCEV {
381 friend class ScalarEvolution;
383 std::vector<SCEVHandle> Operands;
386 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
387 : SCEV(scAddRecExpr), Operands(ops), L(l) {
388 for (size_t i = 0, e = Operands.size(); i != e; ++i)
389 assert(Operands[i]->isLoopInvariant(l) &&
390 "Operands of AddRec must be loop-invariant!");
394 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
395 op_iterator op_begin() const { return Operands.begin(); }
396 op_iterator op_end() const { return Operands.end(); }
398 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
399 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
400 const SCEVHandle &getStart() const { return Operands[0]; }
401 const Loop *getLoop() const { return L; }
404 /// getStepRecurrence - This method constructs and returns the recurrence
405 /// indicating how much this expression steps by. If this is a polynomial
406 /// of degree N, it returns a chrec of degree N-1.
407 SCEVHandle getStepRecurrence(ScalarEvolution &SE) const {
408 if (isAffine()) return getOperand(1);
409 return SE.getAddRecExpr(std::vector<SCEVHandle>(op_begin()+1,op_end()),
413 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
414 if (L == QL) return true;
418 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
420 virtual const Type *getType() const { return Operands[0]->getType(); }
422 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
423 /// an expressions A+B*x where A and B are loop invariant values.
424 bool isAffine() const {
425 // We know that the start value is invariant. This expression is thus
426 // affine iff the step is also invariant.
427 return getNumOperands() == 2;
430 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
431 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
432 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
433 bool isQuadratic() const {
434 return getNumOperands() == 3;
437 /// evaluateAtIteration - Return the value of this chain of recurrences at
438 /// the specified iteration number.
439 SCEVHandle evaluateAtIteration(SCEVHandle It, ScalarEvolution &SE) const;
441 /// getNumIterationsInRange - Return the number of iterations of this loop
442 /// that produce values in the specified constant range. Another way of
443 /// looking at this is that it returns the first iteration number where the
444 /// value is not in the condition, thus computing the exit count. If the
445 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
447 SCEVHandle getNumIterationsInRange(ConstantRange Range,
448 ScalarEvolution &SE) const;
450 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
451 const SCEVHandle &Conc,
452 ScalarEvolution &SE) const;
454 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
456 virtual void print(std::ostream &OS) const;
457 void print(std::ostream *OS) const { if (OS) print(*OS); }
459 /// Methods for support type inquiry through isa, cast, and dyn_cast:
460 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
461 static inline bool classof(const SCEV *S) {
462 return S->getSCEVType() == scAddRecExpr;
467 //===--------------------------------------------------------------------===//
468 /// SCEVSMaxExpr - This class represents a signed maximum selection.
470 class SCEVSMaxExpr : public SCEVCommutativeExpr {
471 friend class ScalarEvolution;
473 explicit SCEVSMaxExpr(const std::vector<SCEVHandle> &ops)
474 : SCEVCommutativeExpr(scSMaxExpr, ops) {
478 virtual const char *getOperationStr() const { return " smax "; }
480 /// Methods for support type inquiry through isa, cast, and dyn_cast:
481 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
482 static inline bool classof(const SCEV *S) {
483 return S->getSCEVType() == scSMaxExpr;
488 //===--------------------------------------------------------------------===//
489 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
491 class SCEVUMaxExpr : public SCEVCommutativeExpr {
492 friend class ScalarEvolution;
494 explicit SCEVUMaxExpr(const std::vector<SCEVHandle> &ops)
495 : SCEVCommutativeExpr(scUMaxExpr, ops) {
499 virtual const char *getOperationStr() const { return " umax "; }
501 /// Methods for support type inquiry through isa, cast, and dyn_cast:
502 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
503 static inline bool classof(const SCEV *S) {
504 return S->getSCEVType() == scUMaxExpr;
509 //===--------------------------------------------------------------------===//
510 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
511 /// value, and only represent it as it's LLVM Value. This is the "bottom"
512 /// value for the analysis.
514 class SCEVUnknown : public SCEV {
515 friend class ScalarEvolution;
518 explicit SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
523 Value *getValue() const { return V; }
525 virtual bool isLoopInvariant(const Loop *L) const;
526 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
527 return false; // not computable
530 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
531 const SCEVHandle &Conc,
532 ScalarEvolution &SE) const {
533 if (&*Sym == this) return Conc;
537 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
539 virtual const Type *getType() const;
541 virtual void print(std::ostream &OS) const;
542 void print(std::ostream *OS) const { if (OS) print(*OS); }
544 /// Methods for support type inquiry through isa, cast, and dyn_cast:
545 static inline bool classof(const SCEVUnknown *S) { return true; }
546 static inline bool classof(const SCEV *S) {
547 return S->getSCEVType() == scUnknown;
551 /// SCEVVisitor - This class defines a simple visitor class that may be used
552 /// for various SCEV analysis purposes.
553 template<typename SC, typename RetVal=void>
555 RetVal visit(const SCEV *S) {
556 switch (S->getSCEVType()) {
558 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
560 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
562 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
564 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
566 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
568 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
570 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
572 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
574 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
576 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
578 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
579 case scCouldNotCompute:
580 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
582 assert(0 && "Unknown SCEV type!");
587 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
588 assert(0 && "Invalid use of SCEVCouldNotCompute!");