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