Move LSR's private isZero function to a public SCEV member
[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 <iosfwd>
28
29 namespace llvm {
30   class APInt;
31   class ConstantInt;
32   class Instruction;
33   class Type;
34   class ConstantRange;
35   class SCEVHandle;
36   class ScalarEvolution;
37
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.
41   ///
42   class SCEV {
43     const unsigned SCEVType;      // The SCEV baseclass this node corresponds to
44     mutable unsigned RefCount;
45
46     friend class SCEVHandle;
47     void addRef() const { ++RefCount; }
48     void dropRef() const {
49       if (--RefCount == 0)
50         delete this;
51     }
52
53     SCEV(const SCEV &);            // DO NOT IMPLEMENT
54     void operator=(const SCEV &);  // DO NOT IMPLEMENT
55   protected:
56     virtual ~SCEV();
57   public:
58     explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
59
60     unsigned getSCEVType() const { return SCEVType; }
61
62     /// getValueRange - Return the tightest constant bounds that this value is
63     /// known to have.  This method is only valid on integer SCEV objects.
64     virtual ConstantRange getValueRange() const;
65
66     /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
67     /// the specified loop.
68     virtual bool isLoopInvariant(const Loop *L) const = 0;
69
70     /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
71     /// known way in the specified loop.  This property being true implies that
72     /// the value is variant in the loop AND that we can emit an expression to
73     /// compute the value of the expression at any particular loop iteration.
74     virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
75
76     /// getType - Return the LLVM type of this SCEV expression.
77     ///
78     virtual const Type *getType() const = 0;
79
80     /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
81     /// 
82     uint32_t getBitWidth() const;
83
84     /// isZero - Return true if the expression is a constant zero.
85     ///
86     bool isZero() const;
87
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
92     /// returns itself.
93     virtual SCEVHandle
94     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
95                                       const SCEVHandle &Conc,
96                                       ScalarEvolution &SE) const = 0;
97
98     /// print - Print out the internal representation of this scalar to the
99     /// specified stream.  This should really only be used for debugging
100     /// purposes.
101     virtual void print(std::ostream &OS) const = 0;
102     void print(std::ostream *OS) const { if (OS) print(*OS); }
103
104     /// dump - This method is used for debugging.
105     ///
106     void dump() const;
107   };
108
109   inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
110     S.print(OS);
111     return OS;
112   }
113
114   /// SCEVCouldNotCompute - An object of this class is returned by queries that
115   /// could not be answered.  For example, if you ask for the number of
116   /// iterations of a linked-list traversal loop, you will get one of these.
117   /// None of the standard SCEV operations are valid on this class, it is just a
118   /// marker.
119   struct SCEVCouldNotCompute : public SCEV {
120     SCEVCouldNotCompute();
121
122     // None of these methods are valid for this object.
123     virtual bool isLoopInvariant(const Loop *L) const;
124     virtual const Type *getType() const;
125     virtual bool hasComputableLoopEvolution(const Loop *L) const;
126     virtual void print(std::ostream &OS) const;
127     void print(std::ostream *OS) const { if (OS) print(*OS); }
128     virtual SCEVHandle
129     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
130                                       const SCEVHandle &Conc,
131                                       ScalarEvolution &SE) const;
132
133     /// Methods for support type inquiry through isa, cast, and dyn_cast:
134     static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
135     static bool classof(const SCEV *S);
136   };
137
138   /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
139   /// freeing the objects when the last reference is dropped.
140   class SCEVHandle {
141     SCEV *S;
142     SCEVHandle();  // DO NOT IMPLEMENT
143   public:
144     SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
145       assert(S && "Cannot create a handle to a null SCEV!");
146       S->addRef();
147     }
148     SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
149       S->addRef();
150     }
151     ~SCEVHandle() { S->dropRef(); }
152
153     operator SCEV*() const { return S; }
154
155     SCEV &operator*() const { return *S; }
156     SCEV *operator->() const { return S; }
157
158     bool operator==(SCEV *RHS) const { return S == RHS; }
159     bool operator!=(SCEV *RHS) const { return S != RHS; }
160
161     const SCEVHandle &operator=(SCEV *RHS) {
162       if (S != RHS) {
163         S->dropRef();
164         S = RHS;
165         S->addRef();
166       }
167       return *this;
168     }
169
170     const SCEVHandle &operator=(const SCEVHandle &RHS) {
171       if (S != RHS.S) {
172         S->dropRef();
173         S = RHS.S;
174         S->addRef();
175       }
176       return *this;
177     }
178   };
179
180   template<typename From> struct simplify_type;
181   template<> struct simplify_type<const SCEVHandle> {
182     typedef SCEV* SimpleType;
183     static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
184       return Node;
185     }
186   };
187   template<> struct simplify_type<SCEVHandle>
188     : public simplify_type<const SCEVHandle> {};
189
190   /// ScalarEvolution - This class is the main scalar evolution driver.  Because
191   /// client code (intentionally) can't do much with the SCEV objects directly,
192   /// they must ask this class for services.
193   ///
194   class ScalarEvolution : public FunctionPass {
195     void *Impl;    // ScalarEvolution uses the pimpl pattern
196   public:
197     static char ID; // Pass identification, replacement for typeid
198     ScalarEvolution() : FunctionPass((intptr_t)&ID), Impl(0) {}
199
200     /// getSCEV - Return a SCEV expression handle for the full generality of the
201     /// specified expression.
202     SCEVHandle getSCEV(Value *V) const;
203
204     SCEVHandle getConstant(ConstantInt *V);
205     SCEVHandle getConstant(const APInt& Val);
206     SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
207     SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
208     SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
209     SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops);
210     SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
211       std::vector<SCEVHandle> Ops;
212       Ops.push_back(LHS);
213       Ops.push_back(RHS);
214       return getAddExpr(Ops);
215     }
216     SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
217                           const SCEVHandle &Op2) {
218       std::vector<SCEVHandle> Ops;
219       Ops.push_back(Op0);
220       Ops.push_back(Op1);
221       Ops.push_back(Op2);
222       return getAddExpr(Ops);
223     }
224     SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
225     SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
226       std::vector<SCEVHandle> Ops;
227       Ops.push_back(LHS);
228       Ops.push_back(RHS);
229       return getMulExpr(Ops);
230     }
231     SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
232     SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
233                              const Loop *L);
234     SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
235                              const Loop *L);
236     SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
237                              const Loop *L) {
238       std::vector<SCEVHandle> NewOp(Operands);
239       return getAddRecExpr(NewOp, L);
240     }
241     SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
242     SCEVHandle getSMaxExpr(std::vector<SCEVHandle> Operands);
243     SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
244     SCEVHandle getUMaxExpr(std::vector<SCEVHandle> Operands);
245     SCEVHandle getUnknown(Value *V);
246
247     /// getNegativeSCEV - Return the SCEV object corresponding to -V.
248     ///
249     SCEVHandle getNegativeSCEV(const SCEVHandle &V);
250
251     /// getNotSCEV - Return the SCEV object corresponding to ~V.
252     ///
253     SCEVHandle getNotSCEV(const SCEVHandle &V);
254
255     /// getMinusSCEV - Return LHS-RHS.
256     ///
257     SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
258                             const SCEVHandle &RHS);
259
260     /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
261     /// of the input value to the specified type.  If the type must be
262     /// extended, it is zero extended.
263     SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
264
265     /// getIntegerSCEV - Given an integer or FP type, create a constant for the
266     /// specified signed integer value and return a SCEV for the constant.
267     SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
268
269     /// hasSCEV - Return true if the SCEV for this value has already been
270     /// computed.
271     bool hasSCEV(Value *V) const;
272
273     /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
274     /// the specified value.
275     void setSCEV(Value *V, const SCEVHandle &H);
276
277     /// getSCEVAtScope - Return a SCEV expression handle for the specified value
278     /// at the specified scope in the program.  The L value specifies a loop
279     /// nest to evaluate the expression at, where null is the top-level or a
280     /// specified loop is immediately inside of the loop.
281     ///
282     /// This method can be used to compute the exit value for a variable defined
283     /// in a loop by querying what the value will hold in the parent loop.
284     ///
285     /// If this value is not computable at this scope, a SCEVCouldNotCompute
286     /// object is returned.
287     SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
288
289     /// getIterationCount - If the specified loop has a predictable iteration
290     /// count, return it, otherwise return a SCEVCouldNotCompute object.
291     SCEVHandle getIterationCount(const Loop *L) const;
292
293     /// hasLoopInvariantIterationCount - Return true if the specified loop has
294     /// an analyzable loop-invariant iteration count.
295     bool hasLoopInvariantIterationCount(const Loop *L) const;
296
297     /// deleteValueFromRecords - This method should be called by the
298     /// client before it removes a Value from the program, to make sure
299     /// that no dangling references are left around.
300     void deleteValueFromRecords(Value *V) const;
301
302     virtual bool runOnFunction(Function &F);
303     virtual void releaseMemory();
304     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
305     virtual void print(std::ostream &OS, const Module* = 0) const;
306     void print(std::ostream *OS, const Module* M = 0) const {
307       if (OS) print(*OS, M);
308     }
309   };
310 }
311
312 #endif