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
43 friend class SCEVHandle;
44 void addRef() { ++RefCount; }
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 /// print - Print out the internal representation of this scalar to the
78 /// specified stream. This should really only be used for debugging
80 virtual void print(std::ostream &OS) const = 0;
82 /// dump - This method is used for debugging.
87 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
92 /// SCEVCouldNotCompute - An object of this class is returned by queries that
93 /// could not be answered. For example, if you ask for the number of
94 /// iterations of a linked-list traversal loop, you will get one of these.
95 /// None of the standard SCEV operations are valid on this class, it is just a
97 struct SCEVCouldNotCompute : public SCEV {
98 SCEVCouldNotCompute();
100 // None of these methods are valid for this object.
101 virtual bool isLoopInvariant(const Loop *L) const;
102 virtual const Type *getType() const;
103 virtual bool hasComputableLoopEvolution(const Loop *L) const;
104 virtual void print(std::ostream &OS) const;
107 /// Methods for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
109 static bool classof(const SCEV *S);
112 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
113 /// freeing the objects when the last reference is dropped.
116 SCEVHandle(); // DO NOT IMPLEMENT
118 SCEVHandle(SCEV *s) : S(s) {
119 assert(S && "Cannot create a handle to a null SCEV!");
122 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
125 ~SCEVHandle() { S->dropRef(); }
127 operator SCEV*() const { return S; }
129 SCEV &operator*() const { return *S; }
130 SCEV *operator->() const { return S; }
132 bool operator==(SCEV *RHS) const { return S == RHS; }
133 bool operator!=(SCEV *RHS) const { return S != RHS; }
135 const SCEVHandle &operator=(SCEV *RHS) {
144 const SCEVHandle &operator=(const SCEVHandle &RHS) {
154 template<typename From> struct simplify_type;
155 template<> struct simplify_type<const SCEVHandle> {
156 typedef SCEV* SimpleType;
157 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
161 template<> struct simplify_type<SCEVHandle>
162 : public simplify_type<const SCEVHandle> {};
164 /// ScalarEvolution - This class is the main scalar evolution driver. Because
165 /// client code (intentionally) can't do much with the SCEV objects directly,
166 /// they must ask this class for services.
168 class ScalarEvolution : public FunctionPass {
169 void *Impl; // ScalarEvolution uses the pimpl pattern
171 ScalarEvolution() : Impl(0) {}
173 /// getSCEV - Return a SCEV expression handle for the full generality of the
174 /// specified expression.
175 SCEVHandle getSCEV(Value *V) const;
177 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
178 /// at the specified scope in the program. The L value specifies a loop
179 /// nest to evaluate the expression at, where null is the top-level or a
180 /// specified loop is immediately inside of the loop.
182 /// This method can be used to compute the exit value for a variable defined
183 /// in a loop by querying what the value will hold in the parent loop.
185 /// If this value is not computable at this scope, a SCEVCouldNotCompute
186 /// object is returned.
187 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
189 /// getIterationCount - If the specified loop has a predictable iteration
190 /// count, return it, otherwise return a SCEVCouldNotCompute object.
191 SCEVHandle getIterationCount(const Loop *L) const;
193 /// hasLoopInvariantIterationCount - Return true if the specified loop has
194 /// an analyzable loop-invariant iteration count.
195 bool hasLoopInvariantIterationCount(const Loop *L) const;
197 /// deleteInstructionFromRecords - This method should be called by the
198 /// client before it removes an instruction from the program, to make sure
199 /// that no dangling references are left around.
200 void deleteInstructionFromRecords(Instruction *I) const;
202 virtual bool runOnFunction(Function &F);
203 virtual void releaseMemory();
204 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
205 virtual void print(std::ostream &OS, const Module* = 0) const;