Use a sign-extend instead of a zero-extend when promoting a
[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 is distributed under the University of Illinois Open Source
6 // 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/Analysis/LoopInfo.h"
26 #include "llvm/Support/DataTypes.h"
27 #include <iosfwd>
28
29 namespace llvm {
30   class APInt;
31   class ConstantInt;
32   class Type;
33   class SCEVHandle;
34   class ScalarEvolution;
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     explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
57
58     unsigned getSCEVType() const { return SCEVType; }
59
60     /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
61     /// the specified loop.
62     virtual bool isLoopInvariant(const Loop *L) const = 0;
63
64     /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
65     /// known way in the specified loop.  This property being true implies that
66     /// the value is variant in the loop AND that we can emit an expression to
67     /// compute the value of the expression at any particular loop iteration.
68     virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
69
70     /// getType - Return the LLVM type of this SCEV expression.
71     ///
72     virtual const Type *getType() const = 0;
73
74     /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
75     /// 
76     uint32_t getBitWidth() const;
77
78     /// isZero - Return true if the expression is a constant zero.
79     ///
80     bool isZero() const;
81
82     /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
83     /// the symbolic value "Sym", construct and return a new SCEV that produces
84     /// the same value, but which uses the concrete value Conc instead of the
85     /// symbolic value.  If this SCEV does not use the symbolic value, it
86     /// returns itself.
87     virtual SCEVHandle
88     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
89                                       const SCEVHandle &Conc,
90                                       ScalarEvolution &SE) const = 0;
91
92     /// dominates - Return true if elements that makes up this SCEV dominates
93     /// the specified basic block.
94     virtual bool dominates(BasicBlock *BB, DominatorTree *DT) 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     void print(std::ostream *OS) const { if (OS) print(*OS); }
101
102     /// dump - This method is used for debugging.
103     ///
104     void dump() const;
105   };
106
107   inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
108     S.print(OS);
109     return OS;
110   }
111
112   /// SCEVCouldNotCompute - An object of this class is returned by queries that
113   /// could not be answered.  For example, if you ask for the number of
114   /// iterations of a linked-list traversal loop, you will get one of these.
115   /// None of the standard SCEV operations are valid on this class, it is just a
116   /// marker.
117   struct SCEVCouldNotCompute : public SCEV {
118     SCEVCouldNotCompute();
119
120     // None of these methods are valid for this object.
121     virtual bool isLoopInvariant(const Loop *L) const;
122     virtual const Type *getType() const;
123     virtual bool hasComputableLoopEvolution(const Loop *L) const;
124     virtual void print(std::ostream &OS) const;
125     void print(std::ostream *OS) const { if (OS) print(*OS); }
126     virtual SCEVHandle
127     replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
128                                       const SCEVHandle &Conc,
129                                       ScalarEvolution &SE) const;
130
131     virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
132       return true;
133     }
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     static char ID; // Pass identification, replacement for typeid
200     ScalarEvolution() : FunctionPass(&ID), Impl(0) {}
201
202     /// getSCEV - Return a SCEV expression handle for the full generality of the
203     /// specified expression.
204     SCEVHandle getSCEV(Value *V) const;
205
206     SCEVHandle getConstant(ConstantInt *V);
207     SCEVHandle getConstant(const APInt& Val);
208     SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
209     SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
210     SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
211     SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops);
212     SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
213       std::vector<SCEVHandle> Ops;
214       Ops.push_back(LHS);
215       Ops.push_back(RHS);
216       return getAddExpr(Ops);
217     }
218     SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
219                           const SCEVHandle &Op2) {
220       std::vector<SCEVHandle> Ops;
221       Ops.push_back(Op0);
222       Ops.push_back(Op1);
223       Ops.push_back(Op2);
224       return getAddExpr(Ops);
225     }
226     SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
227     SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
228       std::vector<SCEVHandle> Ops;
229       Ops.push_back(LHS);
230       Ops.push_back(RHS);
231       return getMulExpr(Ops);
232     }
233     SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
234     SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
235                              const Loop *L);
236     SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
237                              const Loop *L);
238     SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
239                              const Loop *L) {
240       std::vector<SCEVHandle> NewOp(Operands);
241       return getAddRecExpr(NewOp, L);
242     }
243     SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
244     SCEVHandle getSMaxExpr(std::vector<SCEVHandle> Operands);
245     SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
246     SCEVHandle getUMaxExpr(std::vector<SCEVHandle> Operands);
247     SCEVHandle getUnknown(Value *V);
248
249     /// getNegativeSCEV - Return the SCEV object corresponding to -V.
250     ///
251     SCEVHandle getNegativeSCEV(const SCEVHandle &V);
252
253     /// getNotSCEV - Return the SCEV object corresponding to ~V.
254     ///
255     SCEVHandle getNotSCEV(const SCEVHandle &V);
256
257     /// getMinusSCEV - Return LHS-RHS.
258     ///
259     SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
260                             const SCEVHandle &RHS);
261
262     /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
263     /// of the input value to the specified type.  If the type must be
264     /// extended, it is zero extended.
265     SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
266
267     /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
268     /// of the input value to the specified type.  If the type must be
269     /// extended, it is sign extended.
270     SCEVHandle getTruncateOrSignExtend(const SCEVHandle &V, const Type *Ty);
271
272     /// getIntegerSCEV - Given an integer or FP type, create a constant for the
273     /// specified signed integer value and return a SCEV for the constant.
274     SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
275
276     /// hasSCEV - Return true if the SCEV for this value has already been
277     /// computed.
278     bool hasSCEV(Value *V) const;
279
280     /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
281     /// the specified value.
282     void setSCEV(Value *V, const SCEVHandle &H);
283
284     /// getSCEVAtScope - Return a SCEV expression handle for the specified value
285     /// at the specified scope in the program.  The L value specifies a loop
286     /// nest to evaluate the expression at, where null is the top-level or a
287     /// specified loop is immediately inside of the loop.
288     ///
289     /// This method can be used to compute the exit value for a variable defined
290     /// in a loop by querying what the value will hold in the parent loop.
291     ///
292     /// If this value is not computable at this scope, a SCEVCouldNotCompute
293     /// object is returned.
294     SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
295
296     /// isLoopGuardedByCond - Test whether entry to the loop is protected by
297     /// a conditional between LHS and RHS.
298     bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
299                              SCEV *LHS, SCEV *RHS);
300
301     /// getIterationCount - If the specified loop has a predictable iteration
302     /// count, return it, otherwise return a SCEVCouldNotCompute object.
303     SCEVHandle getIterationCount(const Loop *L) const;
304
305     /// hasLoopInvariantIterationCount - Return true if the specified loop has
306     /// an analyzable loop-invariant iteration count.
307     bool hasLoopInvariantIterationCount(const Loop *L) const;
308
309     /// forgetLoopIterationCount - This method should be called by the
310     /// client when it has changed a loop in a way that may effect
311     /// ScalarEvolution's ability to compute a trip count.
312     void forgetLoopIterationCount(const Loop *L);
313
314     /// deleteValueFromRecords - This method should be called by the
315     /// client before it removes a Value from the program, to make sure
316     /// that no dangling references are left around.
317     void deleteValueFromRecords(Value *V) const;
318
319     virtual bool runOnFunction(Function &F);
320     virtual void releaseMemory();
321     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
322     virtual void print(std::ostream &OS, const Module* = 0) const;
323     void print(std::ostream *OS, const Module* M = 0) const {
324       if (OS) print(*OS, M);
325     }
326   };
327 }
328
329 #endif