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 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
63 /// the specified loop.
64 virtual bool isLoopInvariant(const Loop *L) const = 0;
66 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
67 /// known way in the specified loop. This property being true implies that
68 /// the value is variant in the loop AND that we can emit an expression to
69 /// compute the value of the expression at any particular loop iteration.
70 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
72 /// getType - Return the LLVM type of this SCEV expression.
74 virtual const Type *getType() const = 0;
76 /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
78 uint32_t getBitWidth() const;
80 /// isZero - Return true if the expression is a constant zero.
84 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
85 /// the symbolic value "Sym", construct and return a new SCEV that produces
86 /// the same value, but which uses the concrete value Conc instead of the
87 /// symbolic value. If this SCEV does not use the symbolic value, it
90 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
91 const SCEVHandle &Conc,
92 ScalarEvolution &SE) const = 0;
94 /// print - Print out the internal representation of this scalar to the
95 /// specified stream. This should really only be used for debugging
97 virtual void print(std::ostream &OS) const = 0;
98 void print(std::ostream *OS) const { if (OS) print(*OS); }
100 /// dump - This method is used for debugging.
105 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
110 /// SCEVCouldNotCompute - An object of this class is returned by queries that
111 /// could not be answered. For example, if you ask for the number of
112 /// iterations of a linked-list traversal loop, you will get one of these.
113 /// None of the standard SCEV operations are valid on this class, it is just a
115 struct SCEVCouldNotCompute : public SCEV {
116 SCEVCouldNotCompute();
118 // None of these methods are valid for this object.
119 virtual bool isLoopInvariant(const Loop *L) const;
120 virtual const Type *getType() const;
121 virtual bool hasComputableLoopEvolution(const Loop *L) const;
122 virtual void print(std::ostream &OS) const;
123 void print(std::ostream *OS) const { if (OS) print(*OS); }
125 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
126 const SCEVHandle &Conc,
127 ScalarEvolution &SE) const;
129 /// Methods for support type inquiry through isa, cast, and dyn_cast:
130 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
131 static bool classof(const SCEV *S);
134 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
135 /// freeing the objects when the last reference is dropped.
138 SCEVHandle(); // DO NOT IMPLEMENT
140 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
141 assert(S && "Cannot create a handle to a null SCEV!");
144 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
147 ~SCEVHandle() { S->dropRef(); }
149 operator SCEV*() const { return S; }
151 SCEV &operator*() const { return *S; }
152 SCEV *operator->() const { return S; }
154 bool operator==(SCEV *RHS) const { return S == RHS; }
155 bool operator!=(SCEV *RHS) const { return S != RHS; }
157 const SCEVHandle &operator=(SCEV *RHS) {
166 const SCEVHandle &operator=(const SCEVHandle &RHS) {
176 template<typename From> struct simplify_type;
177 template<> struct simplify_type<const SCEVHandle> {
178 typedef SCEV* SimpleType;
179 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
183 template<> struct simplify_type<SCEVHandle>
184 : public simplify_type<const SCEVHandle> {};
186 /// ScalarEvolution - This class is the main scalar evolution driver. Because
187 /// client code (intentionally) can't do much with the SCEV objects directly,
188 /// they must ask this class for services.
190 class ScalarEvolution : public FunctionPass {
191 void *Impl; // ScalarEvolution uses the pimpl pattern
193 static char ID; // Pass identification, replacement for typeid
194 ScalarEvolution() : FunctionPass(&ID), Impl(0) {}
196 /// getSCEV - Return a SCEV expression handle for the full generality of the
197 /// specified expression.
198 SCEVHandle getSCEV(Value *V) const;
200 SCEVHandle getConstant(ConstantInt *V);
201 SCEVHandle getConstant(const APInt& Val);
202 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
203 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
204 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
205 SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops);
206 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
207 std::vector<SCEVHandle> Ops;
210 return getAddExpr(Ops);
212 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
213 const SCEVHandle &Op2) {
214 std::vector<SCEVHandle> Ops;
218 return getAddExpr(Ops);
220 SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
221 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
222 std::vector<SCEVHandle> Ops;
225 return getMulExpr(Ops);
227 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
228 SCEVHandle getSDivExpr(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 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
258 /// of the input value to the specified type. If the type must be
259 /// extended, it is zero extended.
260 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
262 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
263 /// specified signed integer value and return a SCEV for the constant.
264 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
266 /// hasSCEV - Return true if the SCEV for this value has already been
268 bool hasSCEV(Value *V) const;
270 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
271 /// the specified value.
272 void setSCEV(Value *V, const SCEVHandle &H);
274 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
275 /// at the specified scope in the program. The L value specifies a loop
276 /// nest to evaluate the expression at, where null is the top-level or a
277 /// specified loop is immediately inside of the loop.
279 /// This method can be used to compute the exit value for a variable defined
280 /// in a loop by querying what the value will hold in the parent loop.
282 /// If this value is not computable at this scope, a SCEVCouldNotCompute
283 /// object is returned.
284 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
286 /// getIterationCount - If the specified loop has a predictable iteration
287 /// count, return it, otherwise return a SCEVCouldNotCompute object.
288 SCEVHandle getIterationCount(const Loop *L) const;
290 /// hasLoopInvariantIterationCount - Return true if the specified loop has
291 /// an analyzable loop-invariant iteration count.
292 bool hasLoopInvariantIterationCount(const Loop *L) const;
294 /// deleteValueFromRecords - This method should be called by the
295 /// client before it removes a Value from the program, to make sure
296 /// that no dangling references are left around.
297 void deleteValueFromRecords(Value *V) const;
299 virtual bool runOnFunction(Function &F);
300 virtual void releaseMemory();
301 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
302 virtual void print(std::ostream &OS, const Module* = 0) const;
303 void print(std::ostream *OS, const Module* M = 0) const {
304 if (OS) print(*OS, M);