1 //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
16 // This analysis is primarily useful for induction variable substitution and
17 // strength reduction.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
22 #define LLVM_ANALYSIS_SCALAREVOLUTION_H
24 #include "llvm/Pass.h"
25 #include "llvm/Support/DataTypes.h"
26 #include "llvm/Support/Streams.h"
37 /// SCEV - This class represent an analyzed expression in the program. These
38 /// are reference counted opaque objects that the client is not allowed to
39 /// do much with directly.
42 const unsigned SCEVType; // The SCEV baseclass this node corresponds to
43 mutable unsigned RefCount;
45 friend class SCEVHandle;
46 void addRef() const { ++RefCount; }
47 void dropRef() const {
52 SCEV(const SCEV &); // DO NOT IMPLEMENT
53 void operator=(const SCEV &); // DO NOT IMPLEMENT
57 explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
59 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
61 static SCEVHandle getNegativeSCEV(const SCEVHandle &V);
63 /// getMinusSCEV - Return LHS-RHS.
65 static SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
66 const SCEVHandle &RHS);
69 unsigned getSCEVType() const { return SCEVType; }
71 /// getValueRange - Return the tightest constant bounds that this value is
72 /// known to have. This method is only valid on integer SCEV objects.
73 virtual ConstantRange getValueRange() const;
75 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
76 /// the specified loop.
77 virtual bool isLoopInvariant(const Loop *L) const = 0;
79 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
80 /// known way in the specified loop. This property being true implies that
81 /// the value is variant in the loop AND that we can emit an expression to
82 /// compute the value of the expression at any particular loop iteration.
83 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
85 /// getType - Return the LLVM type of this SCEV expression.
87 virtual const Type *getType() const = 0;
89 /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
91 uint32_t getBitWidth() const;
93 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
94 /// the symbolic value "Sym", construct and return a new SCEV that produces
95 /// the same value, but which uses the concrete value Conc instead of the
96 /// symbolic value. If this SCEV does not use the symbolic value, it
99 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
100 const SCEVHandle &Conc) const = 0;
102 /// print - Print out the internal representation of this scalar to the
103 /// specified stream. This should really only be used for debugging
105 virtual void print(std::ostream &OS) const = 0;
106 void print(std::ostream *OS) const { if (OS) print(*OS); }
108 /// dump - This method is used for debugging.
113 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
118 /// SCEVCouldNotCompute - An object of this class is returned by queries that
119 /// could not be answered. For example, if you ask for the number of
120 /// iterations of a linked-list traversal loop, you will get one of these.
121 /// None of the standard SCEV operations are valid on this class, it is just a
123 struct SCEVCouldNotCompute : public SCEV {
124 SCEVCouldNotCompute();
126 // None of these methods are valid for this object.
127 virtual bool isLoopInvariant(const Loop *L) const;
128 virtual const Type *getType() const;
129 virtual bool hasComputableLoopEvolution(const Loop *L) const;
130 virtual void print(std::ostream &OS) const;
131 void print(std::ostream *OS) const { if (OS) print(*OS); }
133 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
134 const SCEVHandle &Conc) const;
136 /// Methods for support type inquiry through isa, cast, and dyn_cast:
137 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
138 static bool classof(const SCEV *S);
141 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
142 /// freeing the objects when the last reference is dropped.
145 SCEVHandle(); // DO NOT IMPLEMENT
147 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
148 assert(S && "Cannot create a handle to a null SCEV!");
151 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
154 ~SCEVHandle() { S->dropRef(); }
156 operator SCEV*() const { return S; }
158 SCEV &operator*() const { return *S; }
159 SCEV *operator->() const { return S; }
161 bool operator==(SCEV *RHS) const { return S == RHS; }
162 bool operator!=(SCEV *RHS) const { return S != RHS; }
164 const SCEVHandle &operator=(SCEV *RHS) {
173 const SCEVHandle &operator=(const SCEVHandle &RHS) {
183 template<typename From> struct simplify_type;
184 template<> struct simplify_type<const SCEVHandle> {
185 typedef SCEV* SimpleType;
186 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
190 template<> struct simplify_type<SCEVHandle>
191 : public simplify_type<const SCEVHandle> {};
193 /// ScalarEvolution - This class is the main scalar evolution driver. Because
194 /// client code (intentionally) can't do much with the SCEV objects directly,
195 /// they must ask this class for services.
197 class ScalarEvolution : public FunctionPass {
198 void *Impl; // ScalarEvolution uses the pimpl pattern
200 static char ID; // Pass identifcation, replacement for typeid
201 ScalarEvolution() : FunctionPass((intptr_t)&ID), Impl(0) {}
203 /// getSCEV - Return a SCEV expression handle for the full generality of the
204 /// specified expression.
205 SCEVHandle getSCEV(Value *V) const;
207 /// hasSCEV - Return true if the SCEV for this value has already been
209 bool hasSCEV(Value *V) const;
211 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
212 /// the specified value.
213 void setSCEV(Value *V, const SCEVHandle &H);
215 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
216 /// at the specified scope in the program. The L value specifies a loop
217 /// nest to evaluate the expression at, where null is the top-level or a
218 /// specified loop is immediately inside of the loop.
220 /// This method can be used to compute the exit value for a variable defined
221 /// in a loop by querying what the value will hold in the parent loop.
223 /// If this value is not computable at this scope, a SCEVCouldNotCompute
224 /// object is returned.
225 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
227 /// getIterationCount - If the specified loop has a predictable iteration
228 /// count, return it, otherwise return a SCEVCouldNotCompute object.
229 SCEVHandle getIterationCount(const Loop *L) const;
231 /// hasLoopInvariantIterationCount - Return true if the specified loop has
232 /// an analyzable loop-invariant iteration count.
233 bool hasLoopInvariantIterationCount(const Loop *L) const;
235 /// deleteInstructionFromRecords - This method should be called by the
236 /// client before it removes an instruction from the program, to make sure
237 /// that no dangling references are left around.
238 void deleteInstructionFromRecords(Instruction *I) const;
240 virtual bool runOnFunction(Function &F);
241 virtual void releaseMemory();
242 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
243 virtual void print(std::ostream &OS, const Module* = 0) const;
244 void print(std::ostream *OS, const Module* M = 0) const {
245 if (OS) print(*OS, M);