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/Streams.h"
36 /// SCEV - This class represent an analyzed expression in the program. These
37 /// are reference counted opaque objects that the client is not allowed to
38 /// do much with directly.
41 const unsigned SCEVType; // The SCEV baseclass this node corresponds to
42 mutable unsigned RefCount;
44 friend class SCEVHandle;
45 void addRef() const { ++RefCount; }
46 void dropRef() const {
51 SCEV(const SCEV &); // DO NOT IMPLEMENT
52 void operator=(const SCEV &); // DO NOT IMPLEMENT
56 SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
58 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
60 static SCEVHandle getNegativeSCEV(const SCEVHandle &V);
62 /// getMinusSCEV - Return LHS-RHS.
64 static SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
65 const SCEVHandle &RHS);
68 unsigned getSCEVType() const { return SCEVType; }
70 /// getValueRange - Return the tightest constant bounds that this value is
71 /// known to have. This method is only valid on integer SCEV objects.
72 virtual ConstantRange getValueRange() const;
74 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
75 /// the specified loop.
76 virtual bool isLoopInvariant(const Loop *L) const = 0;
78 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
79 /// known way in the specified loop. This property being true implies that
80 /// the value is variant in the loop AND that we can emit an expression to
81 /// compute the value of the expression at any particular loop iteration.
82 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
84 /// getType - Return the LLVM type of this SCEV expression.
86 virtual const Type *getType() const = 0;
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) const = 0;
97 /// print - Print out the internal representation of this scalar to the
98 /// specified stream. This should really only be used for debugging
100 void print(llvm_ostream &OS) const {
101 if (OS.stream()) print(*OS.stream());
103 virtual void print(std::ostream &OS) const = 0;
105 /// dump - This method is used for debugging.
110 inline llvm_ostream &operator<<(llvm_ostream &OS, const SCEV &S) {
114 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
119 /// SCEVCouldNotCompute - An object of this class is returned by queries that
120 /// could not be answered. For example, if you ask for the number of
121 /// iterations of a linked-list traversal loop, you will get one of these.
122 /// None of the standard SCEV operations are valid on this class, it is just a
124 struct SCEVCouldNotCompute : public SCEV {
125 SCEVCouldNotCompute();
127 // None of these methods are valid for this object.
128 virtual bool isLoopInvariant(const Loop *L) const;
129 virtual const Type *getType() const;
130 virtual bool hasComputableLoopEvolution(const Loop *L) const;
131 void print(llvm_ostream &OS) const {
132 if (OS.stream()) print(*OS.stream());
134 virtual void print(std::ostream &OS) const;
136 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
137 const SCEVHandle &Conc) const;
139 /// Methods for support type inquiry through isa, cast, and dyn_cast:
140 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
141 static bool classof(const SCEV *S);
144 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
145 /// freeing the objects when the last reference is dropped.
148 SCEVHandle(); // DO NOT IMPLEMENT
150 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
151 assert(S && "Cannot create a handle to a null SCEV!");
154 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
157 ~SCEVHandle() { S->dropRef(); }
159 operator SCEV*() const { return S; }
161 SCEV &operator*() const { return *S; }
162 SCEV *operator->() const { return S; }
164 bool operator==(SCEV *RHS) const { return S == RHS; }
165 bool operator!=(SCEV *RHS) const { return S != RHS; }
167 const SCEVHandle &operator=(SCEV *RHS) {
176 const SCEVHandle &operator=(const SCEVHandle &RHS) {
186 template<typename From> struct simplify_type;
187 template<> struct simplify_type<const SCEVHandle> {
188 typedef SCEV* SimpleType;
189 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
193 template<> struct simplify_type<SCEVHandle>
194 : public simplify_type<const SCEVHandle> {};
196 /// ScalarEvolution - This class is the main scalar evolution driver. Because
197 /// client code (intentionally) can't do much with the SCEV objects directly,
198 /// they must ask this class for services.
200 class ScalarEvolution : public FunctionPass {
201 void *Impl; // ScalarEvolution uses the pimpl pattern
203 ScalarEvolution() : Impl(0) {}
205 /// getSCEV - Return a SCEV expression handle for the full generality of the
206 /// specified expression.
207 SCEVHandle getSCEV(Value *V) const;
209 /// hasSCEV - Return true if the SCEV for this value has already been
211 bool hasSCEV(Value *V) const;
213 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
214 /// the specified value.
215 void setSCEV(Value *V, const SCEVHandle &H);
217 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
218 /// at the specified scope in the program. The L value specifies a loop
219 /// nest to evaluate the expression at, where null is the top-level or a
220 /// specified loop is immediately inside of the loop.
222 /// This method can be used to compute the exit value for a variable defined
223 /// in a loop by querying what the value will hold in the parent loop.
225 /// If this value is not computable at this scope, a SCEVCouldNotCompute
226 /// object is returned.
227 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
229 /// getIterationCount - If the specified loop has a predictable iteration
230 /// count, return it, otherwise return a SCEVCouldNotCompute object.
231 SCEVHandle getIterationCount(const Loop *L) const;
233 /// hasLoopInvariantIterationCount - Return true if the specified loop has
234 /// an analyzable loop-invariant iteration count.
235 bool hasLoopInvariantIterationCount(const Loop *L) const;
237 /// deleteInstructionFromRecords - This method should be called by the
238 /// client before it removes an instruction from the program, to make sure
239 /// that no dangling references are left around.
240 void deleteInstructionFromRecords(Instruction *I) const;
242 virtual bool runOnFunction(Function &F);
243 virtual void releaseMemory();
244 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
245 void print(llvm_ostream &OS, const Module* = 0) const {
246 if (OS.stream()) print(*OS.stream());
248 virtual void print(std::ostream &OS, const Module* = 0) const;