1 //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- 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 // The ScalarEvolution class is an LLVM pass which can be used to analyze and
11 // catagorize scalar expressions in loops. It specializes in recognizing
12 // general induction variables, representing them with the abstract and opaque
13 // SCEV class. Given this analysis, trip counts of loops and other important
14 // properties can be obtained.
16 // This analysis is primarily useful for induction variable substitution and
17 // strength reduction.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
22 #define LLVM_ANALYSIS_SCALAREVOLUTION_H
24 #include "llvm/Pass.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Support/DataTypes.h"
36 class ScalarEvolution;
38 /// SCEV - This class represent an analyzed expression in the program. These
39 /// are reference counted opaque objects that the client is not allowed to
40 /// do much with directly.
43 const unsigned SCEVType; // The SCEV baseclass this node corresponds to
44 mutable unsigned RefCount;
46 friend class SCEVHandle;
47 void addRef() const { ++RefCount; }
48 void dropRef() const {
53 SCEV(const SCEV &); // DO NOT IMPLEMENT
54 void operator=(const SCEV &); // DO NOT IMPLEMENT
58 explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
60 unsigned getSCEVType() const { return SCEVType; }
62 /// getValueRange - Return the tightest constant bounds that this value is
63 /// known to have. This method is only valid on integer SCEV objects.
64 virtual ConstantRange getValueRange() const;
66 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
67 /// the specified loop.
68 virtual bool isLoopInvariant(const Loop *L) const = 0;
70 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
71 /// known way in the specified loop. This property being true implies that
72 /// the value is variant in the loop AND that we can emit an expression to
73 /// compute the value of the expression at any particular loop iteration.
74 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
76 /// getType - Return the LLVM type of this SCEV expression.
78 virtual const Type *getType() const = 0;
80 /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
82 uint32_t getBitWidth() const;
84 /// isZero - Return true if the expression is a constant zero.
88 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
89 /// the symbolic value "Sym", construct and return a new SCEV that produces
90 /// the same value, but which uses the concrete value Conc instead of the
91 /// symbolic value. If this SCEV does not use the symbolic value, it
94 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
95 const SCEVHandle &Conc,
96 ScalarEvolution &SE) const = 0;
98 /// print - Print out the internal representation of this scalar to the
99 /// specified stream. This should really only be used for debugging
101 virtual void print(std::ostream &OS) const = 0;
102 void print(std::ostream *OS) const { if (OS) print(*OS); }
104 /// dump - This method is used for debugging.
109 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
114 /// SCEVCouldNotCompute - An object of this class is returned by queries that
115 /// could not be answered. For example, if you ask for the number of
116 /// iterations of a linked-list traversal loop, you will get one of these.
117 /// None of the standard SCEV operations are valid on this class, it is just a
119 struct SCEVCouldNotCompute : public SCEV {
120 SCEVCouldNotCompute();
122 // None of these methods are valid for this object.
123 virtual bool isLoopInvariant(const Loop *L) const;
124 virtual const Type *getType() const;
125 virtual bool hasComputableLoopEvolution(const Loop *L) const;
126 virtual void print(std::ostream &OS) const;
127 void print(std::ostream *OS) const { if (OS) print(*OS); }
129 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
130 const SCEVHandle &Conc,
131 ScalarEvolution &SE) const;
133 /// Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
135 static bool classof(const SCEV *S);
138 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
139 /// freeing the objects when the last reference is dropped.
142 SCEVHandle(); // DO NOT IMPLEMENT
144 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
145 assert(S && "Cannot create a handle to a null SCEV!");
148 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
151 ~SCEVHandle() { S->dropRef(); }
153 operator SCEV*() const { return S; }
155 SCEV &operator*() const { return *S; }
156 SCEV *operator->() const { return S; }
158 bool operator==(SCEV *RHS) const { return S == RHS; }
159 bool operator!=(SCEV *RHS) const { return S != RHS; }
161 const SCEVHandle &operator=(SCEV *RHS) {
170 const SCEVHandle &operator=(const SCEVHandle &RHS) {
180 template<typename From> struct simplify_type;
181 template<> struct simplify_type<const SCEVHandle> {
182 typedef SCEV* SimpleType;
183 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
187 template<> struct simplify_type<SCEVHandle>
188 : public simplify_type<const SCEVHandle> {};
190 /// ScalarEvolution - This class is the main scalar evolution driver. Because
191 /// client code (intentionally) can't do much with the SCEV objects directly,
192 /// they must ask this class for services.
194 class ScalarEvolution : public FunctionPass {
195 void *Impl; // ScalarEvolution uses the pimpl pattern
197 static char ID; // Pass identification, replacement for typeid
198 ScalarEvolution() : FunctionPass((intptr_t)&ID), Impl(0) {}
200 /// getSCEV - Return a SCEV expression handle for the full generality of the
201 /// specified expression.
202 SCEVHandle getSCEV(Value *V) const;
204 SCEVHandle getConstant(ConstantInt *V);
205 SCEVHandle getConstant(const APInt& Val);
206 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
207 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
208 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
209 SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops);
210 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
211 std::vector<SCEVHandle> Ops;
214 return getAddExpr(Ops);
216 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
217 const SCEVHandle &Op2) {
218 std::vector<SCEVHandle> Ops;
222 return getAddExpr(Ops);
224 SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
225 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
226 std::vector<SCEVHandle> Ops;
229 return getMulExpr(Ops);
231 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
232 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
234 SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
236 SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
238 std::vector<SCEVHandle> NewOp(Operands);
239 return getAddRecExpr(NewOp, L);
241 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
242 SCEVHandle getSMaxExpr(std::vector<SCEVHandle> Operands);
243 SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
244 SCEVHandle getUMaxExpr(std::vector<SCEVHandle> Operands);
245 SCEVHandle getUnknown(Value *V);
247 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
249 SCEVHandle getNegativeSCEV(const SCEVHandle &V);
251 /// getNotSCEV - Return the SCEV object corresponding to ~V.
253 SCEVHandle getNotSCEV(const SCEVHandle &V);
255 /// getMinusSCEV - Return LHS-RHS.
257 SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
258 const SCEVHandle &RHS);
260 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
261 /// of the input value to the specified type. If the type must be
262 /// extended, it is zero extended.
263 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
265 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
266 /// specified signed integer value and return a SCEV for the constant.
267 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
269 /// hasSCEV - Return true if the SCEV for this value has already been
271 bool hasSCEV(Value *V) const;
273 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
274 /// the specified value.
275 void setSCEV(Value *V, const SCEVHandle &H);
277 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
278 /// at the specified scope in the program. The L value specifies a loop
279 /// nest to evaluate the expression at, where null is the top-level or a
280 /// specified loop is immediately inside of the loop.
282 /// This method can be used to compute the exit value for a variable defined
283 /// in a loop by querying what the value will hold in the parent loop.
285 /// If this value is not computable at this scope, a SCEVCouldNotCompute
286 /// object is returned.
287 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
289 /// getIterationCount - If the specified loop has a predictable iteration
290 /// count, return it, otherwise return a SCEVCouldNotCompute object.
291 SCEVHandle getIterationCount(const Loop *L) const;
293 /// hasLoopInvariantIterationCount - Return true if the specified loop has
294 /// an analyzable loop-invariant iteration count.
295 bool hasLoopInvariantIterationCount(const Loop *L) const;
297 /// deleteValueFromRecords - This method should be called by the
298 /// client before it removes a Value from the program, to make sure
299 /// that no dangling references are left around.
300 void deleteValueFromRecords(Value *V) const;
302 virtual bool runOnFunction(Function &F);
303 virtual void releaseMemory();
304 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
305 virtual void print(std::ostream &OS, const Module* = 0) const;
306 void print(std::ostream *OS, const Module* M = 0) const {
307 if (OS) print(*OS, M);