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"
27 #include "llvm/Support/Streams.h"
37 class ScalarEvolution;
39 /// SCEV - This class represent an analyzed expression in the program. These
40 /// are reference counted opaque objects that the client is not allowed to
41 /// do much with directly.
44 const unsigned SCEVType; // The SCEV baseclass this node corresponds to
45 mutable unsigned RefCount;
47 friend class SCEVHandle;
48 void addRef() const { ++RefCount; }
49 void dropRef() const {
54 SCEV(const SCEV &); // DO NOT IMPLEMENT
55 void operator=(const SCEV &); // DO NOT IMPLEMENT
59 explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
61 unsigned getSCEVType() const { return SCEVType; }
63 /// getValueRange - Return the tightest constant bounds that this value is
64 /// known to have. This method is only valid on integer SCEV objects.
65 virtual ConstantRange getValueRange() const;
67 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
68 /// the specified loop.
69 virtual bool isLoopInvariant(const Loop *L) const = 0;
71 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
72 /// known way in the specified loop. This property being true implies that
73 /// the value is variant in the loop AND that we can emit an expression to
74 /// compute the value of the expression at any particular loop iteration.
75 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
77 /// getType - Return the LLVM type of this SCEV expression.
79 virtual const Type *getType() const = 0;
81 /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
83 uint32_t getBitWidth() const;
85 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
86 /// the symbolic value "Sym", construct and return a new SCEV that produces
87 /// the same value, but which uses the concrete value Conc instead of the
88 /// symbolic value. If this SCEV does not use the symbolic value, it
91 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
92 const SCEVHandle &Conc,
93 ScalarEvolution &SE) const = 0;
95 /// print - Print out the internal representation of this scalar to the
96 /// specified stream. This should really only be used for debugging
98 virtual void print(std::ostream &OS) const = 0;
99 void print(std::ostream *OS) const { if (OS) print(*OS); }
101 /// dump - This method is used for debugging.
106 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
111 /// SCEVCouldNotCompute - An object of this class is returned by queries that
112 /// could not be answered. For example, if you ask for the number of
113 /// iterations of a linked-list traversal loop, you will get one of these.
114 /// None of the standard SCEV operations are valid on this class, it is just a
116 struct SCEVCouldNotCompute : public SCEV {
117 SCEVCouldNotCompute();
119 // None of these methods are valid for this object.
120 virtual bool isLoopInvariant(const Loop *L) const;
121 virtual const Type *getType() const;
122 virtual bool hasComputableLoopEvolution(const Loop *L) const;
123 virtual void print(std::ostream &OS) const;
124 void print(std::ostream *OS) const { if (OS) print(*OS); }
126 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
127 const SCEVHandle &Conc,
128 ScalarEvolution &SE) const;
130 /// Methods for support type inquiry through isa, cast, and dyn_cast:
131 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
132 static bool classof(const SCEV *S);
135 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
136 /// freeing the objects when the last reference is dropped.
139 SCEVHandle(); // DO NOT IMPLEMENT
141 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
142 assert(S && "Cannot create a handle to a null SCEV!");
145 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
148 ~SCEVHandle() { S->dropRef(); }
150 operator SCEV*() const { return S; }
152 SCEV &operator*() const { return *S; }
153 SCEV *operator->() const { return S; }
155 bool operator==(SCEV *RHS) const { return S == RHS; }
156 bool operator!=(SCEV *RHS) const { return S != RHS; }
158 const SCEVHandle &operator=(SCEV *RHS) {
167 const SCEVHandle &operator=(const SCEVHandle &RHS) {
177 template<typename From> struct simplify_type;
178 template<> struct simplify_type<const SCEVHandle> {
179 typedef SCEV* SimpleType;
180 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
184 template<> struct simplify_type<SCEVHandle>
185 : public simplify_type<const SCEVHandle> {};
187 /// ScalarEvolution - This class is the main scalar evolution driver. Because
188 /// client code (intentionally) can't do much with the SCEV objects directly,
189 /// they must ask this class for services.
191 class ScalarEvolution : public FunctionPass {
192 void *Impl; // ScalarEvolution uses the pimpl pattern
194 static char ID; // Pass identification, replacement for typeid
195 ScalarEvolution() : FunctionPass((intptr_t)&ID), Impl(0) {}
197 /// getSCEV - Return a SCEV expression handle for the full generality of the
198 /// specified expression.
199 SCEVHandle getSCEV(Value *V) const;
201 SCEVHandle getConstant(ConstantInt *V);
202 SCEVHandle getConstant(const APInt& Val);
203 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
204 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
205 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
206 SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops);
207 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
208 std::vector<SCEVHandle> Ops;
211 return getAddExpr(Ops);
213 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
214 const SCEVHandle &Op2) {
215 std::vector<SCEVHandle> Ops;
219 return getAddExpr(Ops);
221 SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
222 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
223 std::vector<SCEVHandle> Ops;
226 return getMulExpr(Ops);
228 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
229 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
231 SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
233 SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
235 std::vector<SCEVHandle> NewOp(Operands);
236 return getAddRecExpr(NewOp, L);
238 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
239 SCEVHandle getSMaxExpr(std::vector<SCEVHandle> Operands);
240 SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
241 SCEVHandle getUMaxExpr(std::vector<SCEVHandle> Operands);
242 SCEVHandle getUnknown(Value *V);
244 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
246 SCEVHandle getNegativeSCEV(const SCEVHandle &V);
248 /// getNotSCEV - Return the SCEV object corresponding to ~V.
250 SCEVHandle getNotSCEV(const SCEVHandle &V);
252 /// getMinusSCEV - Return LHS-RHS.
254 SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
255 const SCEVHandle &RHS);
257 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
258 /// specified signed integer value and return a SCEV for the constant.
259 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
261 /// hasSCEV - Return true if the SCEV for this value has already been
263 bool hasSCEV(Value *V) const;
265 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
266 /// the specified value.
267 void setSCEV(Value *V, const SCEVHandle &H);
269 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
270 /// at the specified scope in the program. The L value specifies a loop
271 /// nest to evaluate the expression at, where null is the top-level or a
272 /// specified loop is immediately inside of the loop.
274 /// This method can be used to compute the exit value for a variable defined
275 /// in a loop by querying what the value will hold in the parent loop.
277 /// If this value is not computable at this scope, a SCEVCouldNotCompute
278 /// object is returned.
279 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
281 /// getIterationCount - If the specified loop has a predictable iteration
282 /// count, return it, otherwise return a SCEVCouldNotCompute object.
283 SCEVHandle getIterationCount(const Loop *L) const;
285 /// hasLoopInvariantIterationCount - Return true if the specified loop has
286 /// an analyzable loop-invariant iteration count.
287 bool hasLoopInvariantIterationCount(const Loop *L) const;
289 /// deleteValueFromRecords - This method should be called by the
290 /// client before it removes a Value from the program, to make sure
291 /// that no dangling references are left around.
292 void deleteValueFromRecords(Value *V) const;
294 virtual bool runOnFunction(Function &F);
295 virtual void releaseMemory();
296 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
297 virtual void print(std::ostream &OS, const Module* = 0) const;
298 void print(std::ostream *OS, const Module* M = 0) const {
299 if (OS) print(*OS, M);