Move the SCEV object factors from being static members of the individual
[oota-llvm.git] / include / llvm / Analysis / ScalarEvolution.h
1 //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
15 //
16 // This analysis is primarily useful for induction variable substitution and
17 // strength reduction.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
22 #define LLVM_ANALYSIS_SCALAREVOLUTION_H
23
24 #include "llvm/Pass.h"
25 #include "llvm/Support/DataTypes.h"
26 #include "llvm/Support/Streams.h"
27 #include <set>
28
29 namespace llvm {
30   class APInt;
31   class ConstantInt;
32   class Instruction;
33   class Type;
34   class ConstantRange;
35   class Loop;
36   class LoopInfo;
37   class SCEVHandle;
38   class ScalarEvolution;
39
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.
43   ///
44   class SCEV {
45     const unsigned SCEVType;      // The SCEV baseclass this node corresponds to
46     mutable unsigned RefCount;
47
48     friend class SCEVHandle;
49     void addRef() const { ++RefCount; }
50     void dropRef() const {
51       if (--RefCount == 0)
52         delete this;
53     }
54
55     SCEV(const SCEV &);            // DO NOT IMPLEMENT
56     void operator=(const SCEV &);  // DO NOT IMPLEMENT
57   protected:
58     virtual ~SCEV();
59   public:
60     explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
61
62     unsigned getSCEVType() const { return SCEVType; }
63
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;
67
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;
71
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;
77
78     /// getType - Return the LLVM type of this SCEV expression.
79     ///
80     virtual const Type *getType() const = 0;
81
82     /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
83     /// 
84     uint32_t getBitWidth() const;
85
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
90     /// returns itself.
91     virtual SCEVHandle
92     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
93                                       const SCEVHandle &Conc,
94                                       ScalarEvolution &SE) const = 0;
95
96     /// print - Print out the internal representation of this scalar to the
97     /// specified stream.  This should really only be used for debugging
98     /// purposes.
99     virtual void print(std::ostream &OS) const = 0;
100     void print(std::ostream *OS) const { if (OS) print(*OS); }
101
102     /// dump - This method is used for debugging.
103     ///
104     void dump() const;
105   };
106
107   inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
108     S.print(OS);
109     return OS;
110   }
111
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
116   /// marker.
117   struct SCEVCouldNotCompute : public SCEV {
118     SCEVCouldNotCompute();
119
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); }
126     virtual SCEVHandle
127     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
128                                       const SCEVHandle &Conc,
129                                       ScalarEvolution &SE) const;
130
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);
134   };
135
136   /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
137   /// freeing the objects when the last reference is dropped.
138   class SCEVHandle {
139     SCEV *S;
140     SCEVHandle();  // DO NOT IMPLEMENT
141   public:
142     SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
143       assert(S && "Cannot create a handle to a null SCEV!");
144       S->addRef();
145     }
146     SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
147       S->addRef();
148     }
149     ~SCEVHandle() { S->dropRef(); }
150
151     operator SCEV*() const { return S; }
152
153     SCEV &operator*() const { return *S; }
154     SCEV *operator->() const { return S; }
155
156     bool operator==(SCEV *RHS) const { return S == RHS; }
157     bool operator!=(SCEV *RHS) const { return S != RHS; }
158
159     const SCEVHandle &operator=(SCEV *RHS) {
160       if (S != RHS) {
161         S->dropRef();
162         S = RHS;
163         S->addRef();
164       }
165       return *this;
166     }
167
168     const SCEVHandle &operator=(const SCEVHandle &RHS) {
169       if (S != RHS.S) {
170         S->dropRef();
171         S = RHS.S;
172         S->addRef();
173       }
174       return *this;
175     }
176   };
177
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) {
182       return Node;
183     }
184   };
185   template<> struct simplify_type<SCEVHandle>
186     : public simplify_type<const SCEVHandle> {};
187
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.
191   ///
192   class ScalarEvolution : public FunctionPass {
193     void *Impl;    // ScalarEvolution uses the pimpl pattern
194   public:
195     static char ID; // Pass identification, replacement for typeid
196     ScalarEvolution() : FunctionPass((intptr_t)&ID), Impl(0) {}
197
198     /// getSCEV - Return a SCEV expression handle for the full generality of the
199     /// specified expression.
200     SCEVHandle getSCEV(Value *V) const;
201
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;
210       Ops.push_back(LHS);
211       Ops.push_back(RHS);
212       return getAddExpr(Ops);
213     }
214     SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
215                           const SCEVHandle &Op2) {
216       std::vector<SCEVHandle> Ops;
217       Ops.push_back(Op0);
218       Ops.push_back(Op1);
219       Ops.push_back(Op2);
220       return getAddExpr(Ops);
221     }
222     SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
223     SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
224       std::vector<SCEVHandle> Ops;
225       Ops.push_back(LHS);
226       Ops.push_back(RHS);
227       return getMulExpr(Ops);
228     }
229     SCEVHandle getSDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
230     SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
231                              const Loop *L);
232     SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
233                              const Loop *L);
234     SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
235                              const Loop *L) {
236       std::vector<SCEVHandle> NewOp(Operands);
237       return getAddRecExpr(NewOp, L);
238     }
239     SCEVHandle getUnknown(Value *V);
240
241     /// getNegativeSCEV - Return the SCEV object corresponding to -V.
242     ///
243     SCEVHandle getNegativeSCEV(const SCEVHandle &V);
244
245     /// getMinusSCEV - Return LHS-RHS.
246     ///
247     SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
248                             const SCEVHandle &RHS);
249
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);
253
254     /// hasSCEV - Return true if the SCEV for this value has already been
255     /// computed.
256     bool hasSCEV(Value *V) const;
257
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);
261
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.
266     ///
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.
269     ///
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;
273
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;
277
278     /// hasLoopInvariantIterationCount - Return true if the specified loop has
279     /// an analyzable loop-invariant iteration count.
280     bool hasLoopInvariantIterationCount(const Loop *L) const;
281
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;
286
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);
293     }
294   };
295 }
296
297 #endif