Identify Analysis pass.
[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 is distributed under the University of Illinois Open Source
6 // 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/Analysis/LoopInfo.h"
26 #include "llvm/Support/DataTypes.h"
27 #include "llvm/Support/Streams.h"
28 #include <set>
29
30 namespace llvm {
31   class APInt;
32   class ConstantInt;
33   class Instruction;
34   class Type;
35   class ConstantRange;
36   class SCEVHandle;
37   class ScalarEvolution;
38
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.
42   ///
43   class SCEV {
44     const unsigned SCEVType;      // The SCEV baseclass this node corresponds to
45     mutable unsigned RefCount;
46
47     friend class SCEVHandle;
48     void addRef() const { ++RefCount; }
49     void dropRef() const {
50       if (--RefCount == 0)
51         delete this;
52     }
53
54     SCEV(const SCEV &);            // DO NOT IMPLEMENT
55     void operator=(const SCEV &);  // DO NOT IMPLEMENT
56   protected:
57     virtual ~SCEV();
58   public:
59     explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
60
61     unsigned getSCEVType() const { return SCEVType; }
62
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;
66
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;
70
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;
76
77     /// getType - Return the LLVM type of this SCEV expression.
78     ///
79     virtual const Type *getType() const = 0;
80
81     /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
82     /// 
83     uint32_t getBitWidth() const;
84
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
89     /// returns itself.
90     virtual SCEVHandle
91     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
92                                       const SCEVHandle &Conc,
93                                       ScalarEvolution &SE) const = 0;
94
95     /// print - Print out the internal representation of this scalar to the
96     /// specified stream.  This should really only be used for debugging
97     /// purposes.
98     virtual void print(std::ostream &OS) const = 0;
99     void print(std::ostream *OS) const { if (OS) print(*OS); }
100
101     /// dump - This method is used for debugging.
102     ///
103     void dump() const;
104   };
105
106   inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
107     S.print(OS);
108     return OS;
109   }
110
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
115   /// marker.
116   struct SCEVCouldNotCompute : public SCEV {
117     SCEVCouldNotCompute();
118
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); }
125     virtual SCEVHandle
126     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
127                                       const SCEVHandle &Conc,
128                                       ScalarEvolution &SE) const;
129
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);
133   };
134
135   /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
136   /// freeing the objects when the last reference is dropped.
137   class SCEVHandle {
138     SCEV *S;
139     SCEVHandle();  // DO NOT IMPLEMENT
140   public:
141     SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
142       assert(S && "Cannot create a handle to a null SCEV!");
143       S->addRef();
144     }
145     SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
146       S->addRef();
147     }
148     ~SCEVHandle() { S->dropRef(); }
149
150     operator SCEV*() const { return S; }
151
152     SCEV &operator*() const { return *S; }
153     SCEV *operator->() const { return S; }
154
155     bool operator==(SCEV *RHS) const { return S == RHS; }
156     bool operator!=(SCEV *RHS) const { return S != RHS; }
157
158     const SCEVHandle &operator=(SCEV *RHS) {
159       if (S != RHS) {
160         S->dropRef();
161         S = RHS;
162         S->addRef();
163       }
164       return *this;
165     }
166
167     const SCEVHandle &operator=(const SCEVHandle &RHS) {
168       if (S != RHS.S) {
169         S->dropRef();
170         S = RHS.S;
171         S->addRef();
172       }
173       return *this;
174     }
175   };
176
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) {
181       return Node;
182     }
183   };
184   template<> struct simplify_type<SCEVHandle>
185     : public simplify_type<const SCEVHandle> {};
186
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.
190   ///
191   class ScalarEvolution : public FunctionPass {
192     void *Impl;    // ScalarEvolution uses the pimpl pattern
193   public:
194     static char ID; // Pass identification, replacement for typeid
195     ScalarEvolution() : FunctionPass((intptr_t)&ID), Impl(0) {}
196
197     /// getSCEV - Return a SCEV expression handle for the full generality of the
198     /// specified expression.
199     SCEVHandle getSCEV(Value *V) const;
200
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;
209       Ops.push_back(LHS);
210       Ops.push_back(RHS);
211       return getAddExpr(Ops);
212     }
213     SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
214                           const SCEVHandle &Op2) {
215       std::vector<SCEVHandle> Ops;
216       Ops.push_back(Op0);
217       Ops.push_back(Op1);
218       Ops.push_back(Op2);
219       return getAddExpr(Ops);
220     }
221     SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
222     SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
223       std::vector<SCEVHandle> Ops;
224       Ops.push_back(LHS);
225       Ops.push_back(RHS);
226       return getMulExpr(Ops);
227     }
228     SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
229     SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
230                              const Loop *L);
231     SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
232                              const Loop *L);
233     SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
234                              const Loop *L) {
235       std::vector<SCEVHandle> NewOp(Operands);
236       return getAddRecExpr(NewOp, L);
237     }
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);
243
244     /// getNegativeSCEV - Return the SCEV object corresponding to -V.
245     ///
246     SCEVHandle getNegativeSCEV(const SCEVHandle &V);
247
248     /// getNotSCEV - Return the SCEV object corresponding to ~V.
249     ///
250     SCEVHandle getNotSCEV(const SCEVHandle &V);
251
252     /// getMinusSCEV - Return LHS-RHS.
253     ///
254     SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
255                             const SCEVHandle &RHS);
256
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);
260
261     /// hasSCEV - Return true if the SCEV for this value has already been
262     /// computed.
263     bool hasSCEV(Value *V) const;
264
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);
268
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.
273     ///
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.
276     ///
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;
280
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;
284
285     /// hasLoopInvariantIterationCount - Return true if the specified loop has
286     /// an analyzable loop-invariant iteration count.
287     bool hasLoopInvariantIterationCount(const Loop *L) const;
288
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;
293
294     /// isAnalysis - Return true if this pass is  implementing an analysis pass.
295     virtual bool isAnalysis() const { return true; }
296
297     virtual bool runOnFunction(Function &F);
298     virtual void releaseMemory();
299     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
300     virtual void print(std::ostream &OS, const Module* = 0) const;
301     void print(std::ostream *OS, const Module* M = 0) const {
302       if (OS) print(*OS, M);
303     }
304   };
305 }
306
307 #endif