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 virtual void print(std::ostream &OS) const;
62 void print(std::ostream *OS) const { if (OS) print(*OS); }
64 /// Methods for support type inquiry through isa, cast, and dyn_cast:
65 static inline bool classof(const SCEVConstant *S) { return true; }
66 static inline bool classof(const SCEV *S) {
67 return S->getSCEVType() == scConstant;
71 //===--------------------------------------------------------------------===//
72 /// SCEVTruncateExpr - This class represents a truncation of an integer value
73 /// to a smaller integer value.
75 class SCEVTruncateExpr : public SCEV {
76 friend class ScalarEvolution;
80 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
81 virtual ~SCEVTruncateExpr();
83 const SCEVHandle &getOperand() const { return Op; }
84 virtual const Type *getType() const { return Ty; }
86 virtual bool isLoopInvariant(const Loop *L) const {
87 return Op->isLoopInvariant(L);
90 virtual bool hasComputableLoopEvolution(const Loop *L) const {
91 return Op->hasComputableLoopEvolution(L);
94 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
95 const SCEVHandle &Conc,
96 ScalarEvolution &SE) const {
97 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
100 return SE.getTruncateExpr(H, Ty);
103 virtual void print(std::ostream &OS) const;
104 void print(std::ostream *OS) const { if (OS) print(*OS); }
106 /// Methods for support type inquiry through isa, cast, and dyn_cast:
107 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
108 static inline bool classof(const SCEV *S) {
109 return S->getSCEVType() == scTruncate;
113 //===--------------------------------------------------------------------===//
114 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
115 /// integer value to a larger integer value.
117 class SCEVZeroExtendExpr : public SCEV {
118 friend class ScalarEvolution;
122 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
123 virtual ~SCEVZeroExtendExpr();
125 const SCEVHandle &getOperand() const { return Op; }
126 virtual const Type *getType() const { return Ty; }
128 virtual bool isLoopInvariant(const Loop *L) const {
129 return Op->isLoopInvariant(L);
132 virtual bool hasComputableLoopEvolution(const Loop *L) const {
133 return Op->hasComputableLoopEvolution(L);
136 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
137 const SCEVHandle &Conc,
138 ScalarEvolution &SE) const {
139 SCEVHandle H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
142 return SE.getZeroExtendExpr(H, Ty);
145 virtual void print(std::ostream &OS) const;
146 void print(std::ostream *OS) const { if (OS) print(*OS); }
148 /// Methods for support type inquiry through isa, cast, and dyn_cast:
149 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
150 static inline bool classof(const SCEV *S) {
151 return S->getSCEVType() == scZeroExtend;
155 //===--------------------------------------------------------------------===//
156 /// SCEVSignExtendExpr - This class represents a sign extension of a small
157 /// integer value to a larger integer value.
159 class SCEVSignExtendExpr : public SCEV {
160 friend class ScalarEvolution;
164 SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty);
165 virtual ~SCEVSignExtendExpr();
167 const SCEVHandle &getOperand() const { return Op; }
168 virtual const Type *getType() const { return Ty; }
170 virtual bool isLoopInvariant(const Loop *L) const {
171 return Op->isLoopInvariant(L);
174 virtual bool hasComputableLoopEvolution(const Loop *L) const {
175 return Op->hasComputableLoopEvolution(L);
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(std::ostream &OS) const;
188 void print(std::ostream *OS) const { if (OS) print(*OS); }
190 /// Methods for support type inquiry through isa, cast, and dyn_cast:
191 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
192 static inline bool classof(const SCEV *S) {
193 return S->getSCEVType() == scSignExtend;
198 //===--------------------------------------------------------------------===//
199 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
202 class SCEVCommutativeExpr : public SCEV {
203 friend class ScalarEvolution;
205 std::vector<SCEVHandle> Operands;
208 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
210 Operands.reserve(ops.size());
211 Operands.insert(Operands.end(), ops.begin(), ops.end());
213 ~SCEVCommutativeExpr();
216 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
217 const SCEVHandle &getOperand(unsigned i) const {
218 assert(i < Operands.size() && "Operand index out of range!");
222 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
223 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
224 op_iterator op_begin() const { return Operands.begin(); }
225 op_iterator op_end() const { return Operands.end(); }
228 virtual bool isLoopInvariant(const Loop *L) const {
229 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
230 if (!getOperand(i)->isLoopInvariant(L)) return false;
234 // hasComputableLoopEvolution - Commutative expressions have computable loop
235 // evolutions iff they have at least one operand that varies with the loop,
236 // but that all varying operands are computable.
237 virtual bool hasComputableLoopEvolution(const Loop *L) const {
238 bool HasVarying = false;
239 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
240 if (!getOperand(i)->isLoopInvariant(L)) {
241 if (getOperand(i)->hasComputableLoopEvolution(L))
249 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
250 const SCEVHandle &Conc,
251 ScalarEvolution &SE) const;
253 virtual const char *getOperationStr() const = 0;
255 virtual const Type *getType() const { return getOperand(0)->getType(); }
256 virtual void print(std::ostream &OS) const;
257 void print(std::ostream *OS) const { if (OS) print(*OS); }
259 /// Methods for support type inquiry through isa, cast, and dyn_cast:
260 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
261 static inline bool classof(const SCEV *S) {
262 return S->getSCEVType() == scAddExpr ||
263 S->getSCEVType() == scMulExpr ||
264 S->getSCEVType() == scSMaxExpr ||
265 S->getSCEVType() == scUMaxExpr;
270 //===--------------------------------------------------------------------===//
271 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
273 class SCEVAddExpr : public SCEVCommutativeExpr {
274 friend class ScalarEvolution;
276 explicit SCEVAddExpr(const std::vector<SCEVHandle> &ops)
277 : SCEVCommutativeExpr(scAddExpr, ops) {
281 virtual const char *getOperationStr() const { return " + "; }
283 /// Methods for support type inquiry through isa, cast, and dyn_cast:
284 static inline bool classof(const SCEVAddExpr *S) { return true; }
285 static inline bool classof(const SCEV *S) {
286 return S->getSCEVType() == scAddExpr;
290 //===--------------------------------------------------------------------===//
291 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
293 class SCEVMulExpr : public SCEVCommutativeExpr {
294 friend class ScalarEvolution;
296 explicit SCEVMulExpr(const std::vector<SCEVHandle> &ops)
297 : SCEVCommutativeExpr(scMulExpr, ops) {
301 virtual const char *getOperationStr() const { return " * "; }
303 /// Methods for support type inquiry through isa, cast, and dyn_cast:
304 static inline bool classof(const SCEVMulExpr *S) { return true; }
305 static inline bool classof(const SCEV *S) {
306 return S->getSCEVType() == scMulExpr;
311 //===--------------------------------------------------------------------===//
312 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
314 class SCEVUDivExpr : public SCEV {
315 friend class ScalarEvolution;
318 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
319 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
321 virtual ~SCEVUDivExpr();
323 const SCEVHandle &getLHS() const { return LHS; }
324 const SCEVHandle &getRHS() const { return RHS; }
326 virtual bool isLoopInvariant(const Loop *L) const {
327 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
330 virtual bool hasComputableLoopEvolution(const Loop *L) const {
331 return LHS->hasComputableLoopEvolution(L) &&
332 RHS->hasComputableLoopEvolution(L);
335 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
336 const SCEVHandle &Conc,
337 ScalarEvolution &SE) const {
338 SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
339 SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
340 if (L == LHS && R == RHS)
343 return SE.getUDivExpr(L, R);
347 virtual const Type *getType() const;
349 void print(std::ostream &OS) const;
350 void print(std::ostream *OS) const { if (OS) print(*OS); }
352 /// Methods for support type inquiry through isa, cast, and dyn_cast:
353 static inline bool classof(const SCEVUDivExpr *S) { return true; }
354 static inline bool classof(const SCEV *S) {
355 return S->getSCEVType() == scUDivExpr;
360 //===--------------------------------------------------------------------===//
361 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
362 /// count of the specified loop.
364 /// All operands of an AddRec are required to be loop invariant.
366 class SCEVAddRecExpr : public SCEV {
367 friend class ScalarEvolution;
369 std::vector<SCEVHandle> Operands;
372 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
373 : SCEV(scAddRecExpr), Operands(ops), L(l) {
374 for (size_t i = 0, e = Operands.size(); i != e; ++i)
375 assert(Operands[i]->isLoopInvariant(l) &&
376 "Operands of AddRec must be loop-invariant!");
380 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
381 op_iterator op_begin() const { return Operands.begin(); }
382 op_iterator op_end() const { return Operands.end(); }
384 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
385 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
386 const SCEVHandle &getStart() const { return Operands[0]; }
387 const Loop *getLoop() const { return L; }
390 /// getStepRecurrence - This method constructs and returns the recurrence
391 /// indicating how much this expression steps by. If this is a polynomial
392 /// of degree N, it returns a chrec of degree N-1.
393 SCEVHandle getStepRecurrence(ScalarEvolution &SE) const {
394 if (isAffine()) return getOperand(1);
395 return SE.getAddRecExpr(std::vector<SCEVHandle>(op_begin()+1,op_end()),
399 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
400 if (L == QL) return true;
404 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
406 virtual const Type *getType() const { return Operands[0]->getType(); }
408 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
409 /// an expressions A+B*x where A and B are loop invariant values.
410 bool isAffine() const {
411 // We know that the start value is invariant. This expression is thus
412 // affine iff the step is also invariant.
413 return getNumOperands() == 2;
416 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
417 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
418 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
419 bool isQuadratic() const {
420 return getNumOperands() == 3;
423 /// evaluateAtIteration - Return the value of this chain of recurrences at
424 /// the specified iteration number.
425 SCEVHandle evaluateAtIteration(SCEVHandle It, ScalarEvolution &SE) const;
427 /// getNumIterationsInRange - Return the number of iterations of this loop
428 /// that produce values in the specified constant range. Another way of
429 /// looking at this is that it returns the first iteration number where the
430 /// value is not in the condition, thus computing the exit count. If the
431 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
433 SCEVHandle getNumIterationsInRange(ConstantRange Range,
434 ScalarEvolution &SE) const;
436 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
437 const SCEVHandle &Conc,
438 ScalarEvolution &SE) const;
440 virtual void print(std::ostream &OS) const;
441 void print(std::ostream *OS) const { if (OS) print(*OS); }
443 /// Methods for support type inquiry through isa, cast, and dyn_cast:
444 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
445 static inline bool classof(const SCEV *S) {
446 return S->getSCEVType() == scAddRecExpr;
451 //===--------------------------------------------------------------------===//
452 /// SCEVSMaxExpr - This class represents a signed maximum selection.
454 class SCEVSMaxExpr : public SCEVCommutativeExpr {
455 friend class ScalarEvolution;
457 explicit SCEVSMaxExpr(const std::vector<SCEVHandle> &ops)
458 : SCEVCommutativeExpr(scSMaxExpr, ops) {
462 virtual const char *getOperationStr() const { return " smax "; }
464 /// Methods for support type inquiry through isa, cast, and dyn_cast:
465 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
466 static inline bool classof(const SCEV *S) {
467 return S->getSCEVType() == scSMaxExpr;
472 //===--------------------------------------------------------------------===//
473 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
475 class SCEVUMaxExpr : public SCEVCommutativeExpr {
476 friend class ScalarEvolution;
478 explicit SCEVUMaxExpr(const std::vector<SCEVHandle> &ops)
479 : SCEVCommutativeExpr(scUMaxExpr, ops) {
483 virtual const char *getOperationStr() const { return " umax "; }
485 /// Methods for support type inquiry through isa, cast, and dyn_cast:
486 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
487 static inline bool classof(const SCEV *S) {
488 return S->getSCEVType() == scUMaxExpr;
493 //===--------------------------------------------------------------------===//
494 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
495 /// value, and only represent it as it's LLVM Value. This is the "bottom"
496 /// value for the analysis.
498 class SCEVUnknown : public SCEV {
499 friend class ScalarEvolution;
502 explicit SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
507 Value *getValue() const { return V; }
509 virtual bool isLoopInvariant(const Loop *L) const;
510 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
511 return false; // not computable
514 SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
515 const SCEVHandle &Conc,
516 ScalarEvolution &SE) const {
517 if (&*Sym == this) return Conc;
521 virtual const Type *getType() const;
523 virtual void print(std::ostream &OS) const;
524 void print(std::ostream *OS) const { if (OS) print(*OS); }
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(SCEV *S) {
538 switch (S->getSCEVType()) {
540 return ((SC*)this)->visitConstant((SCEVConstant*)S);
542 return ((SC*)this)->visitTruncateExpr((SCEVTruncateExpr*)S);
544 return ((SC*)this)->visitZeroExtendExpr((SCEVZeroExtendExpr*)S);
546 return ((SC*)this)->visitSignExtendExpr((SCEVSignExtendExpr*)S);
548 return ((SC*)this)->visitAddExpr((SCEVAddExpr*)S);
550 return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
552 return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S);
554 return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
556 return ((SC*)this)->visitSMaxExpr((SCEVSMaxExpr*)S);
558 return ((SC*)this)->visitUMaxExpr((SCEVUMaxExpr*)S);
560 return ((SC*)this)->visitUnknown((SCEVUnknown*)S);
561 case scCouldNotCompute:
562 return ((SC*)this)->visitCouldNotCompute((SCEVCouldNotCompute*)S);
564 assert(0 && "Unknown SCEV type!");
569 RetVal visitCouldNotCompute(SCEVCouldNotCompute *S) {
570 assert(0 && "Invalid use of SCEVCouldNotCompute!");