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"
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.
40 const unsigned SCEVType; // The SCEV baseclass this node corresponds to
41 mutable unsigned RefCount;
43 friend class SCEVHandle;
44 void addRef() const { ++RefCount; }
45 void dropRef() const {
50 SCEV(const SCEV &); // DO NOT IMPLEMENT
51 void operator=(const SCEV &); // DO NOT IMPLEMENT
55 SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
57 unsigned getSCEVType() const { return SCEVType; }
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;
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;
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;
73 /// getType - Return the LLVM type of this SCEV expression.
75 virtual const Type *getType() const = 0;
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
83 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
84 const SCEVHandle &Conc) const = 0;
86 /// print - Print out the internal representation of this scalar to the
87 /// specified stream. This should really only be used for debugging
89 virtual void print(std::ostream &OS) const = 0;
91 /// dump - This method is used for debugging.
96 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
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
106 struct SCEVCouldNotCompute : public SCEV {
107 SCEVCouldNotCompute();
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;
115 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
116 const SCEVHandle &Conc) const;
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);
123 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
124 /// freeing the objects when the last reference is dropped.
127 SCEVHandle(); // DO NOT IMPLEMENT
129 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
130 assert(S && "Cannot create a handle to a null SCEV!");
133 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
136 ~SCEVHandle() { S->dropRef(); }
138 operator SCEV*() const { return S; }
140 SCEV &operator*() const { return *S; }
141 SCEV *operator->() const { return S; }
143 bool operator==(SCEV *RHS) const { return S == RHS; }
144 bool operator!=(SCEV *RHS) const { return S != RHS; }
146 const SCEVHandle &operator=(SCEV *RHS) {
155 const SCEVHandle &operator=(const SCEVHandle &RHS) {
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) {
172 template<> struct simplify_type<SCEVHandle>
173 : public simplify_type<const SCEVHandle> {};
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.
179 class ScalarEvolution : public FunctionPass {
180 void *Impl; // ScalarEvolution uses the pimpl pattern
182 ScalarEvolution() : Impl(0) {}
184 /// getSCEV - Return a SCEV expression handle for the full generality of the
185 /// specified expression.
186 SCEVHandle getSCEV(Value *V) const;
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.
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.
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;
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;
204 /// hasLoopInvariantIterationCount - Return true if the specified loop has
205 /// an analyzable loop-invariant iteration count.
206 bool hasLoopInvariantIterationCount(const Loop *L) const;
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;
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;