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"
34 class ScalarEvolution;
37 /// SCEV - This class represent an analyzed expression in the program. These
38 /// are reference counted opaque objects that the client is not allowed to
39 /// do much with directly.
42 const unsigned SCEVType; // The SCEV baseclass this node corresponds to
43 mutable unsigned RefCount;
45 friend class SCEVHandle;
46 void addRef() const { ++RefCount; }
47 void dropRef() const {
52 SCEV(const SCEV &); // DO NOT IMPLEMENT
53 void operator=(const SCEV &); // DO NOT IMPLEMENT
57 explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
59 unsigned getSCEVType() const { return SCEVType; }
61 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
62 /// the specified loop.
63 virtual bool isLoopInvariant(const Loop *L) const = 0;
65 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
66 /// known way in the specified loop. This property being true implies that
67 /// the value is variant in the loop AND that we can emit an expression to
68 /// compute the value of the expression at any particular loop iteration.
69 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
71 /// getType - Return the LLVM type of this SCEV expression.
73 virtual const Type *getType() const = 0;
75 /// isZero - Return true if the expression is a constant zero.
79 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
80 /// the symbolic value "Sym", construct and return a new SCEV that produces
81 /// the same value, but which uses the concrete value Conc instead of the
82 /// symbolic value. If this SCEV does not use the symbolic value, it
85 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
86 const SCEVHandle &Conc,
87 ScalarEvolution &SE) const = 0;
89 /// dominates - Return true if elements that makes up this SCEV dominates
90 /// the specified basic block.
91 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0;
93 /// print - Print out the internal representation of this scalar to the
94 /// specified stream. This should really only be used for debugging
96 virtual void print(std::ostream &OS) const = 0;
97 void print(std::ostream *OS) const { if (OS) print(*OS); }
99 /// dump - This method is used for debugging.
104 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
109 /// SCEVCouldNotCompute - An object of this class is returned by queries that
110 /// could not be answered. For example, if you ask for the number of
111 /// iterations of a linked-list traversal loop, you will get one of these.
112 /// None of the standard SCEV operations are valid on this class, it is just a
114 struct SCEVCouldNotCompute : public SCEV {
115 SCEVCouldNotCompute();
117 // None of these methods are valid for this object.
118 virtual bool isLoopInvariant(const Loop *L) const;
119 virtual const Type *getType() const;
120 virtual bool hasComputableLoopEvolution(const Loop *L) const;
121 virtual void print(std::ostream &OS) const;
122 void print(std::ostream *OS) const { if (OS) print(*OS); }
124 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
125 const SCEVHandle &Conc,
126 ScalarEvolution &SE) const;
128 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
132 /// Methods for support type inquiry through isa, cast, and dyn_cast:
133 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
134 static bool classof(const SCEV *S);
137 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
138 /// freeing the objects when the last reference is dropped.
141 SCEVHandle(); // DO NOT IMPLEMENT
143 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
144 assert(S && "Cannot create a handle to a null SCEV!");
147 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
150 ~SCEVHandle() { S->dropRef(); }
152 operator SCEV*() const { return S; }
154 SCEV &operator*() const { return *S; }
155 SCEV *operator->() const { return S; }
157 bool operator==(SCEV *RHS) const { return S == RHS; }
158 bool operator!=(SCEV *RHS) const { return S != RHS; }
160 const SCEVHandle &operator=(SCEV *RHS) {
169 const SCEVHandle &operator=(const SCEVHandle &RHS) {
179 template<typename From> struct simplify_type;
180 template<> struct simplify_type<const SCEVHandle> {
181 typedef SCEV* SimpleType;
182 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
186 template<> struct simplify_type<SCEVHandle>
187 : public simplify_type<const SCEVHandle> {};
189 /// ScalarEvolution - This class is the main scalar evolution driver. Because
190 /// client code (intentionally) can't do much with the SCEV objects directly,
191 /// they must ask this class for services.
193 class ScalarEvolution : public FunctionPass {
194 void *Impl; // ScalarEvolution uses the pimpl pattern
196 static char ID; // Pass identification, replacement for typeid
197 ScalarEvolution() : FunctionPass(&ID), Impl(0) {}
199 // getTargetData - Return the TargetData object contained in this
201 const TargetData &getTargetData() const;
203 /// getSCEV - Return a SCEV expression handle for the full generality of the
204 /// specified expression.
205 SCEVHandle getSCEV(Value *V) const;
207 SCEVHandle getConstant(ConstantInt *V);
208 SCEVHandle getConstant(const APInt& Val);
209 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
210 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
211 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
212 SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops);
213 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
214 std::vector<SCEVHandle> Ops;
217 return getAddExpr(Ops);
219 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
220 const SCEVHandle &Op2) {
221 std::vector<SCEVHandle> Ops;
225 return getAddExpr(Ops);
227 SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
228 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
229 std::vector<SCEVHandle> Ops;
232 return getMulExpr(Ops);
234 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
235 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
237 SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
239 SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
241 std::vector<SCEVHandle> NewOp(Operands);
242 return getAddRecExpr(NewOp, L);
244 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
245 SCEVHandle getSMaxExpr(std::vector<SCEVHandle> Operands);
246 SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
247 SCEVHandle getUMaxExpr(std::vector<SCEVHandle> Operands);
248 SCEVHandle getUnknown(Value *V);
250 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
252 SCEVHandle getNegativeSCEV(const SCEVHandle &V);
254 /// getNotSCEV - Return the SCEV object corresponding to ~V.
256 SCEVHandle getNotSCEV(const SCEVHandle &V);
258 /// getMinusSCEV - Return LHS-RHS.
260 SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
261 const SCEVHandle &RHS);
263 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
264 /// of the input value to the specified type. If the type must be
265 /// extended, it is zero extended.
266 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
268 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
269 /// of the input value to the specified type. If the type must be
270 /// extended, it is sign extended.
271 SCEVHandle getTruncateOrSignExtend(const SCEVHandle &V, const Type *Ty);
273 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
274 /// specified signed integer value and return a SCEV for the constant.
275 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
277 /// hasSCEV - Return true if the SCEV for this value has already been
279 bool hasSCEV(Value *V) const;
281 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
282 /// the specified value.
283 void setSCEV(Value *V, const SCEVHandle &H);
285 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
286 /// at the specified scope in the program. The L value specifies a loop
287 /// nest to evaluate the expression at, where null is the top-level or a
288 /// specified loop is immediately inside of the loop.
290 /// This method can be used to compute the exit value for a variable defined
291 /// in a loop by querying what the value will hold in the parent loop.
293 /// If this value is not computable at this scope, a SCEVCouldNotCompute
294 /// object is returned.
295 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
297 /// isLoopGuardedByCond - Test whether entry to the loop is protected by
298 /// a conditional between LHS and RHS.
299 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
300 SCEV *LHS, SCEV *RHS);
302 /// getBackedgeTakenCount - If the specified loop has a predictable
303 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
304 /// object. The backedge-taken count is the number of times the loop header
305 /// will be branched to from within the loop. This is one less than the
306 /// trip count of the loop, since it doesn't count the first iteration,
307 /// when the header is branched to from outside the loop.
309 /// Note that it is not valid to call this method on a loop without a
310 /// loop-invariant backedge-taken count (see
311 /// hasLoopInvariantBackedgeTakenCount).
313 SCEVHandle getBackedgeTakenCount(const Loop *L) const;
315 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
316 /// has an analyzable loop-invariant backedge-taken count.
317 bool hasLoopInvariantBackedgeTakenCount(const Loop *L) const;
319 /// forgetLoopBackedgeTakenCount - This method should be called by the
320 /// client when it has changed a loop in a way that may effect
321 /// ScalarEvolution's ability to compute a trip count, or if the loop
323 void forgetLoopBackedgeTakenCount(const Loop *L);
325 /// deleteValueFromRecords - This method should be called by the
326 /// client before it removes a Value from the program, to make sure
327 /// that no dangling references are left around.
328 void deleteValueFromRecords(Value *V) const;
330 virtual bool runOnFunction(Function &F);
331 virtual void releaseMemory();
332 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
333 virtual void print(std::ostream &OS, const Module* = 0) const;
334 void print(std::ostream *OS, const Module* M = 0) const {
335 if (OS) print(*OS, M);