ScalarEvolution.h revision 859b4824eeb2d88c441e855afe3dd7827dfd62a4
1a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)//===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===// 2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// The LLVM Compiler Infrastructure 4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 5a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// License. See LICENSE.TXT for details. 7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)//===----------------------------------------------------------------------===// 9a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 10a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// The ScalarEvolution class is an LLVM pass which can be used to analyze and 11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// catagorize scalar expressions in loops. It specializes in recognizing 12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// general induction variables, representing them with the abstract and opaque 13a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// SCEV class. Given this analysis, trip counts of loops and other important 14a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// properties can be obtained. 15a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 16a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// This analysis is primarily useful for induction variable substitution and 17a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// strength reduction. 18a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 19a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)//===----------------------------------------------------------------------===// 20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H 22a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#define LLVM_ANALYSIS_SCALAREVOLUTION_H 23a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "llvm/Pass.h" 25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "llvm/Analysis/LoopInfo.h" 26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "llvm/Support/DataTypes.h" 27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "llvm/Support/ValueHandle.h" 28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include <iosfwd> 29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace llvm { 31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) class APInt; 32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) class ConstantInt; 33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) class Type; 34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) class SCEVHandle; 35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) class ScalarEvolution; 36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) class TargetData; 37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// SCEV - This class represent an analyzed expression in the program. These 39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// are reference counted opaque objects that the client is not allowed to 40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// do much with directly. 41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// 42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) class SCEV { 43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) const unsigned SCEVType; // The SCEV baseclass this node corresponds to 44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) mutable unsigned RefCount; 45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) friend class SCEVHandle; 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void addRef() const { ++RefCount; } 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void dropRef() const { 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (--RefCount == 0) 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) delete this; 51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) } 52a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) SCEV(const SCEV &); // DO NOT IMPLEMENT 54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) void operator=(const SCEV &); // DO NOT IMPLEMENT 55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) protected: 56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual ~SCEV(); 57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) public: 58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {} 59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) unsigned getSCEVType() const { return SCEVType; } 61a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// isLoopInvariant - Return true if the value of this SCEV is unchanging in 63a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// the specified loop. 64a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual bool isLoopInvariant(const Loop *L) const = 0; 65a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 66a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// hasComputableLoopEvolution - Return true if this SCEV changes value in a 67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// known way in the specified loop. This property being true implies that 68a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// the value is variant in the loop AND that we can emit an expression to 69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// compute the value of the expression at any particular loop iteration. 70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual bool hasComputableLoopEvolution(const Loop *L) const = 0; 71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// getType - Return the LLVM type of this SCEV expression. 73a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// 74a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual const Type *getType() const = 0; 75a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// isZero - Return true if the expression is a constant zero. 77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// 78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) bool isZero() const; 79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// isOne - Return true if the expression is a constant one. 81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// 82a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) bool isOne() const; 83a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// replaceSymbolicValuesWithConcrete - If this SCEV internally references 85a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// the symbolic value "Sym", construct and return a new SCEV that produces 86a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// the same value, but which uses the concrete value Conc instead of the 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// symbolic value. If this SCEV does not use the symbolic value, it 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// returns itself. 8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) virtual SCEVHandle 9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, 91a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) const SCEVHandle &Conc, 92a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) ScalarEvolution &SE) const = 0; 93a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 94a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// dominates - Return true if elements that makes up this SCEV dominates 95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// the specified basic block. 96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0; 97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 98a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// print - Print out the internal representation of this scalar to the 99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// specified stream. This should really only be used for debugging 100a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) /// purposes. 101 virtual void print(raw_ostream &OS) const = 0; 102 void print(std::ostream &OS) const; 103 void print(std::ostream *OS) const { if (OS) print(*OS); } 104 105 /// dump - This method is used for debugging. 106 /// 107 void dump() const; 108 }; 109 110 inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) { 111 S.print(OS); 112 return OS; 113 } 114 115 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) { 116 S.print(OS); 117 return OS; 118 } 119 120 /// SCEVCouldNotCompute - An object of this class is returned by queries that 121 /// could not be answered. For example, if you ask for the number of 122 /// iterations of a linked-list traversal loop, you will get one of these. 123 /// None of the standard SCEV operations are valid on this class, it is just a 124 /// marker. 125 struct SCEVCouldNotCompute : public SCEV { 126 SCEVCouldNotCompute(); 127 ~SCEVCouldNotCompute(); 128 129 // None of these methods are valid for this object. 130 virtual bool isLoopInvariant(const Loop *L) const; 131 virtual const Type *getType() const; 132 virtual bool hasComputableLoopEvolution(const Loop *L) const; 133 virtual void print(raw_ostream &OS) const; 134 virtual SCEVHandle 135 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, 136 const SCEVHandle &Conc, 137 ScalarEvolution &SE) const; 138 139 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const { 140 return true; 141 } 142 143 /// Methods for support type inquiry through isa, cast, and dyn_cast: 144 static inline bool classof(const SCEVCouldNotCompute *S) { return true; } 145 static bool classof(const SCEV *S); 146 }; 147 148 /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be 149 /// notified whenever a Value is deleted. 150 class SCEVCallbackVH : public CallbackVH { 151 ScalarEvolution *SE; 152 virtual void deleted(); 153 virtual void allUsesReplacedWith(Value *New); 154 public: 155 SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0); 156 }; 157 158 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts, 159 /// freeing the objects when the last reference is dropped. 160 class SCEVHandle { 161 const SCEV *S; 162 SCEVHandle(); // DO NOT IMPLEMENT 163 public: 164 SCEVHandle(const SCEV *s) : S(s) { 165 assert(S && "Cannot create a handle to a null SCEV!"); 166 S->addRef(); 167 } 168 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) { 169 S->addRef(); 170 } 171 ~SCEVHandle() { S->dropRef(); } 172 173 operator const SCEV*() const { return S; } 174 175 const SCEV &operator*() const { return *S; } 176 const SCEV *operator->() const { return S; } 177 178 bool operator==(const SCEV *RHS) const { return S == RHS; } 179 bool operator!=(const SCEV *RHS) const { return S != RHS; } 180 181 const SCEVHandle &operator=(SCEV *RHS) { 182 if (S != RHS) { 183 S->dropRef(); 184 S = RHS; 185 S->addRef(); 186 } 187 return *this; 188 } 189 190 const SCEVHandle &operator=(const SCEVHandle &RHS) { 191 if (S != RHS.S) { 192 S->dropRef(); 193 S = RHS.S; 194 S->addRef(); 195 } 196 return *this; 197 } 198 }; 199 200 template<typename From> struct simplify_type; 201 template<> struct simplify_type<const SCEVHandle> { 202 typedef const SCEV* SimpleType; 203 static SimpleType getSimplifiedValue(const SCEVHandle &Node) { 204 return Node; 205 } 206 }; 207 template<> struct simplify_type<SCEVHandle> 208 : public simplify_type<const SCEVHandle> {}; 209 210 /// ScalarEvolution - This class is the main scalar evolution driver. Because 211 /// client code (intentionally) can't do much with the SCEV objects directly, 212 /// they must ask this class for services. 213 /// 214 class ScalarEvolution : public FunctionPass { 215 friend class SCEVCallbackVH; 216 217 /// F - The function we are analyzing. 218 /// 219 Function *F; 220 221 /// LI - The loop information for the function we are currently analyzing. 222 /// 223 LoopInfo *LI; 224 225 /// TD - The target data information for the target we are targetting. 226 /// 227 TargetData *TD; 228 229 /// UnknownValue - This SCEV is used to represent unknown trip counts and 230 /// things. 231 SCEVHandle UnknownValue; 232 233 /// Scalars - This is a cache of the scalars we have analyzed so far. 234 /// 235 std::map<SCEVCallbackVH, SCEVHandle> Scalars; 236 237 /// BackedgeTakenInfo - Information about the backedge-taken count 238 /// of a loop. This currently inclues an exact count and a maximum count. 239 /// 240 struct BackedgeTakenInfo { 241 /// Exact - An expression indicating the exact backedge-taken count of 242 /// the loop if it is known, or a SCEVCouldNotCompute otherwise. 243 SCEVHandle Exact; 244 245 /// Exact - An expression indicating the least maximum backedge-taken 246 /// count of the loop that is known, or a SCEVCouldNotCompute. 247 SCEVHandle Max; 248 249 /*implicit*/ BackedgeTakenInfo(SCEVHandle exact) : 250 Exact(exact), Max(exact) {} 251 252 /*implicit*/ BackedgeTakenInfo(const SCEV *exact) : 253 Exact(exact), Max(exact) {} 254 255 BackedgeTakenInfo(SCEVHandle exact, SCEVHandle max) : 256 Exact(exact), Max(max) {} 257 258 /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any 259 /// computed information, or whether it's all SCEVCouldNotCompute 260 /// values. 261 bool hasAnyInfo() const { 262 return !isa<SCEVCouldNotCompute>(Exact) || 263 !isa<SCEVCouldNotCompute>(Max); 264 } 265 }; 266 267 /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for 268 /// this function as they are computed. 269 std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts; 270 271 /// ConstantEvolutionLoopExitValue - This map contains entries for all of 272 /// the PHI instructions that we attempt to compute constant evolutions for. 273 /// This allows us to avoid potentially expensive recomputation of these 274 /// properties. An instruction maps to null if we are unable to compute its 275 /// exit value. 276 std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue; 277 278 /// ValuesAtScopes - This map contains entries for all the instructions 279 /// that we attempt to compute getSCEVAtScope information for without 280 /// using SCEV techniques, which can be expensive. 281 std::map<Instruction *, std::map<const Loop *, Constant *> > ValuesAtScopes; 282 283 /// createSCEV - We know that there is no SCEV for the specified value. 284 /// Analyze the expression. 285 SCEVHandle createSCEV(Value *V); 286 287 /// createNodeForPHI - Provide the special handling we need to analyze PHI 288 /// SCEVs. 289 SCEVHandle createNodeForPHI(PHINode *PN); 290 291 /// createNodeForGEP - Provide the special handling we need to analyze GEP 292 /// SCEVs. 293 SCEVHandle createNodeForGEP(User *GEP); 294 295 /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value 296 /// for the specified instruction and replaces any references to the 297 /// symbolic value SymName with the specified value. This is used during 298 /// PHI resolution. 299 void ReplaceSymbolicValueWithConcrete(Instruction *I, 300 const SCEVHandle &SymName, 301 const SCEVHandle &NewVal); 302 303 /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given 304 /// loop, lazily computing new values if the loop hasn't been analyzed 305 /// yet. 306 const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L); 307 308 /// ComputeBackedgeTakenCount - Compute the number of times the specified 309 /// loop will iterate. 310 BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L); 311 312 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition 313 /// of 'icmp op load X, cst', try to see if we can compute the trip count. 314 SCEVHandle 315 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, 316 Constant *RHS, 317 const Loop *L, 318 ICmpInst::Predicate p); 319 320 /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute 321 /// a constant number of times (the condition evolves only from constants), 322 /// try to evaluate a few iterations of the loop until we get the exit 323 /// condition gets a value of ExitWhen (true or false). If we cannot 324 /// evaluate the trip count of the loop, return UnknownValue. 325 SCEVHandle ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond, 326 bool ExitWhen); 327 328 /// HowFarToZero - Return the number of times a backedge comparing the 329 /// specified value to zero will execute. If not computable, return 330 /// UnknownValue. 331 SCEVHandle HowFarToZero(const SCEV *V, const Loop *L); 332 333 /// HowFarToNonZero - Return the number of times a backedge checking the 334 /// specified value for nonzero will execute. If not computable, return 335 /// UnknownValue. 336 SCEVHandle HowFarToNonZero(const SCEV *V, const Loop *L); 337 338 /// HowManyLessThans - Return the number of times a backedge containing the 339 /// specified less-than comparison will execute. If not computable, return 340 /// UnknownValue. isSigned specifies whether the less-than is signed. 341 BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS, 342 const Loop *L, bool isSigned); 343 344 /// getLoopPredecessor - If the given loop's header has exactly one unique 345 /// predecessor outside the loop, return it. Otherwise return null. 346 BasicBlock *getLoopPredecessor(const Loop *L); 347 348 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB 349 /// (which may not be an immediate predecessor) which has exactly one 350 /// successor from which BB is reachable, or null if no such block is 351 /// found. 352 BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB); 353 354 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is 355 /// in the header of its containing loop, we know the loop executes a 356 /// constant number of times, and the PHI node is just a recurrence 357 /// involving constants, fold it. 358 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, 359 const Loop *L); 360 361 /// forgetLoopPHIs - Delete the memoized SCEVs associated with the 362 /// PHI nodes in the given loop. This is used when the trip count of 363 /// the loop may have changed. 364 void forgetLoopPHIs(const Loop *L); 365 366 public: 367 static char ID; // Pass identification, replacement for typeid 368 ScalarEvolution(); 369 370 /// isSCEVable - Test if values of the given type are analyzable within 371 /// the SCEV framework. This primarily includes integer types, and it 372 /// can optionally include pointer types if the ScalarEvolution class 373 /// has access to target-specific information. 374 bool isSCEVable(const Type *Ty) const; 375 376 /// getTypeSizeInBits - Return the size in bits of the specified type, 377 /// for which isSCEVable must return true. 378 uint64_t getTypeSizeInBits(const Type *Ty) const; 379 380 /// getEffectiveSCEVType - Return a type with the same bitwidth as 381 /// the given type and which represents how SCEV will treat the given 382 /// type, for which isSCEVable must return true. For pointer types, 383 /// this is the pointer-sized integer type. 384 const Type *getEffectiveSCEVType(const Type *Ty) const; 385 386 /// getSCEV - Return a SCEV expression handle for the full generality of the 387 /// specified expression. 388 SCEVHandle getSCEV(Value *V); 389 390 SCEVHandle getConstant(ConstantInt *V); 391 SCEVHandle getConstant(const APInt& Val); 392 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty); 393 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty); 394 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty); 395 SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops); 396 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) { 397 std::vector<SCEVHandle> Ops; 398 Ops.push_back(LHS); 399 Ops.push_back(RHS); 400 return getAddExpr(Ops); 401 } 402 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1, 403 const SCEVHandle &Op2) { 404 std::vector<SCEVHandle> Ops; 405 Ops.push_back(Op0); 406 Ops.push_back(Op1); 407 Ops.push_back(Op2); 408 return getAddExpr(Ops); 409 } 410 SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops); 411 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) { 412 std::vector<SCEVHandle> Ops; 413 Ops.push_back(LHS); 414 Ops.push_back(RHS); 415 return getMulExpr(Ops); 416 } 417 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 418 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step, 419 const Loop *L); 420 SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands, 421 const Loop *L); 422 SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands, 423 const Loop *L) { 424 std::vector<SCEVHandle> NewOp(Operands); 425 return getAddRecExpr(NewOp, L); 426 } 427 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 428 SCEVHandle getSMaxExpr(std::vector<SCEVHandle> Operands); 429 SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 430 SCEVHandle getUMaxExpr(std::vector<SCEVHandle> Operands); 431 SCEVHandle getUnknown(Value *V); 432 SCEVHandle getCouldNotCompute(); 433 434 /// getNegativeSCEV - Return the SCEV object corresponding to -V. 435 /// 436 SCEVHandle getNegativeSCEV(const SCEVHandle &V); 437 438 /// getNotSCEV - Return the SCEV object corresponding to ~V. 439 /// 440 SCEVHandle getNotSCEV(const SCEVHandle &V); 441 442 /// getMinusSCEV - Return LHS-RHS. 443 /// 444 SCEVHandle getMinusSCEV(const SCEVHandle &LHS, 445 const SCEVHandle &RHS); 446 447 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion 448 /// of the input value to the specified type. If the type must be 449 /// extended, it is zero extended. 450 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty); 451 452 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion 453 /// of the input value to the specified type. If the type must be 454 /// extended, it is sign extended. 455 SCEVHandle getTruncateOrSignExtend(const SCEVHandle &V, const Type *Ty); 456 457 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of 458 /// the input value to the specified type. If the type must be extended, 459 /// it is zero extended. The conversion must not be narrowing. 460 SCEVHandle getNoopOrZeroExtend(const SCEVHandle &V, const Type *Ty); 461 462 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of 463 /// the input value to the specified type. If the type must be extended, 464 /// it is sign extended. The conversion must not be narrowing. 465 SCEVHandle getNoopOrSignExtend(const SCEVHandle &V, const Type *Ty); 466 467 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the 468 /// input value to the specified type. The conversion must not be 469 /// widening. 470 SCEVHandle getTruncateOrNoop(const SCEVHandle &V, const Type *Ty); 471 472 /// getIntegerSCEV - Given an integer or FP type, create a constant for the 473 /// specified signed integer value and return a SCEV for the constant. 474 SCEVHandle getIntegerSCEV(int Val, const Type *Ty); 475 476 /// hasSCEV - Return true if the SCEV for this value has already been 477 /// computed. 478 bool hasSCEV(Value *V) const; 479 480 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for 481 /// the specified value. 482 void setSCEV(Value *V, const SCEVHandle &H); 483 484 /// getSCEVAtScope - Return a SCEV expression handle for the specified value 485 /// at the specified scope in the program. The L value specifies a loop 486 /// nest to evaluate the expression at, where null is the top-level or a 487 /// specified loop is immediately inside of the loop. 488 /// 489 /// This method can be used to compute the exit value for a variable defined 490 /// in a loop by querying what the value will hold in the parent loop. 491 /// 492 /// If this value is not computable at this scope, a SCEVCouldNotCompute 493 /// object is returned. 494 SCEVHandle getSCEVAtScope(const SCEV *S, const Loop *L); 495 496 /// getSCEVAtScope - This is a convenience function which does 497 /// getSCEVAtScope(getSCEV(V), L). 498 SCEVHandle getSCEVAtScope(Value *V, const Loop *L); 499 500 /// isLoopGuardedByCond - Test whether entry to the loop is protected by 501 /// a conditional between LHS and RHS. This is used to help avoid max 502 /// expressions in loop trip counts. 503 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 504 const SCEV *LHS, const SCEV *RHS); 505 506 /// getBackedgeTakenCount - If the specified loop has a predictable 507 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute 508 /// object. The backedge-taken count is the number of times the loop header 509 /// will be branched to from within the loop. This is one less than the 510 /// trip count of the loop, since it doesn't count the first iteration, 511 /// when the header is branched to from outside the loop. 512 /// 513 /// Note that it is not valid to call this method on a loop without a 514 /// loop-invariant backedge-taken count (see 515 /// hasLoopInvariantBackedgeTakenCount). 516 /// 517 SCEVHandle getBackedgeTakenCount(const Loop *L); 518 519 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except 520 /// return the least SCEV value that is known never to be less than the 521 /// actual backedge taken count. 522 SCEVHandle getMaxBackedgeTakenCount(const Loop *L); 523 524 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop 525 /// has an analyzable loop-invariant backedge-taken count. 526 bool hasLoopInvariantBackedgeTakenCount(const Loop *L); 527 528 /// forgetLoopBackedgeTakenCount - This method should be called by the 529 /// client when it has changed a loop in a way that may effect 530 /// ScalarEvolution's ability to compute a trip count, or if the loop 531 /// is deleted. 532 void forgetLoopBackedgeTakenCount(const Loop *L); 533 534 virtual bool runOnFunction(Function &F); 535 virtual void releaseMemory(); 536 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 537 void print(raw_ostream &OS, const Module* = 0) const; 538 virtual void print(std::ostream &OS, const Module* = 0) const; 539 void print(std::ostream *OS, const Module* M = 0) const { 540 if (OS) print(*OS, M); 541 } 542 }; 543} 544 545#endif 546