1 //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Support/DataTypes.h"
27 #include "llvm/Support/ValueHandle.h"
28 #include "llvm/ADT/DenseMap.h"
36 class ScalarEvolution;
38 template<> struct DenseMapInfo<SCEVHandle>;
40 /// SCEV - This class represents an analyzed expression in the program. These
41 /// are reference-counted opaque objects that the client is not allowed to
42 /// do much with directly.
45 const unsigned SCEVType; // The SCEV baseclass this node corresponds to
46 mutable unsigned RefCount;
48 friend class SCEVHandle;
49 friend class DenseMapInfo<SCEVHandle>;
50 void addRef() const { ++RefCount; }
51 void dropRef() const {
56 const ScalarEvolution* parent;
58 SCEV(const SCEV &); // DO NOT IMPLEMENT
59 void operator=(const SCEV &); // DO NOT IMPLEMENT
63 explicit SCEV(unsigned SCEVTy, const ScalarEvolution* p) :
64 SCEVType(SCEVTy), RefCount(0), parent(p) {}
66 unsigned getSCEVType() const { return SCEVType; }
68 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
69 /// the specified loop.
70 virtual bool isLoopInvariant(const Loop *L) const = 0;
72 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
73 /// known way in the specified loop. This property being true implies that
74 /// the value is variant in the loop AND that we can emit an expression to
75 /// compute the value of the expression at any particular loop iteration.
76 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
78 /// getType - Return the LLVM type of this SCEV expression.
80 virtual const Type *getType() const = 0;
82 /// isZero - Return true if the expression is a constant zero.
86 /// isOne - Return true if the expression is a constant one.
90 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
91 /// the symbolic value "Sym", construct and return a new SCEV that produces
92 /// the same value, but which uses the concrete value Conc instead of the
93 /// symbolic value. If this SCEV does not use the symbolic value, it
96 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
97 const SCEVHandle &Conc,
98 ScalarEvolution &SE) const = 0;
100 /// dominates - Return true if elements that makes up this SCEV dominates
101 /// the specified basic block.
102 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0;
104 /// print - Print out the internal representation of this scalar to the
105 /// specified stream. This should really only be used for debugging
107 virtual void print(raw_ostream &OS) const = 0;
108 void print(std::ostream &OS) const;
109 void print(std::ostream *OS) const { if (OS) print(*OS); }
111 /// dump - This method is used for debugging.
116 inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
121 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
126 /// SCEVCouldNotCompute - An object of this class is returned by queries that
127 /// could not be answered. For example, if you ask for the number of
128 /// iterations of a linked-list traversal loop, you will get one of these.
129 /// None of the standard SCEV operations are valid on this class, it is just a
131 struct SCEVCouldNotCompute : public SCEV {
132 SCEVCouldNotCompute(const ScalarEvolution* p);
133 ~SCEVCouldNotCompute();
135 // None of these methods are valid for this object.
136 virtual bool isLoopInvariant(const Loop *L) const;
137 virtual const Type *getType() const;
138 virtual bool hasComputableLoopEvolution(const Loop *L) const;
139 virtual void print(raw_ostream &OS) const;
141 replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
142 const SCEVHandle &Conc,
143 ScalarEvolution &SE) const;
145 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
149 /// Methods for support type inquiry through isa, cast, and dyn_cast:
150 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
151 static bool classof(const SCEV *S);
154 /// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
155 /// freeing the objects when the last reference is dropped.
158 SCEVHandle(); // DO NOT IMPLEMENT
160 SCEVHandle(const SCEV *s) : S(s) {
161 assert(S && "Cannot create a handle to a null SCEV!");
164 SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
167 ~SCEVHandle() { S->dropRef(); }
169 operator const SCEV*() const { return S; }
171 const SCEV &operator*() const { return *S; }
172 const SCEV *operator->() const { return S; }
174 bool operator==(const SCEV *RHS) const { return S == RHS; }
175 bool operator!=(const SCEV *RHS) const { return S != RHS; }
177 const SCEVHandle &operator=(SCEV *RHS) {
186 const SCEVHandle &operator=(const SCEVHandle &RHS) {
196 template<typename From> struct simplify_type;
197 template<> struct simplify_type<const SCEVHandle> {
198 typedef const SCEV* SimpleType;
199 static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
203 template<> struct simplify_type<SCEVHandle>
204 : public simplify_type<const SCEVHandle> {};
206 // Specialize DenseMapInfo for SCEVHandle so that SCEVHandle may be used
207 // as a key in DenseMaps.
209 struct DenseMapInfo<SCEVHandle> {
210 static inline SCEVHandle getEmptyKey() {
211 static SCEVCouldNotCompute Empty(0);
212 if (Empty.RefCount == 0)
216 static inline SCEVHandle getTombstoneKey() {
217 static SCEVCouldNotCompute Tombstone(0);
218 if (Tombstone.RefCount == 0)
222 static unsigned getHashValue(const SCEVHandle &Val) {
223 return DenseMapInfo<const SCEV *>::getHashValue(Val);
225 static bool isEqual(const SCEVHandle &LHS, const SCEVHandle &RHS) {
228 static bool isPod() { return false; }
231 /// ScalarEvolution - This class is the main scalar evolution driver. Because
232 /// client code (intentionally) can't do much with the SCEV objects directly,
233 /// they must ask this class for services.
235 class ScalarEvolution : public FunctionPass {
236 /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be
237 /// notified whenever a Value is deleted.
238 class SCEVCallbackVH : public CallbackVH {
240 virtual void deleted();
241 virtual void allUsesReplacedWith(Value *New);
243 SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0);
246 friend class SCEVCallbackVH;
247 friend class SCEVExpander;
249 /// F - The function we are analyzing.
253 /// LI - The loop information for the function we are currently analyzing.
257 /// TD - The target data information for the target we are targetting.
261 /// CouldNotCompute - This SCEV is used to represent unknown trip
262 /// counts and things.
263 SCEVHandle CouldNotCompute;
265 /// Scalars - This is a cache of the scalars we have analyzed so far.
267 std::map<SCEVCallbackVH, SCEVHandle> Scalars;
269 /// BackedgeTakenInfo - Information about the backedge-taken count
270 /// of a loop. This currently inclues an exact count and a maximum count.
272 struct BackedgeTakenInfo {
273 /// Exact - An expression indicating the exact backedge-taken count of
274 /// the loop if it is known, or a SCEVCouldNotCompute otherwise.
277 /// Exact - An expression indicating the least maximum backedge-taken
278 /// count of the loop that is known, or a SCEVCouldNotCompute.
281 /*implicit*/ BackedgeTakenInfo(SCEVHandle exact) :
282 Exact(exact), Max(exact) {}
284 /*implicit*/ BackedgeTakenInfo(const SCEV *exact) :
285 Exact(exact), Max(exact) {}
287 BackedgeTakenInfo(SCEVHandle exact, SCEVHandle max) :
288 Exact(exact), Max(max) {}
290 /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any
291 /// computed information, or whether it's all SCEVCouldNotCompute
293 bool hasAnyInfo() const {
294 return !isa<SCEVCouldNotCompute>(Exact) ||
295 !isa<SCEVCouldNotCompute>(Max);
299 /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for
300 /// this function as they are computed.
301 std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
303 /// ConstantEvolutionLoopExitValue - This map contains entries for all of
304 /// the PHI instructions that we attempt to compute constant evolutions for.
305 /// This allows us to avoid potentially expensive recomputation of these
306 /// properties. An instruction maps to null if we are unable to compute its
308 std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
310 /// ValuesAtScopes - This map contains entries for all the instructions
311 /// that we attempt to compute getSCEVAtScope information for without
312 /// using SCEV techniques, which can be expensive.
313 std::map<Instruction *, std::map<const Loop *, Constant *> > ValuesAtScopes;
315 /// createSCEV - We know that there is no SCEV for the specified value.
316 /// Analyze the expression.
317 SCEVHandle createSCEV(Value *V);
319 /// createNodeForPHI - Provide the special handling we need to analyze PHI
321 SCEVHandle createNodeForPHI(PHINode *PN);
323 /// createNodeForGEP - Provide the special handling we need to analyze GEP
325 SCEVHandle createNodeForGEP(User *GEP);
327 /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value
328 /// for the specified instruction and replaces any references to the
329 /// symbolic value SymName with the specified value. This is used during
331 void ReplaceSymbolicValueWithConcrete(Instruction *I,
332 const SCEVHandle &SymName,
333 const SCEVHandle &NewVal);
335 /// getBECount - Subtract the end and start values and divide by the step,
336 /// rounding up, to get the number of times the backedge is executed. Return
337 /// CouldNotCompute if an intermediate computation overflows.
338 SCEVHandle getBECount(const SCEVHandle &Start,
339 const SCEVHandle &End,
340 const SCEVHandle &Step);
342 /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given
343 /// loop, lazily computing new values if the loop hasn't been analyzed
345 const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
347 /// ComputeBackedgeTakenCount - Compute the number of times the specified
348 /// loop will iterate.
349 BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L);
351 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
352 /// of 'icmp op load X, cst', try to see if we can compute the trip count.
354 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
357 ICmpInst::Predicate p);
359 /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute
360 /// a constant number of times (the condition evolves only from constants),
361 /// try to evaluate a few iterations of the loop until we get the exit
362 /// condition gets a value of ExitWhen (true or false). If we cannot
363 /// evaluate the trip count of the loop, return CouldNotCompute.
364 SCEVHandle ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond,
367 /// HowFarToZero - Return the number of times a backedge comparing the
368 /// specified value to zero will execute. If not computable, return
370 SCEVHandle HowFarToZero(const SCEV *V, const Loop *L);
372 /// HowFarToNonZero - Return the number of times a backedge checking the
373 /// specified value for nonzero will execute. If not computable, return
375 SCEVHandle HowFarToNonZero(const SCEV *V, const Loop *L);
377 /// HowManyLessThans - Return the number of times a backedge containing the
378 /// specified less-than comparison will execute. If not computable, return
379 /// CouldNotCompute. isSigned specifies whether the less-than is signed.
380 BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
381 const Loop *L, bool isSigned);
383 /// getLoopPredecessor - If the given loop's header has exactly one unique
384 /// predecessor outside the loop, return it. Otherwise return null.
385 BasicBlock *getLoopPredecessor(const Loop *L);
387 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
388 /// (which may not be an immediate predecessor) which has exactly one
389 /// successor from which BB is reachable, or null if no such block is
391 BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
393 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
394 /// in the header of its containing loop, we know the loop executes a
395 /// constant number of times, and the PHI node is just a recurrence
396 /// involving constants, fold it.
397 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
400 /// forgetLoopPHIs - Delete the memoized SCEVs associated with the
401 /// PHI nodes in the given loop. This is used when the trip count of
402 /// the loop may have changed.
403 void forgetLoopPHIs(const Loop *L);
406 static char ID; // Pass identification, replacement for typeid
409 /// isSCEVable - Test if values of the given type are analyzable within
410 /// the SCEV framework. This primarily includes integer types, and it
411 /// can optionally include pointer types if the ScalarEvolution class
412 /// has access to target-specific information.
413 bool isSCEVable(const Type *Ty) const;
415 /// getTypeSizeInBits - Return the size in bits of the specified type,
416 /// for which isSCEVable must return true.
417 uint64_t getTypeSizeInBits(const Type *Ty) const;
419 /// getEffectiveSCEVType - Return a type with the same bitwidth as
420 /// the given type and which represents how SCEV will treat the given
421 /// type, for which isSCEVable must return true. For pointer types,
422 /// this is the pointer-sized integer type.
423 const Type *getEffectiveSCEVType(const Type *Ty) const;
425 /// getSCEV - Return a SCEV expression handle for the full generality of the
426 /// specified expression.
427 SCEVHandle getSCEV(Value *V);
429 SCEVHandle getConstant(ConstantInt *V);
430 SCEVHandle getConstant(const APInt& Val);
431 SCEVHandle getConstant(const Type *Ty, uint64_t V, bool isSigned = false);
432 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
433 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
434 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
435 SCEVHandle getAnyExtendExpr(const SCEVHandle &Op, const Type *Ty);
436 SCEVHandle getAddExpr(SmallVectorImpl<SCEVHandle> &Ops);
437 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
438 SmallVector<SCEVHandle, 2> Ops;
441 return getAddExpr(Ops);
443 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
444 const SCEVHandle &Op2) {
445 SmallVector<SCEVHandle, 3> Ops;
449 return getAddExpr(Ops);
451 SCEVHandle getMulExpr(SmallVectorImpl<SCEVHandle> &Ops);
452 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
453 SmallVector<SCEVHandle, 2> Ops;
456 return getMulExpr(Ops);
458 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
459 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
461 SCEVHandle getAddRecExpr(SmallVectorImpl<SCEVHandle> &Operands,
463 SCEVHandle getAddRecExpr(const SmallVectorImpl<SCEVHandle> &Operands,
465 SmallVector<SCEVHandle, 4> NewOp(Operands.begin(), Operands.end());
466 return getAddRecExpr(NewOp, L);
468 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
469 SCEVHandle getSMaxExpr(SmallVectorImpl<SCEVHandle> &Operands);
470 SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
471 SCEVHandle getUMaxExpr(SmallVectorImpl<SCEVHandle> &Operands);
472 SCEVHandle getUnknown(Value *V);
473 SCEVHandle getCouldNotCompute();
475 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
477 SCEVHandle getNegativeSCEV(const SCEVHandle &V);
479 /// getNotSCEV - Return the SCEV object corresponding to ~V.
481 SCEVHandle getNotSCEV(const SCEVHandle &V);
483 /// getMinusSCEV - Return LHS-RHS.
485 SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
486 const SCEVHandle &RHS);
488 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
489 /// of the input value to the specified type. If the type must be
490 /// extended, it is zero extended.
491 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
493 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
494 /// of the input value to the specified type. If the type must be
495 /// extended, it is sign extended.
496 SCEVHandle getTruncateOrSignExtend(const SCEVHandle &V, const Type *Ty);
498 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of
499 /// the input value to the specified type. If the type must be extended,
500 /// it is zero extended. The conversion must not be narrowing.
501 SCEVHandle getNoopOrZeroExtend(const SCEVHandle &V, const Type *Ty);
503 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of
504 /// the input value to the specified type. If the type must be extended,
505 /// it is sign extended. The conversion must not be narrowing.
506 SCEVHandle getNoopOrSignExtend(const SCEVHandle &V, const Type *Ty);
508 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
509 /// the input value to the specified type. If the type must be extended,
510 /// it is extended with unspecified bits. The conversion must not be
512 SCEVHandle getNoopOrAnyExtend(const SCEVHandle &V, const Type *Ty);
514 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
515 /// input value to the specified type. The conversion must not be
517 SCEVHandle getTruncateOrNoop(const SCEVHandle &V, const Type *Ty);
519 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
520 /// specified signed integer value and return a SCEV for the constant.
521 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
523 /// hasSCEV - Return true if the SCEV for this value has already been
525 bool hasSCEV(Value *V) const;
527 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
528 /// the specified value.
529 void setSCEV(Value *V, const SCEVHandle &H);
531 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
532 /// at the specified scope in the program. The L value specifies a loop
533 /// nest to evaluate the expression at, where null is the top-level or a
534 /// specified loop is immediately inside of the loop.
536 /// This method can be used to compute the exit value for a variable defined
537 /// in a loop by querying what the value will hold in the parent loop.
539 /// In the case that a relevant loop exit value cannot be computed, the
540 /// original value V is returned.
541 SCEVHandle getSCEVAtScope(const SCEV *S, const Loop *L);
543 /// getSCEVAtScope - This is a convenience function which does
544 /// getSCEVAtScope(getSCEV(V), L).
545 SCEVHandle getSCEVAtScope(Value *V, const Loop *L);
547 /// isLoopGuardedByCond - Test whether entry to the loop is protected by
548 /// a conditional between LHS and RHS. This is used to help avoid max
549 /// expressions in loop trip counts.
550 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
551 const SCEV *LHS, const SCEV *RHS);
553 /// getBackedgeTakenCount - If the specified loop has a predictable
554 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
555 /// object. The backedge-taken count is the number of times the loop header
556 /// will be branched to from within the loop. This is one less than the
557 /// trip count of the loop, since it doesn't count the first iteration,
558 /// when the header is branched to from outside the loop.
560 /// Note that it is not valid to call this method on a loop without a
561 /// loop-invariant backedge-taken count (see
562 /// hasLoopInvariantBackedgeTakenCount).
564 SCEVHandle getBackedgeTakenCount(const Loop *L);
566 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
567 /// return the least SCEV value that is known never to be less than the
568 /// actual backedge taken count.
569 SCEVHandle getMaxBackedgeTakenCount(const Loop *L);
571 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
572 /// has an analyzable loop-invariant backedge-taken count.
573 bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
575 /// forgetLoopBackedgeTakenCount - This method should be called by the
576 /// client when it has changed a loop in a way that may effect
577 /// ScalarEvolution's ability to compute a trip count, or if the loop
579 void forgetLoopBackedgeTakenCount(const Loop *L);
581 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
582 /// guaranteed to end in (at every loop iteration). It is, at the same time,
583 /// the minimum number of times S is divisible by 2. For example, given {4,+,8}
584 /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S.
585 uint32_t GetMinTrailingZeros(const SCEVHandle &S);
587 /// GetMinLeadingZeros - Determine the minimum number of zero bits that S is
588 /// guaranteed to begin with (at every loop iteration).
589 uint32_t GetMinLeadingZeros(const SCEVHandle &S);
591 /// GetMinSignBits - Determine the minimum number of sign bits that S is
592 /// guaranteed to begin with.
593 uint32_t GetMinSignBits(const SCEVHandle &S);
595 virtual bool runOnFunction(Function &F);
596 virtual void releaseMemory();
597 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
598 void print(raw_ostream &OS, const Module* = 0) const;
599 virtual void print(std::ostream &OS, const Module* = 0) const;
600 void print(std::ostream *OS, const Module* M = 0) const {
601 if (OS) print(*OS, M);