9844940c613807be4706170cfbcb67ae7bc16fdb
[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     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     /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
78     /// the symbolic value "Sym", construct and return a new SCEV that produces
79     /// the same value, but which uses the concrete value Conc instead of the
80     /// symbolic value.  If this SCEV does not use the symbolic value, it
81     /// returns itself.
82     virtual SCEVHandle 
83     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
84                                       const SCEVHandle &Conc) const = 0;
85
86     /// print - Print out the internal representation of this scalar to the
87     /// specified stream.  This should really only be used for debugging
88     /// purposes.
89     virtual void print(std::ostream &OS) const = 0;
90
91     /// dump - This method is used for debugging.
92     ///
93     void dump() const;
94   };
95   
96   inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
97     S.print(OS);
98     return OS;
99   }
100
101   /// SCEVCouldNotCompute - An object of this class is returned by queries that
102   /// could not be answered.  For example, if you ask for the number of
103   /// iterations of a linked-list traversal loop, you will get one of these.
104   /// None of the standard SCEV operations are valid on this class, it is just a
105   /// marker.
106   struct SCEVCouldNotCompute : public SCEV {
107     SCEVCouldNotCompute();
108
109     // None of these methods are valid for this object.
110     virtual bool isLoopInvariant(const Loop *L) const;
111     virtual const Type *getType() const;
112     virtual bool hasComputableLoopEvolution(const Loop *L) const;
113     virtual void print(std::ostream &OS) const;
114     virtual SCEVHandle 
115     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
116                                       const SCEVHandle &Conc) const;
117
118     /// Methods for support type inquiry through isa, cast, and dyn_cast:
119     static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
120     static bool classof(const SCEV *S);
121   };
122
123   /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
124   /// freeing the objects when the last reference is dropped.
125   class SCEVHandle {
126     SCEV *S;
127     SCEVHandle();  // DO NOT IMPLEMENT
128   public:
129     SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
130       assert(S && "Cannot create a handle to a null SCEV!");
131       S->addRef();
132     }
133     SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
134       S->addRef();      
135     }
136     ~SCEVHandle() { S->dropRef(); }
137
138     operator SCEV*() const { return S; }
139
140     SCEV &operator*() const { return *S; }
141     SCEV *operator->() const { return S; }
142
143     bool operator==(SCEV *RHS) const { return S == RHS; }
144     bool operator!=(SCEV *RHS) const { return S != RHS; }
145
146     const SCEVHandle &operator=(SCEV *RHS) {
147       if (S != RHS) {
148         S->dropRef();
149         S = RHS;
150         S->addRef();
151       }
152       return *this;
153     }
154
155     const SCEVHandle &operator=(const SCEVHandle &RHS) {
156       if (S != RHS.S) {
157         S->dropRef();
158         S = RHS.S;
159         S->addRef();
160       }
161       return *this;
162     }
163   };
164
165   template<typename From> struct simplify_type;
166   template<> struct simplify_type<const SCEVHandle> {
167     typedef SCEV* SimpleType;
168     static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
169       return Node;
170     }
171   };
172   template<> struct simplify_type<SCEVHandle>
173     : public simplify_type<const SCEVHandle> {};
174
175   /// ScalarEvolution - This class is the main scalar evolution driver.  Because
176   /// client code (intentionally) can't do much with the SCEV objects directly,
177   /// they must ask this class for services.
178   ///
179   class ScalarEvolution : public FunctionPass {
180     void *Impl;    // ScalarEvolution uses the pimpl pattern
181   public:
182     ScalarEvolution() : Impl(0) {}
183     
184     /// getSCEV - Return a SCEV expression handle for the full generality of the
185     /// specified expression.
186     SCEVHandle getSCEV(Value *V) const;
187
188     /// getSCEVAtScope - Return a SCEV expression handle for the specified value
189     /// at the specified scope in the program.  The L value specifies a loop
190     /// nest to evaluate the expression at, where null is the top-level or a
191     /// specified loop is immediately inside of the loop.
192     ///
193     /// This method can be used to compute the exit value for a variable defined
194     /// in a loop by querying what the value will hold in the parent loop.
195     ///
196     /// If this value is not computable at this scope, a SCEVCouldNotCompute
197     /// object is returned.
198     SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
199
200     /// getIterationCount - If the specified loop has a predictable iteration
201     /// count, return it, otherwise return a SCEVCouldNotCompute object.
202     SCEVHandle getIterationCount(const Loop *L) const;
203
204     /// hasLoopInvariantIterationCount - Return true if the specified loop has
205     /// an analyzable loop-invariant iteration count.
206     bool hasLoopInvariantIterationCount(const Loop *L) const;
207
208     /// deleteInstructionFromRecords - This method should be called by the
209     /// client before it removes an instruction from the program, to make sure
210     /// that no dangling references are left around.
211     void deleteInstructionFromRecords(Instruction *I) const;
212
213     virtual bool runOnFunction(Function &F);
214     virtual void releaseMemory();
215     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
216     virtual void print(std::ostream &OS, const Module* = 0) const;
217   };
218 }
219
220 #endif