1 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
24 // These should be ordered in terms of increasing complexity to make the
26 scConstant, scTruncate, scZeroExtend, scAddExpr, scMulExpr, scUDivExpr,
27 scAddRecExpr, scUnknown, scCouldNotCompute
30 //===--------------------------------------------------------------------===//
31 /// SCEVConstant - This class represents a constant integer value.
33 class SCEVConstant : public SCEV {
35 SCEVConstant(ConstantInt *v) : SCEV(scConstant), V(v) {}
37 virtual ~SCEVConstant();
39 /// get method - This just gets and returns a new SCEVConstant object.
41 static SCEVHandle get(ConstantInt *V);
43 ConstantInt *getValue() const { return V; }
45 /// getValueRange - Return the tightest constant bounds that this value is
46 /// known to have. This method is only valid on integer SCEV objects.
47 virtual ConstantRange getValueRange() const;
49 virtual bool isLoopInvariant(const Loop *L) const {
53 virtual bool hasComputableLoopEvolution(const Loop *L) const {
54 return false; // Not loop variant
57 virtual const Type *getType() const;
59 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
60 Instruction *InsertPt) {
61 return (Value*)getValue();
64 virtual void print(std::ostream &OS) const;
66 /// Methods for support type inquiry through isa, cast, and dyn_cast:
67 static inline bool classof(const SCEVConstant *S) { return true; }
68 static inline bool classof(const SCEV *S) {
69 return S->getSCEVType() == scConstant;
73 //===--------------------------------------------------------------------===//
74 /// SCEVTruncateExpr - This class represents a truncation of an integer value
75 /// to a smaller integer value.
77 class SCEVTruncateExpr : public SCEV {
80 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty);
81 virtual ~SCEVTruncateExpr();
83 /// get method - This just gets and returns a new SCEVTruncate object
85 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
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 /// getValueRange - Return the tightest constant bounds that this value is
99 /// known to have. This method is only valid on integer SCEV objects.
100 virtual ConstantRange getValueRange() const;
102 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
103 Instruction *InsertPt);
105 virtual void print(std::ostream &OS) const;
107 /// Methods for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
109 static inline bool classof(const SCEV *S) {
110 return S->getSCEVType() == scTruncate;
114 //===--------------------------------------------------------------------===//
115 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
116 /// integer value to a larger integer value.
118 class SCEVZeroExtendExpr : public SCEV {
121 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty);
122 virtual ~SCEVZeroExtendExpr();
124 /// get method - This just gets and returns a new SCEVZeroExtend object
126 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
128 const SCEVHandle &getOperand() const { return Op; }
129 virtual const Type *getType() const { return Ty; }
131 virtual bool isLoopInvariant(const Loop *L) const {
132 return Op->isLoopInvariant(L);
135 virtual bool hasComputableLoopEvolution(const Loop *L) const {
136 return Op->hasComputableLoopEvolution(L);
139 /// getValueRange - Return the tightest constant bounds that this value is
140 /// known to have. This method is only valid on integer SCEV objects.
141 virtual ConstantRange getValueRange() const;
143 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
144 Instruction *InsertPt);
146 virtual void print(std::ostream &OS) const;
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;
156 //===--------------------------------------------------------------------===//
157 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
160 class SCEVCommutativeExpr : public SCEV {
161 std::vector<SCEVHandle> Operands;
164 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
166 Operands.reserve(ops.size());
167 Operands.insert(Operands.end(), ops.begin(), ops.end());
169 ~SCEVCommutativeExpr();
172 unsigned getNumOperands() const { return Operands.size(); }
173 const SCEVHandle &getOperand(unsigned i) const {
174 assert(i < Operands.size() && "Operand index out of range!");
178 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
179 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
180 op_iterator op_begin() const { return Operands.begin(); }
181 op_iterator op_end() const { return Operands.end(); }
184 virtual bool isLoopInvariant(const Loop *L) const {
185 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
186 if (!getOperand(i)->isLoopInvariant(L)) return false;
190 virtual bool hasComputableLoopEvolution(const Loop *L) const {
191 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
192 if (getOperand(i)->hasComputableLoopEvolution(L)) return true;
196 virtual const char *getOperationStr() const = 0;
198 virtual const Type *getType() const { return getOperand(0)->getType(); }
199 virtual void print(std::ostream &OS) const;
201 /// Methods for support type inquiry through isa, cast, and dyn_cast:
202 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
203 static inline bool classof(const SCEV *S) {
204 return S->getSCEVType() == scAddExpr ||
205 S->getSCEVType() == scMulExpr;
210 //===--------------------------------------------------------------------===//
211 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
213 class SCEVAddExpr : public SCEVCommutativeExpr {
214 SCEVAddExpr(const std::vector<SCEVHandle> &ops)
215 : SCEVCommutativeExpr(scAddExpr, ops) {
219 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
221 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
222 std::vector<SCEVHandle> Ops;
228 static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
229 const SCEVHandle &Op2) {
230 std::vector<SCEVHandle> Ops;
237 virtual const char *getOperationStr() const { return " + "; }
239 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
240 Instruction *InsertPt);
242 /// Methods for support type inquiry through isa, cast, and dyn_cast:
243 static inline bool classof(const SCEVAddExpr *S) { return true; }
244 static inline bool classof(const SCEV *S) {
245 return S->getSCEVType() == scAddExpr;
249 //===--------------------------------------------------------------------===//
250 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
252 class SCEVMulExpr : public SCEVCommutativeExpr {
253 SCEVMulExpr(const std::vector<SCEVHandle> &ops)
254 : SCEVCommutativeExpr(scMulExpr, ops) {
258 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
260 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
261 std::vector<SCEVHandle> Ops;
267 virtual const char *getOperationStr() const { return " * "; }
269 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
270 Instruction *InsertPt);
272 /// Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const SCEVMulExpr *S) { return true; }
274 static inline bool classof(const SCEV *S) {
275 return S->getSCEVType() == scMulExpr;
280 //===--------------------------------------------------------------------===//
281 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
283 class SCEVUDivExpr : public SCEV {
285 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
286 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
288 virtual ~SCEVUDivExpr();
290 /// get method - This just gets and returns a new SCEVUDiv object.
292 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
294 const SCEVHandle &getLHS() const { return LHS; }
295 const SCEVHandle &getRHS() const { return RHS; }
297 virtual bool isLoopInvariant(const Loop *L) const {
298 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
301 virtual bool hasComputableLoopEvolution(const Loop *L) const {
302 return LHS->hasComputableLoopEvolution(L) &&
303 RHS->hasComputableLoopEvolution(L);
306 virtual const Type *getType() const;
308 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
309 Instruction *InsertPt);
311 void print(std::ostream &OS) const;
313 /// Methods for support type inquiry through isa, cast, and dyn_cast:
314 static inline bool classof(const SCEVUDivExpr *S) { return true; }
315 static inline bool classof(const SCEV *S) {
316 return S->getSCEVType() == scUDivExpr;
321 //===--------------------------------------------------------------------===//
322 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
323 /// count of the specified loop.
325 /// All operands of an AddRec are required to be loop invariant.
327 class SCEVAddRecExpr : public SCEV {
328 std::vector<SCEVHandle> Operands;
331 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
332 : SCEV(scAddRecExpr), Operands(ops), L(l) {
333 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
334 assert(Operands[i]->isLoopInvariant(l) &&
335 "Operands of AddRec must be loop-invariant!");
339 static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
341 static SCEVHandle get(std::vector<SCEVHandle> &Operands,
343 static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
345 std::vector<SCEVHandle> NewOp(Operands);
346 return get(NewOp, L);
349 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
350 op_iterator op_begin() const { return Operands.begin(); }
351 op_iterator op_end() const { return Operands.end(); }
353 unsigned getNumOperands() const { return Operands.size(); }
354 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
355 const SCEVHandle &getStart() const { return Operands[0]; }
356 const Loop *getLoop() const { return L; }
359 /// getStepRecurrence - This method constructs and returns the recurrence
360 /// indicating how much this expression steps by. If this is a polynomial
361 /// of degree N, it returns a chrec of degree N-1.
362 SCEVHandle getStepRecurrence() const {
363 if (getNumOperands() == 2) return getOperand(1);
364 return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
368 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
369 if (L == QL) return true;
370 /// FIXME: What if the start or step value a recurrence for the specified
375 virtual bool isLoopInvariant(const Loop *QueryLoop) const;
377 virtual const Type *getType() const { return Operands[0]->getType(); }
379 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
380 Instruction *InsertPt);
383 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
384 /// an expressions A+B*x where A and B are loop invariant values.
385 bool isAffine() const {
386 // We know that the start value is invariant. This expression is thus
387 // affine iff the step is also invariant.
388 return getNumOperands() == 2;
391 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
392 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
393 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
394 bool isQuadratic() const {
395 return getNumOperands() == 3;
398 /// evaluateAtIteration - Return the value of this chain of recurrences at
399 /// the specified iteration number.
400 SCEVHandle evaluateAtIteration(SCEVHandle It) const;
402 /// getNumIterationsInRange - Return the number of iterations of this loop
403 /// that produce values in the specified constant range. Another way of
404 /// looking at this is that it returns the first iteration number where the
405 /// value is not in the condition, thus computing the exit count. If the
406 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
408 SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
411 virtual void print(std::ostream &OS) const;
413 /// Methods for support type inquiry through isa, cast, and dyn_cast:
414 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
415 static inline bool classof(const SCEV *S) {
416 return S->getSCEVType() == scAddRecExpr;
420 //===--------------------------------------------------------------------===//
421 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
422 /// value, and only represent it as it's LLVM Value. This is the "bottom"
423 /// value for the analysis.
425 class SCEVUnknown : public SCEV {
427 SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
432 /// get method - For SCEVUnknown, this just gets and returns a new
434 static SCEVHandle get(Value *V);
436 Value *getValue() const { return V; }
438 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
439 Instruction *InsertPt) {
443 virtual bool isLoopInvariant(const Loop *L) const;
444 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
445 return false; // not computable
448 virtual const Type *getType() const;
450 virtual void print(std::ostream &OS) const;
452 /// Methods for support type inquiry through isa, cast, and dyn_cast:
453 static inline bool classof(const SCEVUnknown *S) { return true; }
454 static inline bool classof(const SCEV *S) {
455 return S->getSCEVType() == scUnknown;