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