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"
18 #include "llvm/Support/ErrorHandling.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) :
41 SCEV(scConstant), V(v) {}
43 virtual void Profile(FoldingSetNodeID &ID) const;
45 ConstantInt *getValue() const { return V; }
47 virtual bool isLoopInvariant(const Loop *L) const {
51 virtual bool hasComputableLoopEvolution(const Loop *L) const {
52 return false; // Not loop variant
55 virtual const Type *getType() const;
57 const SCEV *replaceSymbolicValuesWithConcrete(const SCEV *Sym,
59 ScalarEvolution &SE) const {
63 bool dominates(BasicBlock *BB, DominatorTree *DT) const {
67 virtual void print(raw_ostream &OS) const;
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 /// SCEVCastExpr - This is the base class for unary cast operator classes.
79 class SCEVCastExpr : public SCEV {
84 SCEVCastExpr(unsigned SCEVTy, const SCEV *op, const Type *ty);
87 virtual void Profile(FoldingSetNodeID &ID) const;
89 const SCEV *getOperand() const { return Op; }
90 virtual const Type *getType() const { return Ty; }
92 virtual bool isLoopInvariant(const Loop *L) const {
93 return Op->isLoopInvariant(L);
96 virtual bool hasComputableLoopEvolution(const Loop *L) const {
97 return Op->hasComputableLoopEvolution(L);
100 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const;
102 /// Methods for support type inquiry through isa, cast, and dyn_cast:
103 static inline bool classof(const SCEVCastExpr *S) { return true; }
104 static inline bool classof(const SCEV *S) {
105 return S->getSCEVType() == scTruncate ||
106 S->getSCEVType() == scZeroExtend ||
107 S->getSCEVType() == scSignExtend;
111 //===--------------------------------------------------------------------===//
112 /// SCEVTruncateExpr - This class represents a truncation of an integer value
113 /// to a smaller integer value.
115 class SCEVTruncateExpr : public SCEVCastExpr {
116 friend class ScalarEvolution;
118 SCEVTruncateExpr(const SCEV *op, const Type *ty);
121 const SCEV *replaceSymbolicValuesWithConcrete(const SCEV *Sym,
123 ScalarEvolution &SE) const {
124 const SCEV *H = Op->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
127 return SE.getTruncateExpr(H, Ty);
130 virtual void print(raw_ostream &OS) const;
132 /// Methods for support type inquiry through isa, cast, and dyn_cast:
133 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
134 static inline bool classof(const SCEV *S) {
135 return S->getSCEVType() == scTruncate;
139 //===--------------------------------------------------------------------===//
140 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
141 /// integer value to a larger integer value.
143 class SCEVZeroExtendExpr : public SCEVCastExpr {
144 friend class ScalarEvolution;
146 SCEVZeroExtendExpr(const SCEV *op, const Type *ty);
149 const SCEV *replaceSymbolicValuesWithConcrete(const SCEV *Sym,
151 ScalarEvolution &SE) const {
152 const SCEV *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 SCEV *op, const Type *ty);
177 const SCEV *replaceSymbolicValuesWithConcrete(const SCEV *Sym,
179 ScalarEvolution &SE) const {
180 const SCEV *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<const SCEV *, 8> Operands;
204 SCEVNAryExpr(enum SCEVTypes T, const SmallVectorImpl<const SCEV *> &ops)
205 : SCEV(T), Operands(ops.begin(), ops.end()) {}
208 virtual void Profile(FoldingSetNodeID &ID) const;
210 unsigned getNumOperands() const { return (unsigned)Operands.size(); }
211 const SCEV *getOperand(unsigned i) const {
212 assert(i < Operands.size() && "Operand index out of range!");
216 const SmallVectorImpl<const SCEV *> &getOperands() const {
219 typedef SmallVectorImpl<const SCEV *>::const_iterator op_iterator;
220 op_iterator op_begin() const { return Operands.begin(); }
221 op_iterator op_end() const { return Operands.end(); }
223 virtual bool isLoopInvariant(const Loop *L) const {
224 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
225 if (!getOperand(i)->isLoopInvariant(L)) return false;
229 // hasComputableLoopEvolution - N-ary expressions have computable loop
230 // evolutions iff they have at least one operand that varies with the loop,
231 // but that all varying operands are computable.
232 virtual bool hasComputableLoopEvolution(const Loop *L) const {
233 bool HasVarying = false;
234 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
235 if (!getOperand(i)->isLoopInvariant(L)) {
236 if (getOperand(i)->hasComputableLoopEvolution(L))
244 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
246 virtual const Type *getType() const { return getOperand(0)->getType(); }
248 /// Methods for support type inquiry through isa, cast, and dyn_cast:
249 static inline bool classof(const SCEVNAryExpr *S) { return true; }
250 static inline bool classof(const SCEV *S) {
251 return S->getSCEVType() == scAddExpr ||
252 S->getSCEVType() == scMulExpr ||
253 S->getSCEVType() == scSMaxExpr ||
254 S->getSCEVType() == scUMaxExpr ||
255 S->getSCEVType() == scAddRecExpr;
259 //===--------------------------------------------------------------------===//
260 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
263 class SCEVCommutativeExpr : public SCEVNAryExpr {
265 SCEVCommutativeExpr(enum SCEVTypes T,
266 const SmallVectorImpl<const SCEV *> &ops)
267 : SCEVNAryExpr(T, ops) {}
270 const SCEV *replaceSymbolicValuesWithConcrete(const SCEV *Sym,
272 ScalarEvolution &SE) const;
274 virtual const char *getOperationStr() const = 0;
276 virtual void print(raw_ostream &OS) const;
278 /// Methods for support type inquiry through isa, cast, and dyn_cast:
279 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
280 static inline bool classof(const SCEV *S) {
281 return S->getSCEVType() == scAddExpr ||
282 S->getSCEVType() == scMulExpr ||
283 S->getSCEVType() == scSMaxExpr ||
284 S->getSCEVType() == scUMaxExpr;
289 //===--------------------------------------------------------------------===//
290 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
292 class SCEVAddExpr : public SCEVCommutativeExpr {
293 friend class ScalarEvolution;
295 explicit SCEVAddExpr(const SmallVectorImpl<const SCEV *> &ops)
296 : SCEVCommutativeExpr(scAddExpr, ops) {
300 virtual const char *getOperationStr() const { return " + "; }
302 /// Methods for support type inquiry through isa, cast, and dyn_cast:
303 static inline bool classof(const SCEVAddExpr *S) { return true; }
304 static inline bool classof(const SCEV *S) {
305 return S->getSCEVType() == scAddExpr;
309 //===--------------------------------------------------------------------===//
310 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
312 class SCEVMulExpr : public SCEVCommutativeExpr {
313 friend class ScalarEvolution;
315 explicit SCEVMulExpr(const SmallVectorImpl<const SCEV *> &ops)
316 : SCEVCommutativeExpr(scMulExpr, ops) {
320 virtual const char *getOperationStr() const { return " * "; }
322 /// Methods for support type inquiry through isa, cast, and dyn_cast:
323 static inline bool classof(const SCEVMulExpr *S) { return true; }
324 static inline bool classof(const SCEV *S) {
325 return S->getSCEVType() == scMulExpr;
330 //===--------------------------------------------------------------------===//
331 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
333 class SCEVUDivExpr : public SCEV {
334 friend class ScalarEvolution;
338 SCEVUDivExpr(const SCEV *lhs, const SCEV *rhs)
339 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
342 virtual void Profile(FoldingSetNodeID &ID) const;
344 const SCEV *getLHS() const { return LHS; }
345 const SCEV *getRHS() const { return RHS; }
347 virtual bool isLoopInvariant(const Loop *L) const {
348 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
351 virtual bool hasComputableLoopEvolution(const Loop *L) const {
352 return LHS->hasComputableLoopEvolution(L) &&
353 RHS->hasComputableLoopEvolution(L);
356 const SCEV *replaceSymbolicValuesWithConcrete(const SCEV *Sym,
358 ScalarEvolution &SE) const {
359 const SCEV *L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
360 const SCEV *R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
361 if (L == LHS && R == RHS)
364 return SE.getUDivExpr(L, R);
367 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
369 virtual const Type *getType() const;
371 void print(raw_ostream &OS) const;
373 /// Methods for support type inquiry through isa, cast, and dyn_cast:
374 static inline bool classof(const SCEVUDivExpr *S) { return true; }
375 static inline bool classof(const SCEV *S) {
376 return S->getSCEVType() == scUDivExpr;
381 //===--------------------------------------------------------------------===//
382 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
383 /// count of the specified loop. This is the primary focus of the
384 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
385 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
386 /// created and analyzed.
388 /// All operands of an AddRec are required to be loop invariant.
390 class SCEVAddRecExpr : public SCEVNAryExpr {
391 friend class ScalarEvolution;
395 SCEVAddRecExpr(const SmallVectorImpl<const SCEV *> &ops, const Loop *l)
396 : SCEVNAryExpr(scAddRecExpr, ops), L(l) {
397 for (size_t i = 0, e = Operands.size(); i != e; ++i)
398 assert(Operands[i]->isLoopInvariant(l) &&
399 "Operands of AddRec must be loop-invariant!");
403 virtual void Profile(FoldingSetNodeID &ID) const;
405 const SCEV *getStart() const { return Operands[0]; }
406 const Loop *getLoop() const { return L; }
408 /// getStepRecurrence - This method constructs and returns the recurrence
409 /// indicating how much this expression steps by. If this is a polynomial
410 /// of degree N, it returns a chrec of degree N-1.
411 const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
412 if (isAffine()) return getOperand(1);
413 return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
418 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
419 if (L == QL) return true;
423 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
425 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
426 /// an expressions A+B*x where A and B are loop invariant values.
427 bool isAffine() const {
428 // We know that the start value is invariant. This expression is thus
429 // affine iff the step is also invariant.
430 return getNumOperands() == 2;
433 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
434 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
435 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
436 bool isQuadratic() const {
437 return getNumOperands() == 3;
440 /// evaluateAtIteration - Return the value of this chain of recurrences at
441 /// the specified iteration number.
442 const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
444 /// getNumIterationsInRange - Return the number of iterations of this loop
445 /// that produce values in the specified constant range. Another way of
446 /// looking at this is that it returns the first iteration number where the
447 /// value is not in the condition, thus computing the exit count. If the
448 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
450 const SCEV *getNumIterationsInRange(ConstantRange Range,
451 ScalarEvolution &SE) const;
453 const SCEV *replaceSymbolicValuesWithConcrete(const SCEV *Sym,
455 ScalarEvolution &SE) const;
457 /// getPostIncExpr - Return an expression representing the value of
458 /// this expression one iteration of the loop ahead.
459 const SCEV *getPostIncExpr(ScalarEvolution &SE) const {
460 return SE.getAddExpr(this, getStepRecurrence(SE));
463 virtual void print(raw_ostream &OS) const;
465 /// Methods for support type inquiry through isa, cast, and dyn_cast:
466 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
467 static inline bool classof(const SCEV *S) {
468 return S->getSCEVType() == scAddRecExpr;
473 //===--------------------------------------------------------------------===//
474 /// SCEVSMaxExpr - This class represents a signed maximum selection.
476 class SCEVSMaxExpr : public SCEVCommutativeExpr {
477 friend class ScalarEvolution;
479 explicit SCEVSMaxExpr(const SmallVectorImpl<const SCEV *> &ops)
480 : SCEVCommutativeExpr(scSMaxExpr, ops) {
484 virtual const char *getOperationStr() const { return " smax "; }
486 /// Methods for support type inquiry through isa, cast, and dyn_cast:
487 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
488 static inline bool classof(const SCEV *S) {
489 return S->getSCEVType() == scSMaxExpr;
494 //===--------------------------------------------------------------------===//
495 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
497 class SCEVUMaxExpr : public SCEVCommutativeExpr {
498 friend class ScalarEvolution;
500 explicit SCEVUMaxExpr(const SmallVectorImpl<const SCEV *> &ops)
501 : SCEVCommutativeExpr(scUMaxExpr, ops) {
505 virtual const char *getOperationStr() const { return " umax "; }
507 /// Methods for support type inquiry through isa, cast, and dyn_cast:
508 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
509 static inline bool classof(const SCEV *S) {
510 return S->getSCEVType() == scUMaxExpr;
515 //===--------------------------------------------------------------------===//
516 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
517 /// value, and only represent it as it's LLVM Value. This is the "bottom"
518 /// value for the analysis.
520 class SCEVUnknown : public SCEV {
521 friend class ScalarEvolution;
524 explicit SCEVUnknown(Value *v) :
525 SCEV(scUnknown), V(v) {}
528 virtual void Profile(FoldingSetNodeID &ID) const;
530 Value *getValue() const { return V; }
532 virtual bool isLoopInvariant(const Loop *L) const;
533 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
534 return false; // not computable
537 const SCEV *replaceSymbolicValuesWithConcrete(const SCEV *Sym,
539 ScalarEvolution &SE) const {
540 if (&*Sym == this) return Conc;
544 bool dominates(BasicBlock *BB, DominatorTree *DT) const;
546 virtual const Type *getType() const;
548 virtual void print(raw_ostream &OS) const;
550 /// Methods for support type inquiry through isa, cast, and dyn_cast:
551 static inline bool classof(const SCEVUnknown *S) { return true; }
552 static inline bool classof(const SCEV *S) {
553 return S->getSCEVType() == scUnknown;
557 /// SCEVVisitor - This class defines a simple visitor class that may be used
558 /// for various SCEV analysis purposes.
559 template<typename SC, typename RetVal=void>
561 RetVal visit(const SCEV *S) {
562 switch (S->getSCEVType()) {
564 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
566 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
568 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
570 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
572 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
574 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
576 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
578 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
580 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
582 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
584 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
585 case scCouldNotCompute:
586 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
588 LLVM_UNREACHABLE("Unknown SCEV type!");
592 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
593 LLVM_UNREACHABLE("Invalid use of SCEVCouldNotCompute!");