LLVM API Documentation
00001 //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // The ScalarEvolution class is an LLVM pass which can be used to analyze and 00011 // catagorize scalar expressions in loops. It specializes in recognizing 00012 // general induction variables, representing them with the abstract and opaque 00013 // SCEV class. Given this analysis, trip counts of loops and other important 00014 // properties can be obtained. 00015 // 00016 // This analysis is primarily useful for induction variable substitution and 00017 // strength reduction. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H 00022 #define LLVM_ANALYSIS_SCALAREVOLUTION_H 00023 00024 #include "llvm/Pass.h" 00025 #include "llvm/Analysis/LoopInfo.h" 00026 #include "llvm/Support/DataTypes.h" 00027 #include <iosfwd> 00028 00029 namespace llvm { 00030 class APInt; 00031 class ConstantInt; 00032 class Instruction; 00033 class Type; 00034 class ConstantRange; 00035 class SCEVHandle; 00036 class ScalarEvolution; 00037 00038 /// SCEV - This class represent an analyzed expression in the program. These 00039 /// are reference counted opaque objects that the client is not allowed to 00040 /// do much with directly. 00041 /// 00042 class SCEV { 00043 const unsigned SCEVType; // The SCEV baseclass this node corresponds to 00044 mutable unsigned RefCount; 00045 00046 friend class SCEVHandle; 00047 void addRef() const { ++RefCount; } 00048 void dropRef() const { 00049 if (--RefCount == 0) 00050 delete this; 00051 } 00052 00053 SCEV(const SCEV &); // DO NOT IMPLEMENT 00054 void operator=(const SCEV &); // DO NOT IMPLEMENT 00055 protected: 00056 virtual ~SCEV(); 00057 public: 00058 explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {} 00059 00060 unsigned getSCEVType() const { return SCEVType; } 00061 00062 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in 00063 /// the specified loop. 00064 virtual bool isLoopInvariant(const Loop *L) const = 0; 00065 00066 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a 00067 /// known way in the specified loop. This property being true implies that 00068 /// the value is variant in the loop AND that we can emit an expression to 00069 /// compute the value of the expression at any particular loop iteration. 00070 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0; 00071 00072 /// getType - Return the LLVM type of this SCEV expression. 00073 /// 00074 virtual const Type *getType() const = 0; 00075 00076 /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise. 00077 /// 00078 uint32_t getBitWidth() const; 00079 00080 /// isZero - Return true if the expression is a constant zero. 00081 /// 00082 bool isZero() const; 00083 00084 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references 00085 /// the symbolic value "Sym", construct and return a new SCEV that produces 00086 /// the same value, but which uses the concrete value Conc instead of the 00087 /// symbolic value. If this SCEV does not use the symbolic value, it 00088 /// returns itself. 00089 virtual SCEVHandle 00090 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, 00091 const SCEVHandle &Conc, 00092 ScalarEvolution &SE) const = 0; 00093 00094 /// print - Print out the internal representation of this scalar to the 00095 /// specified stream. This should really only be used for debugging 00096 /// purposes. 00097 virtual void print(std::ostream &OS) const = 0; 00098 void print(std::ostream *OS) const { if (OS) print(*OS); } 00099 00100 /// dump - This method is used for debugging. 00101 /// 00102 void dump() const; 00103 }; 00104 00105 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) { 00106 S.print(OS); 00107 return OS; 00108 } 00109 00110 /// SCEVCouldNotCompute - An object of this class is returned by queries that 00111 /// could not be answered. For example, if you ask for the number of 00112 /// iterations of a linked-list traversal loop, you will get one of these. 00113 /// None of the standard SCEV operations are valid on this class, it is just a 00114 /// marker. 00115 struct SCEVCouldNotCompute : public SCEV { 00116 SCEVCouldNotCompute(); 00117 00118 // None of these methods are valid for this object. 00119 virtual bool isLoopInvariant(const Loop *L) const; 00120 virtual const Type *getType() const; 00121 virtual bool hasComputableLoopEvolution(const Loop *L) const; 00122 virtual void print(std::ostream &OS) const; 00123 void print(std::ostream *OS) const { if (OS) print(*OS); } 00124 virtual SCEVHandle 00125 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, 00126 const SCEVHandle &Conc, 00127 ScalarEvolution &SE) const; 00128 00129 /// Methods for support type inquiry through isa, cast, and dyn_cast: 00130 static inline bool classof(const SCEVCouldNotCompute *S) { return true; } 00131 static bool classof(const SCEV *S); 00132 }; 00133 00134 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts, 00135 /// freeing the objects when the last reference is dropped. 00136 class SCEVHandle { 00137 SCEV *S; 00138 SCEVHandle(); // DO NOT IMPLEMENT 00139 public: 00140 SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) { 00141 assert(S && "Cannot create a handle to a null SCEV!"); 00142 S->addRef(); 00143 } 00144 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) { 00145 S->addRef(); 00146 } 00147 ~SCEVHandle() { S->dropRef(); } 00148 00149 operator SCEV*() const { return S; } 00150 00151 SCEV &operator*() const { return *S; } 00152 SCEV *operator->() const { return S; } 00153 00154 bool operator==(SCEV *RHS) const { return S == RHS; } 00155 bool operator!=(SCEV *RHS) const { return S != RHS; } 00156 00157 const SCEVHandle &operator=(SCEV *RHS) { 00158 if (S != RHS) { 00159 S->dropRef(); 00160 S = RHS; 00161 S->addRef(); 00162 } 00163 return *this; 00164 } 00165 00166 const SCEVHandle &operator=(const SCEVHandle &RHS) { 00167 if (S != RHS.S) { 00168 S->dropRef(); 00169 S = RHS.S; 00170 S->addRef(); 00171 } 00172 return *this; 00173 } 00174 }; 00175 00176 template<typename From> struct simplify_type; 00177 template<> struct simplify_type<const SCEVHandle> { 00178 typedef SCEV* SimpleType; 00179 static SimpleType getSimplifiedValue(const SCEVHandle &Node) { 00180 return Node; 00181 } 00182 }; 00183 template<> struct simplify_type<SCEVHandle> 00184 : public simplify_type<const SCEVHandle> {}; 00185 00186 /// ScalarEvolution - This class is the main scalar evolution driver. Because 00187 /// client code (intentionally) can't do much with the SCEV objects directly, 00188 /// they must ask this class for services. 00189 /// 00190 class ScalarEvolution : public FunctionPass { 00191 void *Impl; // ScalarEvolution uses the pimpl pattern 00192 public: 00193 static char ID; // Pass identification, replacement for typeid 00194 ScalarEvolution() : FunctionPass(&ID), Impl(0) {} 00195 00196 /// getSCEV - Return a SCEV expression handle for the full generality of the 00197 /// specified expression. 00198 SCEVHandle getSCEV(Value *V) const; 00199 00200 SCEVHandle getConstant(ConstantInt *V); 00201 SCEVHandle getConstant(const APInt& Val); 00202 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty); 00203 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty); 00204 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty); 00205 SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops); 00206 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) { 00207 std::vector<SCEVHandle> Ops; 00208 Ops.push_back(LHS); 00209 Ops.push_back(RHS); 00210 return getAddExpr(Ops); 00211 } 00212 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1, 00213 const SCEVHandle &Op2) { 00214 std::vector<SCEVHandle> Ops; 00215 Ops.push_back(Op0); 00216 Ops.push_back(Op1); 00217 Ops.push_back(Op2); 00218 return getAddExpr(Ops); 00219 } 00220 SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops); 00221 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) { 00222 std::vector<SCEVHandle> Ops; 00223 Ops.push_back(LHS); 00224 Ops.push_back(RHS); 00225 return getMulExpr(Ops); 00226 } 00227 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 00228 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step, 00229 const Loop *L); 00230 SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands, 00231 const Loop *L); 00232 SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands, 00233 const Loop *L) { 00234 std::vector<SCEVHandle> NewOp(Operands); 00235 return getAddRecExpr(NewOp, L); 00236 } 00237 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 00238 SCEVHandle getSMaxExpr(std::vector<SCEVHandle> Operands); 00239 SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 00240 SCEVHandle getUMaxExpr(std::vector<SCEVHandle> Operands); 00241 SCEVHandle getUnknown(Value *V); 00242 00243 /// getNegativeSCEV - Return the SCEV object corresponding to -V. 00244 /// 00245 SCEVHandle getNegativeSCEV(const SCEVHandle &V); 00246 00247 /// getNotSCEV - Return the SCEV object corresponding to ~V. 00248 /// 00249 SCEVHandle getNotSCEV(const SCEVHandle &V); 00250 00251 /// getMinusSCEV - Return LHS-RHS. 00252 /// 00253 SCEVHandle getMinusSCEV(const SCEVHandle &LHS, 00254 const SCEVHandle &RHS); 00255 00256 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion 00257 /// of the input value to the specified type. If the type must be 00258 /// extended, it is zero extended. 00259 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty); 00260 00261 /// getIntegerSCEV - Given an integer or FP type, create a constant for the 00262 /// specified signed integer value and return a SCEV for the constant. 00263 SCEVHandle getIntegerSCEV(int Val, const Type *Ty); 00264 00265 /// hasSCEV - Return true if the SCEV for this value has already been 00266 /// computed. 00267 bool hasSCEV(Value *V) const; 00268 00269 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for 00270 /// the specified value. 00271 void setSCEV(Value *V, const SCEVHandle &H); 00272 00273 /// getSCEVAtScope - Return a SCEV expression handle for the specified value 00274 /// at the specified scope in the program. The L value specifies a loop 00275 /// nest to evaluate the expression at, where null is the top-level or a 00276 /// specified loop is immediately inside of the loop. 00277 /// 00278 /// This method can be used to compute the exit value for a variable defined 00279 /// in a loop by querying what the value will hold in the parent loop. 00280 /// 00281 /// If this value is not computable at this scope, a SCEVCouldNotCompute 00282 /// object is returned. 00283 SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const; 00284 00285 /// getIterationCount - If the specified loop has a predictable iteration 00286 /// count, return it, otherwise return a SCEVCouldNotCompute object. 00287 SCEVHandle getIterationCount(const Loop *L) const; 00288 00289 /// hasLoopInvariantIterationCount - Return true if the specified loop has 00290 /// an analyzable loop-invariant iteration count. 00291 bool hasLoopInvariantIterationCount(const Loop *L) const; 00292 00293 /// deleteValueFromRecords - This method should be called by the 00294 /// client before it removes a Value from the program, to make sure 00295 /// that no dangling references are left around. 00296 void deleteValueFromRecords(Value *V) const; 00297 00298 virtual bool runOnFunction(Function &F); 00299 virtual void releaseMemory(); 00300 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 00301 virtual void print(std::ostream &OS, const Module* = 0) const; 00302 void print(std::ostream *OS, const Module* M = 0) const { 00303 if (OS) print(*OS, M); 00304 } 00305 }; 00306 } 00307 00308 #endif