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 /// ComputeBackedgeTakenCountFromExit - Compute the number of times the
352 /// backedge of the specified loop will execute if it exits via the
354 BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L,
355 BasicBlock *ExitingBlock);
357 /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
358 /// backedge of the specified loop will execute if its exit condition
359 /// were a conditional branch of ExitCond, TBB, and FBB.
361 ComputeBackedgeTakenCountFromExitCond(const Loop *L,
366 /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of
367 /// times the backedge of the specified loop will execute if its exit
368 /// condition were a conditional branch of the ICmpInst ExitCond, TBB,
371 ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
376 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
377 /// of 'icmp op load X, cst', try to see if we can compute the trip count.
379 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
382 ICmpInst::Predicate p);
384 /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute
385 /// a constant number of times (the condition evolves only from constants),
386 /// try to evaluate a few iterations of the loop until we get the exit
387 /// condition gets a value of ExitWhen (true or false). If we cannot
388 /// evaluate the trip count of the loop, return CouldNotCompute.
389 SCEVHandle ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond,
392 /// HowFarToZero - Return the number of times a backedge comparing the
393 /// specified value to zero will execute. If not computable, return
395 SCEVHandle HowFarToZero(const SCEV *V, const Loop *L);
397 /// HowFarToNonZero - Return the number of times a backedge checking the
398 /// specified value for nonzero will execute. If not computable, return
400 SCEVHandle HowFarToNonZero(const SCEV *V, const Loop *L);
402 /// HowManyLessThans - Return the number of times a backedge containing the
403 /// specified less-than comparison will execute. If not computable, return
404 /// CouldNotCompute. isSigned specifies whether the less-than is signed.
405 BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
406 const Loop *L, bool isSigned);
408 /// getLoopPredecessor - If the given loop's header has exactly one unique
409 /// predecessor outside the loop, return it. Otherwise return null.
410 BasicBlock *getLoopPredecessor(const Loop *L);
412 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
413 /// (which may not be an immediate predecessor) which has exactly one
414 /// successor from which BB is reachable, or null if no such block is
416 BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
418 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
419 /// in the header of its containing loop, we know the loop executes a
420 /// constant number of times, and the PHI node is just a recurrence
421 /// involving constants, fold it.
422 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
425 /// forgetLoopPHIs - Delete the memoized SCEVs associated with the
426 /// PHI nodes in the given loop. This is used when the trip count of
427 /// the loop may have changed.
428 void forgetLoopPHIs(const Loop *L);
431 static char ID; // Pass identification, replacement for typeid
434 /// isSCEVable - Test if values of the given type are analyzable within
435 /// the SCEV framework. This primarily includes integer types, and it
436 /// can optionally include pointer types if the ScalarEvolution class
437 /// has access to target-specific information.
438 bool isSCEVable(const Type *Ty) const;
440 /// getTypeSizeInBits - Return the size in bits of the specified type,
441 /// for which isSCEVable must return true.
442 uint64_t getTypeSizeInBits(const Type *Ty) const;
444 /// getEffectiveSCEVType - Return a type with the same bitwidth as
445 /// the given type and which represents how SCEV will treat the given
446 /// type, for which isSCEVable must return true. For pointer types,
447 /// this is the pointer-sized integer type.
448 const Type *getEffectiveSCEVType(const Type *Ty) const;
450 /// getSCEV - Return a SCEV expression handle for the full generality of the
451 /// specified expression.
452 SCEVHandle getSCEV(Value *V);
454 SCEVHandle getConstant(ConstantInt *V);
455 SCEVHandle getConstant(const APInt& Val);
456 SCEVHandle getConstant(const Type *Ty, uint64_t V, bool isSigned = false);
457 SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
458 SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
459 SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
460 SCEVHandle getAnyExtendExpr(const SCEVHandle &Op, const Type *Ty);
461 SCEVHandle getAddExpr(SmallVectorImpl<SCEVHandle> &Ops);
462 SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
463 SmallVector<SCEVHandle, 2> Ops;
466 return getAddExpr(Ops);
468 SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
469 const SCEVHandle &Op2) {
470 SmallVector<SCEVHandle, 3> Ops;
474 return getAddExpr(Ops);
476 SCEVHandle getMulExpr(SmallVectorImpl<SCEVHandle> &Ops);
477 SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
478 SmallVector<SCEVHandle, 2> Ops;
481 return getMulExpr(Ops);
483 SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
484 SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
486 SCEVHandle getAddRecExpr(SmallVectorImpl<SCEVHandle> &Operands,
488 SCEVHandle getAddRecExpr(const SmallVectorImpl<SCEVHandle> &Operands,
490 SmallVector<SCEVHandle, 4> NewOp(Operands.begin(), Operands.end());
491 return getAddRecExpr(NewOp, L);
493 SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
494 SCEVHandle getSMaxExpr(SmallVectorImpl<SCEVHandle> &Operands);
495 SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
496 SCEVHandle getUMaxExpr(SmallVectorImpl<SCEVHandle> &Operands);
497 SCEVHandle getUnknown(Value *V);
498 SCEVHandle getCouldNotCompute();
500 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
502 SCEVHandle getNegativeSCEV(const SCEVHandle &V);
504 /// getNotSCEV - Return the SCEV object corresponding to ~V.
506 SCEVHandle getNotSCEV(const SCEVHandle &V);
508 /// getMinusSCEV - Return LHS-RHS.
510 SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
511 const SCEVHandle &RHS);
513 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
514 /// of the input value to the specified type. If the type must be
515 /// extended, it is zero extended.
516 SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
518 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
519 /// of the input value to the specified type. If the type must be
520 /// extended, it is sign extended.
521 SCEVHandle getTruncateOrSignExtend(const SCEVHandle &V, const Type *Ty);
523 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of
524 /// the input value to the specified type. If the type must be extended,
525 /// it is zero extended. The conversion must not be narrowing.
526 SCEVHandle getNoopOrZeroExtend(const SCEVHandle &V, const Type *Ty);
528 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of
529 /// the input value to the specified type. If the type must be extended,
530 /// it is sign extended. The conversion must not be narrowing.
531 SCEVHandle getNoopOrSignExtend(const SCEVHandle &V, const Type *Ty);
533 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
534 /// the input value to the specified type. If the type must be extended,
535 /// it is extended with unspecified bits. The conversion must not be
537 SCEVHandle getNoopOrAnyExtend(const SCEVHandle &V, const Type *Ty);
539 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
540 /// input value to the specified type. The conversion must not be
542 SCEVHandle getTruncateOrNoop(const SCEVHandle &V, const Type *Ty);
544 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
545 /// specified signed integer value and return a SCEV for the constant.
546 SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
548 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
549 /// the types using zero-extension, and then perform a umax operation
551 SCEVHandle getUMaxFromMismatchedTypes(const SCEVHandle &LHS,
552 const SCEVHandle &RHS);
554 /// hasSCEV - Return true if the SCEV for this value has already been
556 bool hasSCEV(Value *V) const;
558 /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
559 /// the specified value.
560 void setSCEV(Value *V, const SCEVHandle &H);
562 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
563 /// at the specified scope in the program. The L value specifies a loop
564 /// nest to evaluate the expression at, where null is the top-level or a
565 /// specified loop is immediately inside of the loop.
567 /// This method can be used to compute the exit value for a variable defined
568 /// in a loop by querying what the value will hold in the parent loop.
570 /// In the case that a relevant loop exit value cannot be computed, the
571 /// original value V is returned.
572 SCEVHandle getSCEVAtScope(const SCEV *S, const Loop *L);
574 /// getSCEVAtScope - This is a convenience function which does
575 /// getSCEVAtScope(getSCEV(V), L).
576 SCEVHandle getSCEVAtScope(Value *V, const Loop *L);
578 /// isLoopGuardedByCond - Test whether entry to the loop is protected by
579 /// a conditional between LHS and RHS. This is used to help avoid max
580 /// expressions in loop trip counts.
581 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
582 const SCEV *LHS, const SCEV *RHS);
584 /// getBackedgeTakenCount - If the specified loop has a predictable
585 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
586 /// object. The backedge-taken count is the number of times the loop header
587 /// will be branched to from within the loop. This is one less than the
588 /// trip count of the loop, since it doesn't count the first iteration,
589 /// when the header is branched to from outside the loop.
591 /// Note that it is not valid to call this method on a loop without a
592 /// loop-invariant backedge-taken count (see
593 /// hasLoopInvariantBackedgeTakenCount).
595 SCEVHandle getBackedgeTakenCount(const Loop *L);
597 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
598 /// return the least SCEV value that is known never to be less than the
599 /// actual backedge taken count.
600 SCEVHandle getMaxBackedgeTakenCount(const Loop *L);
602 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
603 /// has an analyzable loop-invariant backedge-taken count.
604 bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
606 /// forgetLoopBackedgeTakenCount - This method should be called by the
607 /// client when it has changed a loop in a way that may effect
608 /// ScalarEvolution's ability to compute a trip count, or if the loop
610 void forgetLoopBackedgeTakenCount(const Loop *L);
612 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
613 /// guaranteed to end in (at every loop iteration). It is, at the same time,
614 /// the minimum number of times S is divisible by 2. For example, given {4,+,8}
615 /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S.
616 uint32_t GetMinTrailingZeros(const SCEVHandle &S);
618 /// GetMinLeadingZeros - Determine the minimum number of zero bits that S is
619 /// guaranteed to begin with (at every loop iteration).
620 uint32_t GetMinLeadingZeros(const SCEVHandle &S);
622 /// GetMinSignBits - Determine the minimum number of sign bits that S is
623 /// guaranteed to begin with.
624 uint32_t GetMinSignBits(const SCEVHandle &S);
626 virtual bool runOnFunction(Function &F);
627 virtual void releaseMemory();
628 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
629 void print(raw_ostream &OS, const Module* = 0) const;
630 virtual void print(std::ostream &OS, const Module* = 0) const;
631 void print(std::ostream *OS, const Module* M = 0) const {
632 if (OS) print(*OS, M);