Rename ScalarEvolution::deleteInstructionFromRecords to
[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 Instruction;
31   class Type;
32   class ConstantRange;
33   class Loop;
34   class LoopInfo;
35   class SCEVHandle;
36
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.
40   ///
41   class SCEV {
42     const unsigned SCEVType;      // The SCEV baseclass this node corresponds to
43     mutable unsigned RefCount;
44
45     friend class SCEVHandle;
46     void addRef() const { ++RefCount; }
47     void dropRef() const {
48       if (--RefCount == 0)
49         delete this;
50     }
51
52     SCEV(const SCEV &);            // DO NOT IMPLEMENT
53     void operator=(const SCEV &);  // DO NOT IMPLEMENT
54   protected:
55     virtual ~SCEV();
56   public:
57     explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
58
59     /// getNegativeSCEV - Return the SCEV object corresponding to -V.
60     ///
61     static SCEVHandle getNegativeSCEV(const SCEVHandle &V);
62
63     /// getMinusSCEV - Return LHS-RHS.
64     ///
65     static SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
66                                    const SCEVHandle &RHS);
67
68
69     unsigned getSCEVType() const { return SCEVType; }
70
71     /// getValueRange - Return the tightest constant bounds that this value is
72     /// known to have.  This method is only valid on integer SCEV objects.
73     virtual ConstantRange getValueRange() const;
74
75     /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
76     /// the specified loop.
77     virtual bool isLoopInvariant(const Loop *L) const = 0;
78
79     /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
80     /// known way in the specified loop.  This property being true implies that
81     /// the value is variant in the loop AND that we can emit an expression to
82     /// compute the value of the expression at any particular loop iteration.
83     virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
84
85     /// getType - Return the LLVM type of this SCEV expression.
86     ///
87     virtual const Type *getType() const = 0;
88
89     /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
90     /// 
91     uint32_t getBitWidth() const;
92
93     /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
94     /// the symbolic value "Sym", construct and return a new SCEV that produces
95     /// the same value, but which uses the concrete value Conc instead of the
96     /// symbolic value.  If this SCEV does not use the symbolic value, it
97     /// returns itself.
98     virtual SCEVHandle
99     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
100                                       const SCEVHandle &Conc) const = 0;
101
102     /// print - Print out the internal representation of this scalar to the
103     /// specified stream.  This should really only be used for debugging
104     /// purposes.
105     virtual void print(std::ostream &OS) const = 0;
106     void print(std::ostream *OS) const { if (OS) print(*OS); }
107
108     /// dump - This method is used for debugging.
109     ///
110     void dump() const;
111   };
112
113   inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
114     S.print(OS);
115     return OS;
116   }
117
118   /// SCEVCouldNotCompute - An object of this class is returned by queries that
119   /// could not be answered.  For example, if you ask for the number of
120   /// iterations of a linked-list traversal loop, you will get one of these.
121   /// None of the standard SCEV operations are valid on this class, it is just a
122   /// marker.
123   struct SCEVCouldNotCompute : public SCEV {
124     SCEVCouldNotCompute();
125
126     // None of these methods are valid for this object.
127     virtual bool isLoopInvariant(const Loop *L) const;
128     virtual const Type *getType() const;
129     virtual bool hasComputableLoopEvolution(const Loop *L) const;
130     virtual void print(std::ostream &OS) const;
131     void print(std::ostream *OS) const { if (OS) print(*OS); }
132     virtual SCEVHandle
133     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
134                                       const SCEVHandle &Conc) const;
135
136     /// Methods for support type inquiry through isa, cast, and dyn_cast:
137     static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
138     static bool classof(const SCEV *S);
139   };
140
141   /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
142   /// freeing the objects when the last reference is dropped.
143   class SCEVHandle {
144     SCEV *S;
145     SCEVHandle();  // DO NOT IMPLEMENT
146   public:
147     SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
148       assert(S && "Cannot create a handle to a null SCEV!");
149       S->addRef();
150     }
151     SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
152       S->addRef();
153     }
154     ~SCEVHandle() { S->dropRef(); }
155
156     operator SCEV*() const { return S; }
157
158     SCEV &operator*() const { return *S; }
159     SCEV *operator->() const { return S; }
160
161     bool operator==(SCEV *RHS) const { return S == RHS; }
162     bool operator!=(SCEV *RHS) const { return S != RHS; }
163
164     const SCEVHandle &operator=(SCEV *RHS) {
165       if (S != RHS) {
166         S->dropRef();
167         S = RHS;
168         S->addRef();
169       }
170       return *this;
171     }
172
173     const SCEVHandle &operator=(const SCEVHandle &RHS) {
174       if (S != RHS.S) {
175         S->dropRef();
176         S = RHS.S;
177         S->addRef();
178       }
179       return *this;
180     }
181   };
182
183   template<typename From> struct simplify_type;
184   template<> struct simplify_type<const SCEVHandle> {
185     typedef SCEV* SimpleType;
186     static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
187       return Node;
188     }
189   };
190   template<> struct simplify_type<SCEVHandle>
191     : public simplify_type<const SCEVHandle> {};
192
193   /// ScalarEvolution - This class is the main scalar evolution driver.  Because
194   /// client code (intentionally) can't do much with the SCEV objects directly,
195   /// they must ask this class for services.
196   ///
197   class ScalarEvolution : public FunctionPass {
198     void *Impl;    // ScalarEvolution uses the pimpl pattern
199   public:
200     static char ID; // Pass identification, replacement for typeid
201     ScalarEvolution() : FunctionPass((intptr_t)&ID), Impl(0) {}
202
203     /// getSCEV - Return a SCEV expression handle for the full generality of the
204     /// specified expression.
205     SCEVHandle getSCEV(Value *V) const;
206
207     /// hasSCEV - Return true if the SCEV for this value has already been
208     /// computed.
209     bool hasSCEV(Value *V) const;
210
211     /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
212     /// the specified value.
213     void setSCEV(Value *V, const SCEVHandle &H);
214
215     /// getSCEVAtScope - Return a SCEV expression handle for the specified value
216     /// at the specified scope in the program.  The L value specifies a loop
217     /// nest to evaluate the expression at, where null is the top-level or a
218     /// specified loop is immediately inside of the loop.
219     ///
220     /// This method can be used to compute the exit value for a variable defined
221     /// in a loop by querying what the value will hold in the parent loop.
222     ///
223     /// If this value is not computable at this scope, a SCEVCouldNotCompute
224     /// object is returned.
225     SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
226
227     /// getIterationCount - If the specified loop has a predictable iteration
228     /// count, return it, otherwise return a SCEVCouldNotCompute object.
229     SCEVHandle getIterationCount(const Loop *L) const;
230
231     /// hasLoopInvariantIterationCount - Return true if the specified loop has
232     /// an analyzable loop-invariant iteration count.
233     bool hasLoopInvariantIterationCount(const Loop *L) const;
234
235     /// deleteValueFromRecords - This method should be called by the
236     /// client before it removes a Value from the program, to make sure
237     /// that no dangling references are left around.
238     void deleteValueFromRecords(Value *V) const;
239
240     virtual bool runOnFunction(Function &F);
241     virtual void releaseMemory();
242     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
243     virtual void print(std::ostream &OS, const Module* = 0) const;
244     void print(std::ostream *OS, const Module* M = 0) const {
245       if (OS) print(*OS, M);
246     }
247   };
248 }
249
250 #endif