ScalarEvolution.h revision 444f49150df8a4280ccea20fc2839cd899fc7558
180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com//===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===// 280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// 380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// The LLVM Compiler Infrastructure 480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// 580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// This file is distributed under the University of Illinois Open Source 680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// License. See LICENSE.TXT for details. 780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// 880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com//===----------------------------------------------------------------------===// 980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// 1080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// The ScalarEvolution class is an LLVM pass which can be used to analyze and 1180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// catagorize scalar expressions in loops. It specializes in recognizing 1280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// general induction variables, representing them with the abstract and opaque 1380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// SCEV class. Given this analysis, trip counts of loops and other important 1480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// properties can be obtained. 1580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// 1680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// This analysis is primarily useful for induction variable substitution and 1780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// strength reduction. 1880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com// 1980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com//===----------------------------------------------------------------------===// 2080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 2180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H 2280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#define LLVM_ANALYSIS_SCALAREVOLUTION_H 2380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 2480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include "llvm/Pass.h" 2580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include "llvm/Analysis/LoopInfo.h" 2680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include "llvm/Support/DataTypes.h" 2780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include "llvm/Support/ValueHandle.h" 2880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include "llvm/ADT/DenseMap.h" 2980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com#include <iosfwd> 3080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 3180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.comnamespace llvm { 3280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com class APInt; 3380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com class ConstantInt; 3480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com class Type; 3580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com class SCEVHandle; 3680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com class ScalarEvolution; 3780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com class TargetData; 3880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com template<> struct DenseMapInfo<SCEVHandle>; 3980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 4080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// SCEV - This class represents an analyzed expression in the program. These 4180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// are reference-counted opaque objects that the client is not allowed to 4280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// do much with directly. 4380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// 4480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com class SCEV { 4580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com const unsigned SCEVType; // The SCEV baseclass this node corresponds to 4680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com mutable unsigned RefCount; 4780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 4880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com friend class SCEVHandle; 4980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com friend class DenseMapInfo<SCEVHandle>; 5080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com void addRef() const { ++RefCount; } 5180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com void dropRef() const { 5280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com if (--RefCount == 0) 5380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com delete this; 5480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com } 5580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 5680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com SCEV(const SCEV &); // DO NOT IMPLEMENT 5780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com void operator=(const SCEV &); // DO NOT IMPLEMENT 5880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com protected: 5980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com virtual ~SCEV(); 6080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com public: 6180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {} 6280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 6380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com unsigned getSCEVType() const { return SCEVType; } 6480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 6580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// isLoopInvariant - Return true if the value of this SCEV is unchanging in 6680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// the specified loop. 6780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com virtual bool isLoopInvariant(const Loop *L) const = 0; 6880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 6980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// hasComputableLoopEvolution - Return true if this SCEV changes value in a 7080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// known way in the specified loop. This property being true implies that 7180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// the value is variant in the loop AND that we can emit an expression to 7280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// compute the value of the expression at any particular loop iteration. 7380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com virtual bool hasComputableLoopEvolution(const Loop *L) const = 0; 7480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 7580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// getType - Return the LLVM type of this SCEV expression. 7680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// 7780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com virtual const Type *getType() const = 0; 7880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 7980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// isZero - Return true if the expression is a constant zero. 8080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// 8180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com bool isZero() const; 8280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 8380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// isOne - Return true if the expression is a constant one. 8480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// 8580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com bool isOne() const; 8680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 8780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// replaceSymbolicValuesWithConcrete - If this SCEV internally references 8880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// the symbolic value "Sym", construct and return a new SCEV that produces 8980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// the same value, but which uses the concrete value Conc instead of the 9080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// symbolic value. If this SCEV does not use the symbolic value, it 9180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// returns itself. 9280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com virtual SCEVHandle 9380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, 9480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com const SCEVHandle &Conc, 9580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com ScalarEvolution &SE) const = 0; 9680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 9780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// dominates - Return true if elements that makes up this SCEV dominates 9880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// the specified basic block. 9980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0; 10080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 10180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// print - Print out the internal representation of this scalar to the 10280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// specified stream. This should really only be used for debugging 10380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// purposes. 10480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com virtual void print(raw_ostream &OS) const = 0; 10580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com void print(std::ostream &OS) const; 10680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com void print(std::ostream *OS) const { if (OS) print(*OS); } 10780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 10880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// dump - This method is used for debugging. 10980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// 11080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com void dump() const; 11180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com }; 11280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 11380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) { 11480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com S.print(OS); 11580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com return OS; 11680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com } 11780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 11880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) { 11980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com S.print(OS); 12080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com return OS; 12180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com } 12280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 12380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// SCEVCouldNotCompute - An object of this class is returned by queries that 12480d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// could not be answered. For example, if you ask for the number of 12580d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// iterations of a linked-list traversal loop, you will get one of these. 12680d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// None of the standard SCEV operations are valid on this class, it is just a 12780d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com /// marker. 12880d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com struct SCEVCouldNotCompute : public SCEV { 12980d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com SCEVCouldNotCompute(); 13080d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com ~SCEVCouldNotCompute(); 13180d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com 13280d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com // None of these methods are valid for this object. 13380d738a894e26aa958eacc62afbc70617b13dc83philip.liard@gmail.com virtual bool isLoopInvariant(const Loop *L) const; 134 virtual const Type *getType() const; 135 virtual bool hasComputableLoopEvolution(const Loop *L) const; 136 virtual void print(raw_ostream &OS) const; 137 virtual SCEVHandle 138 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, 139 const SCEVHandle &Conc, 140 ScalarEvolution &SE) const; 141 142 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const { 143 return true; 144 } 145 146 /// Methods for support type inquiry through isa, cast, and dyn_cast: 147 static inline bool classof(const SCEVCouldNotCompute *S) { return true; } 148 static bool classof(const SCEV *S); 149 }; 150 151 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts, 152 /// freeing the objects when the last reference is dropped. 153 class SCEVHandle { 154 const SCEV *S; 155 SCEVHandle(); // DO NOT IMPLEMENT 156 public: 157 SCEVHandle(const SCEV *s) : S(s) { 158 assert(S && "Cannot create a handle to a null SCEV!"); 159 S->addRef(); 160 } 161 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) { 162 S->addRef(); 163 } 164 ~SCEVHandle() { S->dropRef(); } 165 166 operator const SCEV*() const { return S; } 167 168 const SCEV &operator*() const { return *S; } 169 const SCEV *operator->() const { return S; } 170 171 bool operator==(const SCEV *RHS) const { return S == RHS; } 172 bool operator!=(const SCEV *RHS) const { return S != RHS; } 173 174 const SCEVHandle &operator=(SCEV *RHS) { 175 if (S != RHS) { 176 S->dropRef(); 177 S = RHS; 178 S->addRef(); 179 } 180 return *this; 181 } 182 183 const SCEVHandle &operator=(const SCEVHandle &RHS) { 184 if (S != RHS.S) { 185 S->dropRef(); 186 S = RHS.S; 187 S->addRef(); 188 } 189 return *this; 190 } 191 }; 192 193 template<typename From> struct simplify_type; 194 template<> struct simplify_type<const SCEVHandle> { 195 typedef const SCEV* SimpleType; 196 static SimpleType getSimplifiedValue(const SCEVHandle &Node) { 197 return Node; 198 } 199 }; 200 template<> struct simplify_type<SCEVHandle> 201 : public simplify_type<const SCEVHandle> {}; 202 203 // Specialize DenseMapInfo for SCEVHandle so that SCEVHandle may be used 204 // as a key in DenseMaps. 205 template<> 206 struct DenseMapInfo<SCEVHandle> { 207 static inline SCEVHandle getEmptyKey() { 208 static SCEVCouldNotCompute Empty; 209 if (Empty.RefCount == 0) 210 Empty.addRef(); 211 return &Empty; 212 } 213 static inline SCEVHandle getTombstoneKey() { 214 static SCEVCouldNotCompute Tombstone; 215 if (Tombstone.RefCount == 0) 216 Tombstone.addRef(); 217 return &Tombstone; 218 } 219 static unsigned getHashValue(const SCEVHandle &Val) { 220 return DenseMapInfo<const SCEV *>::getHashValue(Val); 221 } 222 static bool isEqual(const SCEVHandle &LHS, const SCEVHandle &RHS) { 223 return LHS == RHS; 224 } 225 static bool isPod() { return false; } 226 }; 227 228 /// ScalarEvolution - This class is the main scalar evolution driver. Because 229 /// client code (intentionally) can't do much with the SCEV objects directly, 230 /// they must ask this class for services. 231 /// 232 class ScalarEvolution : public FunctionPass { 233 /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be 234 /// notified whenever a Value is deleted. 235 class SCEVCallbackVH : public CallbackVH { 236 ScalarEvolution *SE; 237 virtual void deleted(); 238 virtual void allUsesReplacedWith(Value *New); 239 public: 240 SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0); 241 }; 242 243 friend class SCEVCallbackVH; 244 friend class SCEVExpander; 245 246 /// F - The function we are analyzing. 247 /// 248 Function *F; 249 250 /// LI - The loop information for the function we are currently analyzing. 251 /// 252 LoopInfo *LI; 253 254 /// TD - The target data information for the target we are targetting. 255 /// 256 TargetData *TD; 257 258 /// CouldNotCompute - This SCEV is used to represent unknown trip 259 /// counts and things. 260 SCEVHandle CouldNotCompute; 261 262 /// Scalars - This is a cache of the scalars we have analyzed so far. 263 /// 264 std::map<SCEVCallbackVH, SCEVHandle> Scalars; 265 266 /// BackedgeTakenInfo - Information about the backedge-taken count 267 /// of a loop. This currently inclues an exact count and a maximum count. 268 /// 269 struct BackedgeTakenInfo { 270 /// Exact - An expression indicating the exact backedge-taken count of 271 /// the loop if it is known, or a SCEVCouldNotCompute otherwise. 272 SCEVHandle Exact; 273 274 /// Exact - An expression indicating the least maximum backedge-taken 275 /// count of the loop that is known, or a SCEVCouldNotCompute. 276 SCEVHandle Max; 277 278 /*implicit*/ BackedgeTakenInfo(SCEVHandle exact) : 279 Exact(exact), Max(exact) {} 280 281 /*implicit*/ BackedgeTakenInfo(const SCEV *exact) : 282 Exact(exact), Max(exact) {} 283 284 BackedgeTakenInfo(SCEVHandle exact, SCEVHandle max) : 285 Exact(exact), Max(max) {} 286 287 /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any 288 /// computed information, or whether it's all SCEVCouldNotCompute 289 /// values. 290 bool hasAnyInfo() const { 291 return !isa<SCEVCouldNotCompute>(Exact) || 292 !isa<SCEVCouldNotCompute>(Max); 293 } 294 }; 295 296 /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for 297 /// this function as they are computed. 298 std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts; 299 300 /// ConstantEvolutionLoopExitValue - This map contains entries for all of 301 /// the PHI instructions that we attempt to compute constant evolutions for. 302 /// This allows us to avoid potentially expensive recomputation of these 303 /// properties. An instruction maps to null if we are unable to compute its 304 /// exit value. 305 std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue; 306 307 /// ValuesAtScopes - This map contains entries for all the instructions 308 /// that we attempt to compute getSCEVAtScope information for without 309 /// using SCEV techniques, which can be expensive. 310 std::map<Instruction *, std::map<const Loop *, Constant *> > ValuesAtScopes; 311 312 /// createSCEV - We know that there is no SCEV for the specified value. 313 /// Analyze the expression. 314 SCEVHandle createSCEV(Value *V); 315 316 /// createNodeForPHI - Provide the special handling we need to analyze PHI 317 /// SCEVs. 318 SCEVHandle createNodeForPHI(PHINode *PN); 319 320 /// createNodeForGEP - Provide the special handling we need to analyze GEP 321 /// SCEVs. 322 SCEVHandle createNodeForGEP(User *GEP); 323 324 /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value 325 /// for the specified instruction and replaces any references to the 326 /// symbolic value SymName with the specified value. This is used during 327 /// PHI resolution. 328 void ReplaceSymbolicValueWithConcrete(Instruction *I, 329 const SCEVHandle &SymName, 330 const SCEVHandle &NewVal); 331 332 /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given 333 /// loop, lazily computing new values if the loop hasn't been analyzed 334 /// yet. 335 const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L); 336 337 /// ComputeBackedgeTakenCount - Compute the number of times the specified 338 /// loop will iterate. 339 BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L); 340 341 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition 342 /// of 'icmp op load X, cst', try to see if we can compute the trip count. 343 SCEVHandle 344 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, 345 Constant *RHS, 346 const Loop *L, 347 ICmpInst::Predicate p); 348 349 /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute 350 /// a constant number of times (the condition evolves only from constants), 351 /// try to evaluate a few iterations of the loop until we get the exit 352 /// condition gets a value of ExitWhen (true or false). If we cannot 353 /// evaluate the trip count of the loop, return CouldNotCompute. 354 SCEVHandle ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond, 355 bool ExitWhen); 356 357 /// HowFarToZero - Return the number of times a backedge comparing the 358 /// specified value to zero will execute. If not computable, return 359 /// CouldNotCompute. 360 SCEVHandle HowFarToZero(const SCEV *V, const Loop *L); 361 362 /// HowFarToNonZero - Return the number of times a backedge checking the 363 /// specified value for nonzero will execute. If not computable, return 364 /// CouldNotCompute. 365 SCEVHandle HowFarToNonZero(const SCEV *V, const Loop *L); 366 367 /// HowManyLessThans - Return the number of times a backedge containing the 368 /// specified less-than comparison will execute. If not computable, return 369 /// CouldNotCompute. isSigned specifies whether the less-than is signed. 370 BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS, 371 const Loop *L, bool isSigned); 372 373 /// getLoopPredecessor - If the given loop's header has exactly one unique 374 /// predecessor outside the loop, return it. Otherwise return null. 375 BasicBlock *getLoopPredecessor(const Loop *L); 376 377 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB 378 /// (which may not be an immediate predecessor) which has exactly one 379 /// successor from which BB is reachable, or null if no such block is 380 /// found. 381 BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB); 382 383 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is 384 /// in the header of its containing loop, we know the loop executes a 385 /// constant number of times, and the PHI node is just a recurrence 386 /// involving constants, fold it. 387 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, 388 const Loop *L); 389 390 /// forgetLoopPHIs - Delete the memoized SCEVs associated with the 391 /// PHI nodes in the given loop. This is used when the trip count of 392 /// the loop may have changed. 393 void forgetLoopPHIs(const Loop *L); 394 395 public: 396 static char ID; // Pass identification, replacement for typeid 397 ScalarEvolution(); 398 399 /// isSCEVable - Test if values of the given type are analyzable within 400 /// the SCEV framework. This primarily includes integer types, and it 401 /// can optionally include pointer types if the ScalarEvolution class 402 /// has access to target-specific information. 403 bool isSCEVable(const Type *Ty) const; 404 405 /// getTypeSizeInBits - Return the size in bits of the specified type, 406 /// for which isSCEVable must return true. 407 uint64_t getTypeSizeInBits(const Type *Ty) const; 408 409 /// getEffectiveSCEVType - Return a type with the same bitwidth as 410 /// the given type and which represents how SCEV will treat the given 411 /// type, for which isSCEVable must return true. For pointer types, 412 /// this is the pointer-sized integer type. 413 const Type *getEffectiveSCEVType(const Type *Ty) const; 414 415 /// getSCEV - Return a SCEV expression handle for the full generality of the 416 /// specified expression. 417 SCEVHandle getSCEV(Value *V); 418 419 SCEVHandle getConstant(ConstantInt *V); 420 SCEVHandle getConstant(const APInt& Val); 421 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty); 422 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty); 423 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty); 424 SCEVHandle getAnyExtendExpr(const SCEVHandle &Op, const Type *Ty); 425 SCEVHandle getAddExpr(SmallVectorImpl<SCEVHandle> &Ops); 426 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) { 427 SmallVector<SCEVHandle, 2> Ops; 428 Ops.push_back(LHS); 429 Ops.push_back(RHS); 430 return getAddExpr(Ops); 431 } 432 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1, 433 const SCEVHandle &Op2) { 434 SmallVector<SCEVHandle, 3> Ops; 435 Ops.push_back(Op0); 436 Ops.push_back(Op1); 437 Ops.push_back(Op2); 438 return getAddExpr(Ops); 439 } 440 SCEVHandle getMulExpr(SmallVectorImpl<SCEVHandle> &Ops); 441 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) { 442 SmallVector<SCEVHandle, 2> Ops; 443 Ops.push_back(LHS); 444 Ops.push_back(RHS); 445 return getMulExpr(Ops); 446 } 447 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 448 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step, 449 const Loop *L); 450 SCEVHandle getAddRecExpr(SmallVectorImpl<SCEVHandle> &Operands, 451 const Loop *L); 452 SCEVHandle getAddRecExpr(const SmallVectorImpl<SCEVHandle> &Operands, 453 const Loop *L) { 454 SmallVector<SCEVHandle, 4> NewOp(Operands.begin(), Operands.end()); 455 return getAddRecExpr(NewOp, L); 456 } 457 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 458 SCEVHandle getSMaxExpr(SmallVectorImpl<SCEVHandle> &Operands); 459 SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); 460 SCEVHandle getUMaxExpr(SmallVectorImpl<SCEVHandle> &Operands); 461 SCEVHandle getUnknown(Value *V); 462 SCEVHandle getCouldNotCompute(); 463 464 /// getNegativeSCEV - Return the SCEV object corresponding to -V. 465 /// 466 SCEVHandle getNegativeSCEV(const SCEVHandle &V); 467 468 /// getNotSCEV - Return the SCEV object corresponding to ~V. 469 /// 470 SCEVHandle getNotSCEV(const SCEVHandle &V); 471 472 /// getMinusSCEV - Return LHS-RHS. 473 /// 474 SCEVHandle getMinusSCEV(const SCEVHandle &LHS, 475 const SCEVHandle &RHS); 476 477 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion 478 /// of the input value to the specified type. If the type must be 479 /// extended, it is zero extended. 480 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty); 481 482 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion 483 /// of the input value to the specified type. If the type must be 484 /// extended, it is sign extended. 485 SCEVHandle getTruncateOrSignExtend(const SCEVHandle &V, const Type *Ty); 486 487 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of 488 /// the input value to the specified type. If the type must be extended, 489 /// it is zero extended. The conversion must not be narrowing. 490 SCEVHandle getNoopOrZeroExtend(const SCEVHandle &V, const Type *Ty); 491 492 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of 493 /// the input value to the specified type. If the type must be extended, 494 /// it is sign extended. The conversion must not be narrowing. 495 SCEVHandle getNoopOrSignExtend(const SCEVHandle &V, const Type *Ty); 496 497 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of 498 /// the input value to the specified type. If the type must be extended, 499 /// it is extended with unspecified bits. The conversion must not be 500 /// narrowing. 501 SCEVHandle getNoopOrAnyExtend(const SCEVHandle &V, const Type *Ty); 502 503 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the 504 /// input value to the specified type. The conversion must not be 505 /// widening. 506 SCEVHandle getTruncateOrNoop(const SCEVHandle &V, const Type *Ty); 507 508 /// getIntegerSCEV - Given an integer or FP type, create a constant for the 509 /// specified signed integer value and return a SCEV for the constant. 510 SCEVHandle getIntegerSCEV(int Val, const Type *Ty); 511 512 /// hasSCEV - Return true if the SCEV for this value has already been 513 /// computed. 514 bool hasSCEV(Value *V) const; 515 516 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for 517 /// the specified value. 518 void setSCEV(Value *V, const SCEVHandle &H); 519 520 /// getSCEVAtScope - Return a SCEV expression handle for the specified value 521 /// at the specified scope in the program. The L value specifies a loop 522 /// nest to evaluate the expression at, where null is the top-level or a 523 /// specified loop is immediately inside of the loop. 524 /// 525 /// This method can be used to compute the exit value for a variable defined 526 /// in a loop by querying what the value will hold in the parent loop. 527 /// 528 /// In the case that a relevant loop exit value cannot be computed, the 529 /// original value V is returned. 530 SCEVHandle getSCEVAtScope(const SCEV *S, const Loop *L); 531 532 /// getSCEVAtScope - This is a convenience function which does 533 /// getSCEVAtScope(getSCEV(V), L). 534 SCEVHandle getSCEVAtScope(Value *V, const Loop *L); 535 536 /// isLoopGuardedByCond - Test whether entry to the loop is protected by 537 /// a conditional between LHS and RHS. This is used to help avoid max 538 /// expressions in loop trip counts. 539 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 540 const SCEV *LHS, const SCEV *RHS); 541 542 /// getBackedgeTakenCount - If the specified loop has a predictable 543 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute 544 /// object. The backedge-taken count is the number of times the loop header 545 /// will be branched to from within the loop. This is one less than the 546 /// trip count of the loop, since it doesn't count the first iteration, 547 /// when the header is branched to from outside the loop. 548 /// 549 /// Note that it is not valid to call this method on a loop without a 550 /// loop-invariant backedge-taken count (see 551 /// hasLoopInvariantBackedgeTakenCount). 552 /// 553 SCEVHandle getBackedgeTakenCount(const Loop *L); 554 555 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except 556 /// return the least SCEV value that is known never to be less than the 557 /// actual backedge taken count. 558 SCEVHandle getMaxBackedgeTakenCount(const Loop *L); 559 560 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop 561 /// has an analyzable loop-invariant backedge-taken count. 562 bool hasLoopInvariantBackedgeTakenCount(const Loop *L); 563 564 /// forgetLoopBackedgeTakenCount - This method should be called by the 565 /// client when it has changed a loop in a way that may effect 566 /// ScalarEvolution's ability to compute a trip count, or if the loop 567 /// is deleted. 568 void forgetLoopBackedgeTakenCount(const Loop *L); 569 570 virtual bool runOnFunction(Function &F); 571 virtual void releaseMemory(); 572 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 573 void print(raw_ostream &OS, const Module* = 0) const; 574 virtual void print(std::ostream &OS, const Module* = 0) const; 575 void print(std::ostream *OS, const Module* M = 0) const { 576 if (OS) print(*OS, M); 577 } 578 }; 579} 580 581#endif 582