Remove the SCEV::expandCodeFor method, add a new SCEVVisitor class.
[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     unsigned RefCount;
42
43     friend class SCEVHandle;
44     void addRef() { ++RefCount; }
45     void dropRef() {
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     unsigned getSCEVType() const { return SCEVType; }
58
59     /// getValueRange - Return the tightest constant bounds that this value is
60     /// known to have.  This method is only valid on integer SCEV objects.
61     virtual ConstantRange getValueRange() const;
62
63     /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
64     /// the specified loop.
65     virtual bool isLoopInvariant(const Loop *L) const = 0;
66
67     /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
68     /// known way in the specified loop.  This property being true implies that
69     /// the value is variant in the loop AND that we can emit an expression to
70     /// compute the value of the expression at any particular loop iteration.
71     virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
72
73     /// getType - Return the LLVM type of this SCEV expression.
74     ///
75     virtual const Type *getType() const = 0;
76
77     /// print - Print out the internal representation of this scalar to the
78     /// specified stream.  This should really only be used for debugging
79     /// purposes.
80     virtual void print(std::ostream &OS) const = 0;
81
82     /// dump - This method is used for debugging.
83     ///
84     void dump() const;
85   };
86   
87   inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
88     S.print(OS);
89     return OS;
90   }
91
92   /// SCEVCouldNotCompute - An object of this class is returned by queries that
93   /// could not be answered.  For example, if you ask for the number of
94   /// iterations of a linked-list traversal loop, you will get one of these.
95   /// None of the standard SCEV operations are valid on this class, it is just a
96   /// marker.
97   struct SCEVCouldNotCompute : public SCEV {
98     SCEVCouldNotCompute();
99
100     // None of these methods are valid for this object.
101     virtual bool isLoopInvariant(const Loop *L) const;
102     virtual const Type *getType() const;
103     virtual bool hasComputableLoopEvolution(const Loop *L) const;
104     virtual void print(std::ostream &OS) const;
105
106
107     /// Methods for support type inquiry through isa, cast, and dyn_cast:
108     static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
109     static bool classof(const SCEV *S);
110   };
111
112   /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
113   /// freeing the objects when the last reference is dropped.
114   class SCEVHandle {
115     SCEV *S;
116     SCEVHandle();  // DO NOT IMPLEMENT
117   public:
118     SCEVHandle(SCEV *s) : S(s) {
119       assert(S && "Cannot create a handle to a null SCEV!");
120       S->addRef();
121     }
122     SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
123       S->addRef();      
124     }
125     ~SCEVHandle() { S->dropRef(); }
126
127     operator SCEV*() const { return S; }
128
129     SCEV &operator*() const { return *S; }
130     SCEV *operator->() const { return S; }
131
132     bool operator==(SCEV *RHS) const { return S == RHS; }
133     bool operator!=(SCEV *RHS) const { return S != RHS; }
134
135     const SCEVHandle &operator=(SCEV *RHS) {
136       if (S != RHS) {
137         S->dropRef();
138         S = RHS;
139         S->addRef();
140       }
141       return *this;
142     }
143
144     const SCEVHandle &operator=(const SCEVHandle &RHS) {
145       if (S != RHS.S) {
146         S->dropRef();
147         S = RHS.S;
148         S->addRef();
149       }
150       return *this;
151     }
152   };
153
154   template<typename From> struct simplify_type;
155   template<> struct simplify_type<const SCEVHandle> {
156     typedef SCEV* SimpleType;
157     static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
158       return Node;
159     }
160   };
161   template<> struct simplify_type<SCEVHandle>
162     : public simplify_type<const SCEVHandle> {};
163
164   /// ScalarEvolution - This class is the main scalar evolution driver.  Because
165   /// client code (intentionally) can't do much with the SCEV objects directly,
166   /// they must ask this class for services.
167   ///
168   class ScalarEvolution : public FunctionPass {
169     void *Impl;    // ScalarEvolution uses the pimpl pattern
170   public:
171     ScalarEvolution() : Impl(0) {}
172     
173     /// getSCEV - Return a SCEV expression handle for the full generality of the
174     /// specified expression.
175     SCEVHandle getSCEV(Value *V) const;
176
177     /// getSCEVAtScope - Return a SCEV expression handle for the specified value
178     /// at the specified scope in the program.  The L value specifies a loop
179     /// nest to evaluate the expression at, where null is the top-level or a
180     /// specified loop is immediately inside of the loop.
181     ///
182     /// This method can be used to compute the exit value for a variable defined
183     /// in a loop by querying what the value will hold in the parent loop.
184     ///
185     /// If this value is not computable at this scope, a SCEVCouldNotCompute
186     /// object is returned.
187     SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
188
189     /// getIterationCount - If the specified loop has a predictable iteration
190     /// count, return it, otherwise return a SCEVCouldNotCompute object.
191     SCEVHandle getIterationCount(const Loop *L) const;
192
193     /// hasLoopInvariantIterationCount - Return true if the specified loop has
194     /// an analyzable loop-invariant iteration count.
195     bool hasLoopInvariantIterationCount(const Loop *L) const;
196
197     /// deleteInstructionFromRecords - This method should be called by the
198     /// client before it removes an instruction from the program, to make sure
199     /// that no dangling references are left around.
200     void deleteInstructionFromRecords(Instruction *I) const;
201
202     /// shouldSubstituteIndVar - Return true if we should perform induction
203     /// variable substitution for this variable.  This is a hack because we
204     /// don't have a strength reduction pass yet.  When we do we will promote
205     /// all vars, because we can strength reduce them later as desired.
206     bool shouldSubstituteIndVar(const SCEV *S) const;
207
208     virtual bool runOnFunction(Function &F);
209     virtual void releaseMemory();
210     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
211     virtual void print(std::ostream &OS) const;
212   };
213 }
214
215 #endif