1 //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- 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 // 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/Support/DataTypes.h"
26 #include "llvm/Support/Streams.h"
38 class ScalarEvolution;
40 /// SCEV - This class represent an analyzed expression in the program. These
41 /// are reference counted opaque objects that the client is not allowed to
42 /// do much with directly.
45 const unsigned SCEVType; // The SCEV baseclass this node corresponds to
46 mutable unsigned RefCount;
48 friend class SCEVHandle;
49 void addRef() const { ++RefCount; }
50 void dropRef() const {
55 SCEV(const SCEV &); // DO NOT IMPLEMENT
56 void operator=(const SCEV &); // DO NOT IMPLEMENT
60 explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
62 unsigned getSCEVType() const { return SCEVType; }
64 /// getValueRange - Return the tightest constant bounds that this value is
65 /// known to have. This method is only valid on integer SCEV objects.
66 virtual ConstantRange getValueRange() const;
68 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
69 /// the specified loop.
70 virtual bool isLoopInvariant(const Loop *L) const = 0;
72 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
73 /// known way in the specified loop. This property being true implies that
74 /// the value is variant in the loop AND that we can emit an expression to
75 /// compute the value of the expression at any particular loop iteration.
76 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
78 /// getType - Return the LLVM type of this SCEV expression.
80 virtual const Type *getType() const = 0;
82 /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
84 uint32_t getBitWidth() const;
86 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
87 /// the symbolic value "Sym", construct and return a new SCEV that produces
88 /// the same value, but which uses the concrete value Conc instead of the
89 /// symbolic value. If this SCEV does not use the symbolic value, it
92 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
93 const SCEVHandle &Conc,
94 ScalarEvolution &SE) const = 0;
96 /// print - Print out the internal representation of this scalar to the
97 /// specified stream. This should really only be used for debugging
99 virtual void print(std::ostream &OS) const = 0;
100 void print(std::ostream *OS) const { if (OS) print(*OS); }
102 /// dump - This method is used for debugging.
107 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
112 /// SCEVCouldNotCompute - An object of this class is returned by queries that
113 /// could not be answered. For example, if you ask for the number of
114 /// iterations of a linked-list traversal loop, you will get one of these.
115 /// None of the standard SCEV operations are valid on this class, it is just a
117 struct SCEVCouldNotCompute : public SCEV {
118 SCEVCouldNotCompute();
120 // None of these methods are valid for this object.
121 virtual bool isLoopInvariant(const Loop *L) const;
122 virtual const Type *getType() const;
123 virtual bool hasComputableLoopEvolution(const Loop *L) const;
124 virtual void print(std::ostream &OS) const;
125 void print(std::ostream *OS) const { if (OS) print(*OS); }
127 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
128 const SCEVHandle &Conc,
129 ScalarEvolution &SE) const;
131 /// Methods for support type inquiry through isa, cast, and dyn_cast:
132 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
133 static bool classof(const SCEV *S);
136 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
137 /// freeing the objects when the last reference is dropped.
140 SCEVHandle(); // DO NOT IMPLEMENT
142 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
143 assert(S && "Cannot create a handle to a null SCEV!");
146 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
149 ~SCEVHandle() { S->dropRef(); }
151 operator SCEV*() const { return S; }
153 SCEV &operator*() const { return *S; }
154 SCEV *operator->() const { return S; }
156 bool operator==(SCEV *RHS) const { return S == RHS; }
157 bool operator!=(SCEV *RHS) const { return S != RHS; }
159 const SCEVHandle &operator=(SCEV *RHS) {
168 const SCEVHandle &operator=(const SCEVHandle &RHS) {
178 template<typename From> struct simplify_type;
179 template<> struct simplify_type<const SCEVHandle> {
180 typedef SCEV* SimpleType;
181 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
185 template<> struct simplify_type<SCEVHandle>
186 : public simplify_type<const SCEVHandle> {};
188 /// ScalarEvolution - This class is the main scalar evolution driver. Because
189 /// client code (intentionally) can't do much with the SCEV objects directly,
190 /// they must ask this class for services.
192 class ScalarEvolution : public FunctionPass {
193 void *Impl; // ScalarEvolution uses the pimpl pattern
195 static char ID; // Pass identification, replacement for typeid
196 ScalarEvolution() : FunctionPass((intptr_t)&ID), Impl(0) {}
198 /// getSCEV - Return a SCEV expression handle for the full generality of the
199 /// specified expression.
200 SCEVHandle getSCEV(Value *V) const;
202 SCEVHandle getConstant(ConstantInt *V);
203 SCEVHandle getConstant(const APInt& Val);
204 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
205 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
206 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
207 SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops);
208 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
209 std::vector<SCEVHandle> Ops;
212 return getAddExpr(Ops);
214 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
215 const SCEVHandle &Op2) {
216 std::vector<SCEVHandle> Ops;
220 return getAddExpr(Ops);
222 SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
223 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
224 std::vector<SCEVHandle> Ops;
227 return getMulExpr(Ops);
229 SCEVHandle getSDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
230 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
232 SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
234 SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
236 std::vector<SCEVHandle> NewOp(Operands);
237 return getAddRecExpr(NewOp, L);
239 SCEVHandle getUnknown(Value *V);
241 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
243 SCEVHandle getNegativeSCEV(const SCEVHandle &V);
245 /// getMinusSCEV - Return LHS-RHS.
247 SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
248 const SCEVHandle &RHS);
250 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
251 /// specified signed integer value and return a SCEV for the constant.
252 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
254 /// hasSCEV - Return true if the SCEV for this value has already been
256 bool hasSCEV(Value *V) const;
258 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
259 /// the specified value.
260 void setSCEV(Value *V, const SCEVHandle &H);
262 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
263 /// at the specified scope in the program. The L value specifies a loop
264 /// nest to evaluate the expression at, where null is the top-level or a
265 /// specified loop is immediately inside of the loop.
267 /// This method can be used to compute the exit value for a variable defined
268 /// in a loop by querying what the value will hold in the parent loop.
270 /// If this value is not computable at this scope, a SCEVCouldNotCompute
271 /// object is returned.
272 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
274 /// getIterationCount - If the specified loop has a predictable iteration
275 /// count, return it, otherwise return a SCEVCouldNotCompute object.
276 SCEVHandle getIterationCount(const Loop *L) const;
278 /// hasLoopInvariantIterationCount - Return true if the specified loop has
279 /// an analyzable loop-invariant iteration count.
280 bool hasLoopInvariantIterationCount(const Loop *L) const;
282 /// deleteValueFromRecords - This method should be called by the
283 /// client before it removes a Value from the program, to make sure
284 /// that no dangling references are left around.
285 void deleteValueFromRecords(Value *V) const;
287 virtual bool runOnFunction(Function &F);
288 virtual void releaseMemory();
289 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
290 virtual void print(std::ostream &OS, const Module* = 0) const;
291 void print(std::ostream *OS, const Module* M = 0) const {
292 if (OS) print(*OS, M);