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,
30 scUnknown, scCouldNotCompute
33 //===--------------------------------------------------------------------===//
34 /// SCEVConstant - This class represents a constant integer value.
36 class SCEVConstant : public SCEV {
37 friend class ScalarEvolution;
40 SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
41 SCEV(ID, scConstant), V(v) {}
43 ConstantInt *getValue() const { return V; }
45 Type *getType() const { return V->getType(); }
47 /// Methods for support type inquiry through isa, cast, and dyn_cast:
48 static inline bool classof(const SCEVConstant *S) { return true; }
49 static inline bool classof(const SCEV *S) {
50 return S->getSCEVType() == scConstant;
54 //===--------------------------------------------------------------------===//
55 /// SCEVCastExpr - This is the base class for unary cast operator classes.
57 class SCEVCastExpr : public SCEV {
62 SCEVCastExpr(const FoldingSetNodeIDRef ID,
63 unsigned SCEVTy, const SCEV *op, Type *ty);
66 const SCEV *getOperand() const { return Op; }
67 Type *getType() const { return Ty; }
69 /// Methods for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const SCEVCastExpr *S) { return true; }
71 static inline bool classof(const SCEV *S) {
72 return S->getSCEVType() == scTruncate ||
73 S->getSCEVType() == scZeroExtend ||
74 S->getSCEVType() == scSignExtend;
78 //===--------------------------------------------------------------------===//
79 /// SCEVTruncateExpr - This class represents a truncation of an integer value
80 /// to a smaller integer value.
82 class SCEVTruncateExpr : public SCEVCastExpr {
83 friend class ScalarEvolution;
85 SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
86 const SCEV *op, Type *ty);
89 /// Methods for support type inquiry through isa, cast, and dyn_cast:
90 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
91 static inline bool classof(const SCEV *S) {
92 return S->getSCEVType() == scTruncate;
96 //===--------------------------------------------------------------------===//
97 /// SCEVZeroExtendExpr - This class represents a zero extension of a small
98 /// integer value to a larger integer value.
100 class SCEVZeroExtendExpr : public SCEVCastExpr {
101 friend class ScalarEvolution;
103 SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
104 const SCEV *op, Type *ty);
107 /// Methods for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
109 static inline bool classof(const SCEV *S) {
110 return S->getSCEVType() == scZeroExtend;
114 //===--------------------------------------------------------------------===//
115 /// SCEVSignExtendExpr - This class represents a sign extension of a small
116 /// integer value to a larger integer value.
118 class SCEVSignExtendExpr : public SCEVCastExpr {
119 friend class ScalarEvolution;
121 SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
122 const SCEV *op, Type *ty);
125 /// Methods for support type inquiry through isa, cast, and dyn_cast:
126 static inline bool classof(const SCEVSignExtendExpr *S) { return true; }
127 static inline bool classof(const SCEV *S) {
128 return S->getSCEVType() == scSignExtend;
133 //===--------------------------------------------------------------------===//
134 /// SCEVNAryExpr - This node is a base class providing common
135 /// functionality for n'ary operators.
137 class SCEVNAryExpr : public SCEV {
139 // Since SCEVs are immutable, ScalarEvolution allocates operand
140 // arrays with its SCEVAllocator, so this class just needs a simple
141 // pointer rather than a more elaborate vector-like data structure.
142 // This also avoids the need for a non-trivial destructor.
143 const SCEV *const *Operands;
146 SCEVNAryExpr(const FoldingSetNodeIDRef ID,
147 enum SCEVTypes T, const SCEV *const *O, size_t N)
148 : SCEV(ID, T), Operands(O), NumOperands(N) {}
151 size_t getNumOperands() const { return NumOperands; }
152 const SCEV *getOperand(unsigned i) const {
153 assert(i < NumOperands && "Operand index out of range!");
157 typedef const SCEV *const *op_iterator;
158 op_iterator op_begin() const { return Operands; }
159 op_iterator op_end() const { return Operands + NumOperands; }
161 Type *getType() const { return getOperand(0)->getType(); }
163 NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const {
164 return (NoWrapFlags)(SubclassData & Mask);
167 /// Methods for support type inquiry through isa, cast, and dyn_cast:
168 static inline bool classof(const SCEVNAryExpr *S) { return true; }
169 static inline bool classof(const SCEV *S) {
170 return S->getSCEVType() == scAddExpr ||
171 S->getSCEVType() == scMulExpr ||
172 S->getSCEVType() == scSMaxExpr ||
173 S->getSCEVType() == scUMaxExpr ||
174 S->getSCEVType() == scAddRecExpr;
178 //===--------------------------------------------------------------------===//
179 /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
182 class SCEVCommutativeExpr : public SCEVNAryExpr {
184 SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
185 enum SCEVTypes T, const SCEV *const *O, size_t N)
186 : SCEVNAryExpr(ID, T, O, N) {}
189 /// Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
191 static inline bool classof(const SCEV *S) {
192 return S->getSCEVType() == scAddExpr ||
193 S->getSCEVType() == scMulExpr ||
194 S->getSCEVType() == scSMaxExpr ||
195 S->getSCEVType() == scUMaxExpr;
198 /// Set flags for a non-recurrence without clearing previously set flags.
199 void setNoWrapFlags(NoWrapFlags Flags) {
200 SubclassData |= Flags;
205 //===--------------------------------------------------------------------===//
206 /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
208 class SCEVAddExpr : public SCEVCommutativeExpr {
209 friend class ScalarEvolution;
211 SCEVAddExpr(const FoldingSetNodeIDRef ID,
212 const SCEV *const *O, size_t N)
213 : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
217 Type *getType() const {
218 // Use the type of the last operand, which is likely to be a pointer
219 // type, if there is one. This doesn't usually matter, but it can help
220 // reduce casts when the expressions are expanded.
221 return getOperand(getNumOperands() - 1)->getType();
224 /// Methods for support type inquiry through isa, cast, and dyn_cast:
225 static inline bool classof(const SCEVAddExpr *S) { return true; }
226 static inline bool classof(const SCEV *S) {
227 return S->getSCEVType() == scAddExpr;
231 //===--------------------------------------------------------------------===//
232 /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
234 class SCEVMulExpr : public SCEVCommutativeExpr {
235 friend class ScalarEvolution;
237 SCEVMulExpr(const FoldingSetNodeIDRef ID,
238 const SCEV *const *O, size_t N)
239 : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
243 /// Methods for support type inquiry through isa, cast, and dyn_cast:
244 static inline bool classof(const SCEVMulExpr *S) { return true; }
245 static inline bool classof(const SCEV *S) {
246 return S->getSCEVType() == scMulExpr;
251 //===--------------------------------------------------------------------===//
252 /// SCEVUDivExpr - This class represents a binary unsigned division operation.
254 class SCEVUDivExpr : public SCEV {
255 friend class ScalarEvolution;
259 SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
260 : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
263 const SCEV *getLHS() const { return LHS; }
264 const SCEV *getRHS() const { return RHS; }
266 Type *getType() const {
267 // In most cases the types of LHS and RHS will be the same, but in some
268 // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
269 // depend on the type for correctness, but handling types carefully can
270 // avoid extra casts in the SCEVExpander. The LHS is more likely to be
271 // a pointer type than the RHS, so use the RHS' type here.
272 return getRHS()->getType();
275 /// Methods for support type inquiry through isa, cast, and dyn_cast:
276 static inline bool classof(const SCEVUDivExpr *S) { return true; }
277 static inline bool classof(const SCEV *S) {
278 return S->getSCEVType() == scUDivExpr;
283 //===--------------------------------------------------------------------===//
284 /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
285 /// count of the specified loop. This is the primary focus of the
286 /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
287 /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
288 /// created and analyzed.
290 /// All operands of an AddRec are required to be loop invariant.
292 class SCEVAddRecExpr : public SCEVNAryExpr {
293 friend class ScalarEvolution;
297 SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
298 const SCEV *const *O, size_t N, const Loop *l)
299 : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {}
302 const SCEV *getStart() const { return Operands[0]; }
303 const Loop *getLoop() const { return L; }
305 /// getStepRecurrence - This method constructs and returns the recurrence
306 /// indicating how much this expression steps by. If this is a polynomial
307 /// of degree N, it returns a chrec of degree N-1.
308 /// We cannot determine whether the step recurrence has self-wraparound.
309 const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
310 if (isAffine()) return getOperand(1);
311 return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
313 getLoop(), FlagAnyWrap);
316 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
317 /// an expressions A+B*x where A and B are loop invariant values.
318 bool isAffine() const {
319 // We know that the start value is invariant. This expression is thus
320 // affine iff the step is also invariant.
321 return getNumOperands() == 2;
324 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
325 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
326 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
327 bool isQuadratic() const {
328 return getNumOperands() == 3;
331 /// Set flags for a recurrence without clearing any previously set flags.
332 /// For AddRec, either NUW or NSW implies NW. Keep track of this fact here
333 /// to make it easier to propagate flags.
334 void setNoWrapFlags(NoWrapFlags Flags) {
335 if (Flags & (FlagNUW | FlagNSW))
336 Flags = ScalarEvolution::setFlags(Flags, FlagNW);
337 SubclassData |= Flags;
340 /// evaluateAtIteration - Return the value of this chain of recurrences at
341 /// the specified iteration number.
342 const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
344 /// getNumIterationsInRange - Return the number of iterations of this loop
345 /// that produce values in the specified constant range. Another way of
346 /// looking at this is that it returns the first iteration number where the
347 /// value is not in the condition, thus computing the exit count. If the
348 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
350 const SCEV *getNumIterationsInRange(ConstantRange Range,
351 ScalarEvolution &SE) const;
353 /// getPostIncExpr - Return an expression representing the value of
354 /// this expression one iteration of the loop ahead.
355 const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
356 return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
359 /// Methods for support type inquiry through isa, cast, and dyn_cast:
360 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
361 static inline bool classof(const SCEV *S) {
362 return S->getSCEVType() == scAddRecExpr;
367 //===--------------------------------------------------------------------===//
368 /// SCEVSMaxExpr - This class represents a signed maximum selection.
370 class SCEVSMaxExpr : public SCEVCommutativeExpr {
371 friend class ScalarEvolution;
373 SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
374 const SCEV *const *O, size_t N)
375 : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
376 // Max never overflows.
377 setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
381 /// Methods for support type inquiry through isa, cast, and dyn_cast:
382 static inline bool classof(const SCEVSMaxExpr *S) { return true; }
383 static inline bool classof(const SCEV *S) {
384 return S->getSCEVType() == scSMaxExpr;
389 //===--------------------------------------------------------------------===//
390 /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
392 class SCEVUMaxExpr : public SCEVCommutativeExpr {
393 friend class ScalarEvolution;
395 SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
396 const SCEV *const *O, size_t N)
397 : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
398 // Max never overflows.
399 setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
403 /// Methods for support type inquiry through isa, cast, and dyn_cast:
404 static inline bool classof(const SCEVUMaxExpr *S) { return true; }
405 static inline bool classof(const SCEV *S) {
406 return S->getSCEVType() == scUMaxExpr;
410 //===--------------------------------------------------------------------===//
411 /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
412 /// value, and only represent it as its LLVM Value. This is the "bottom"
413 /// value for the analysis.
415 class SCEVUnknown : public SCEV, private CallbackVH {
416 friend class ScalarEvolution;
418 // Implement CallbackVH.
419 virtual void deleted();
420 virtual void allUsesReplacedWith(Value *New);
422 /// SE - The parent ScalarEvolution value. This is used to update
423 /// the parent's maps when the value associated with a SCEVUnknown
424 /// is deleted or RAUW'd.
427 /// Next - The next pointer in the linked list of all
428 /// SCEVUnknown instances owned by a ScalarEvolution.
431 SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
432 ScalarEvolution *se, SCEVUnknown *next) :
433 SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
436 Value *getValue() const { return getValPtr(); }
438 /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
439 /// constant representing a type size, alignment, or field offset in
440 /// a target-independent manner, and hasn't happened to have been
441 /// folded with other operations into something unrecognizable. This
442 /// is mainly only useful for pretty-printing and other situations
443 /// where it isn't absolutely required for these to succeed.
444 bool isSizeOf(Type *&AllocTy) const;
445 bool isAlignOf(Type *&AllocTy) const;
446 bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
448 Type *getType() const { return getValPtr()->getType(); }
450 /// Methods for support type inquiry through isa, cast, and dyn_cast:
451 static inline bool classof(const SCEVUnknown *S) { return true; }
452 static inline bool classof(const SCEV *S) {
453 return S->getSCEVType() == scUnknown;
457 /// SCEVVisitor - This class defines a simple visitor class that may be used
458 /// for various SCEV analysis purposes.
459 template<typename SC, typename RetVal=void>
461 RetVal visit(const SCEV *S) {
462 switch (S->getSCEVType()) {
464 return ((SC*)this)->visitConstant((const SCEVConstant*)S);
466 return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
468 return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
470 return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
472 return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
474 return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
476 return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
478 return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
480 return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
482 return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
484 return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
485 case scCouldNotCompute:
486 return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
488 llvm_unreachable("Unknown SCEV type!");
492 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
493 llvm_unreachable("Invalid use of SCEVCouldNotCompute!");