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/Instructions.h"
26 #include "llvm/Support/DataTypes.h"
27 #include "llvm/Support/ValueHandle.h"
28 #include "llvm/Support/Allocator.h"
29 #include "llvm/Support/ConstantRange.h"
30 #include "llvm/ADT/FoldingSet.h"
31 #include "llvm/ADT/DenseMap.h"
41 class ScalarEvolution;
48 /// SCEV - This class represents an analyzed expression in the program. These
49 /// are opaque objects that the client is not allowed to do much with
52 class SCEV : public FastFoldingSetNode {
53 // The SCEV baseclass this node corresponds to
54 const unsigned short SCEVType;
57 /// SubclassData - This field is initialized to zero and may be used in
58 /// subclasses to store miscelaneous information.
59 unsigned short SubclassData;
62 SCEV(const SCEV &); // DO NOT IMPLEMENT
63 void operator=(const SCEV &); // DO NOT IMPLEMENT
67 explicit SCEV(const FoldingSetNodeID &ID, unsigned SCEVTy) :
68 FastFoldingSetNode(ID), SCEVType(SCEVTy), SubclassData(0) {}
70 unsigned getSCEVType() const { return SCEVType; }
72 /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
73 /// the specified loop.
74 virtual bool isLoopInvariant(const Loop *L) const = 0;
76 /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
77 /// known way in the specified loop. This property being true implies that
78 /// the value is variant in the loop AND that we can emit an expression to
79 /// compute the value of the expression at any particular loop iteration.
80 virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
82 /// getType - Return the LLVM type of this SCEV expression.
84 virtual const Type *getType() const = 0;
86 /// isZero - Return true if the expression is a constant zero.
90 /// isOne - Return true if the expression is a constant one.
94 /// isAllOnesValue - Return true if the expression is a constant
97 bool isAllOnesValue() const;
99 /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
100 /// the symbolic value "Sym", construct and return a new SCEV that produces
101 /// the same value, but which uses the concrete value Conc instead of the
102 /// symbolic value. If this SCEV does not use the symbolic value, it
105 replaceSymbolicValuesWithConcrete(const SCEV *Sym,
107 ScalarEvolution &SE) const = 0;
109 /// dominates - Return true if elements that makes up this SCEV dominates
110 /// the specified basic block.
111 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0;
113 /// print - Print out the internal representation of this scalar to the
114 /// specified stream. This should really only be used for debugging
116 virtual void print(raw_ostream &OS) const = 0;
117 void print(std::ostream &OS) const;
118 void print(std::ostream *OS) const { if (OS) print(*OS); }
120 /// dump - This method is used for debugging.
125 inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
130 inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
135 /// SCEVCouldNotCompute - An object of this class is returned by queries that
136 /// could not be answered. For example, if you ask for the number of
137 /// iterations of a linked-list traversal loop, you will get one of these.
138 /// None of the standard SCEV operations are valid on this class, it is just a
140 struct SCEVCouldNotCompute : public SCEV {
141 SCEVCouldNotCompute();
143 // None of these methods are valid for this object.
144 virtual bool isLoopInvariant(const Loop *L) const;
145 virtual const Type *getType() const;
146 virtual bool hasComputableLoopEvolution(const Loop *L) const;
147 virtual void print(raw_ostream &OS) const;
149 replaceSymbolicValuesWithConcrete(const SCEV *Sym,
151 ScalarEvolution &SE) const;
153 virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
157 /// Methods for support type inquiry through isa, cast, and dyn_cast:
158 static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
159 static bool classof(const SCEV *S);
162 /// ScalarEvolution - This class is the main scalar evolution driver. Because
163 /// client code (intentionally) can't do much with the SCEV objects directly,
164 /// they must ask this class for services.
166 class ScalarEvolution : public FunctionPass {
167 /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be
168 /// notified whenever a Value is deleted.
169 class SCEVCallbackVH : public CallbackVH {
171 virtual void deleted();
172 virtual void allUsesReplacedWith(Value *New);
174 SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0);
177 friend class SCEVCallbackVH;
178 friend struct SCEVExpander;
180 /// F - The function we are analyzing.
184 /// LI - The loop information for the function we are currently analyzing.
188 /// TD - The target data information for the target we are targetting.
192 /// CouldNotCompute - This SCEV is used to represent unknown trip
193 /// counts and things.
194 SCEVCouldNotCompute CouldNotCompute;
196 /// Scalars - This is a cache of the scalars we have analyzed so far.
198 std::map<SCEVCallbackVH, const SCEV *> Scalars;
200 /// BackedgeTakenInfo - Information about the backedge-taken count
201 /// of a loop. This currently inclues an exact count and a maximum count.
203 struct BackedgeTakenInfo {
204 /// Exact - An expression indicating the exact backedge-taken count of
205 /// the loop if it is known, or a SCEVCouldNotCompute otherwise.
208 /// Max - An expression indicating the least maximum backedge-taken
209 /// count of the loop that is known, or a SCEVCouldNotCompute.
212 /*implicit*/ BackedgeTakenInfo(const SCEV *exact) :
213 Exact(exact), Max(exact) {}
215 BackedgeTakenInfo(const SCEV *exact, const SCEV *max) :
216 Exact(exact), Max(max) {}
218 /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any
219 /// computed information, or whether it's all SCEVCouldNotCompute
221 bool hasAnyInfo() const {
222 return !isa<SCEVCouldNotCompute>(Exact) ||
223 !isa<SCEVCouldNotCompute>(Max);
227 /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for
228 /// this function as they are computed.
229 std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
231 /// ConstantEvolutionLoopExitValue - This map contains entries for all of
232 /// the PHI instructions that we attempt to compute constant evolutions for.
233 /// This allows us to avoid potentially expensive recomputation of these
234 /// properties. An instruction maps to null if we are unable to compute its
236 std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
238 /// ValuesAtScopes - This map contains entries for all the instructions
239 /// that we attempt to compute getSCEVAtScope information for without
240 /// using SCEV techniques, which can be expensive.
241 std::map<Instruction *, std::map<const Loop *, Constant *> > ValuesAtScopes;
243 /// createSCEV - We know that there is no SCEV for the specified value.
244 /// Analyze the expression.
245 const SCEV *createSCEV(Value *V);
247 /// createNodeForPHI - Provide the special handling we need to analyze PHI
249 const SCEV *createNodeForPHI(PHINode *PN);
251 /// createNodeForGEP - Provide the special handling we need to analyze GEP
253 const SCEV *createNodeForGEP(Operator *GEP);
255 /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value
256 /// for the specified instruction and replaces any references to the
257 /// symbolic value SymName with the specified value. This is used during
259 void ReplaceSymbolicValueWithConcrete(Instruction *I,
263 /// getBECount - Subtract the end and start values and divide by the step,
264 /// rounding up, to get the number of times the backedge is executed. Return
265 /// CouldNotCompute if an intermediate computation overflows.
266 const SCEV *getBECount(const SCEV *Start,
270 /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given
271 /// loop, lazily computing new values if the loop hasn't been analyzed
273 const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
275 /// ComputeBackedgeTakenCount - Compute the number of times the specified
276 /// loop will iterate.
277 BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L);
279 /// ComputeBackedgeTakenCountFromExit - Compute the number of times the
280 /// backedge of the specified loop will execute if it exits via the
282 BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L,
283 BasicBlock *ExitingBlock);
285 /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
286 /// backedge of the specified loop will execute if its exit condition
287 /// were a conditional branch of ExitCond, TBB, and FBB.
289 ComputeBackedgeTakenCountFromExitCond(const Loop *L,
294 /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of
295 /// times the backedge of the specified loop will execute if its exit
296 /// condition were a conditional branch of the ICmpInst ExitCond, TBB,
299 ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
304 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
305 /// of 'icmp op load X, cst', try to see if we can compute the trip count.
307 ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
310 ICmpInst::Predicate p);
312 /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute
313 /// a constant number of times (the condition evolves only from constants),
314 /// try to evaluate a few iterations of the loop until we get the exit
315 /// condition gets a value of ExitWhen (true or false). If we cannot
316 /// evaluate the trip count of the loop, return CouldNotCompute.
317 const SCEV *ComputeBackedgeTakenCountExhaustively(const Loop *L,
321 /// HowFarToZero - Return the number of times a backedge comparing the
322 /// specified value to zero will execute. If not computable, return
324 const SCEV *HowFarToZero(const SCEV *V, const Loop *L);
326 /// HowFarToNonZero - Return the number of times a backedge checking the
327 /// specified value for nonzero will execute. If not computable, return
329 const SCEV *HowFarToNonZero(const SCEV *V, const Loop *L);
331 /// HowManyLessThans - Return the number of times a backedge containing the
332 /// specified less-than comparison will execute. If not computable, return
333 /// CouldNotCompute. isSigned specifies whether the less-than is signed.
334 BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
335 const Loop *L, bool isSigned);
337 /// getLoopPredecessor - If the given loop's header has exactly one unique
338 /// predecessor outside the loop, return it. Otherwise return null.
339 BasicBlock *getLoopPredecessor(const Loop *L);
341 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
342 /// (which may not be an immediate predecessor) which has exactly one
343 /// successor from which BB is reachable, or null if no such block is
345 BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
347 /// isImpliedCond - Test whether the condition described by Pred, LHS,
348 /// and RHS is true whenever the given Cond value evaluates to true.
349 bool isImpliedCond(Value *Cond, ICmpInst::Predicate Pred,
350 const SCEV *LHS, const SCEV *RHS,
353 /// isImpliedCondOperands - Test whether the condition described by Pred,
354 /// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS,
355 /// and FoundRHS is true.
356 bool isImpliedCondOperands(ICmpInst::Predicate Pred,
357 const SCEV *LHS, const SCEV *RHS,
358 const SCEV *FoundLHS, const SCEV *FoundRHS);
360 /// isImpliedCondOperandsHelper - Test whether the condition described by
361 /// Pred, LHS, and RHS is true whenever the condition desribed by Pred,
362 /// FoundLHS, and FoundRHS is true.
363 bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
364 const SCEV *LHS, const SCEV *RHS,
365 const SCEV *FoundLHS, const SCEV *FoundRHS);
367 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
368 /// in the header of its containing loop, we know the loop executes a
369 /// constant number of times, and the PHI node is just a recurrence
370 /// involving constants, fold it.
371 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
375 static char ID; // Pass identification, replacement for typeid
378 LLVMContext &getContext() const { return F->getContext(); }
380 /// isSCEVable - Test if values of the given type are analyzable within
381 /// the SCEV framework. This primarily includes integer types, and it
382 /// can optionally include pointer types if the ScalarEvolution class
383 /// has access to target-specific information.
384 bool isSCEVable(const Type *Ty) const;
386 /// getTypeSizeInBits - Return the size in bits of the specified type,
387 /// for which isSCEVable must return true.
388 uint64_t getTypeSizeInBits(const Type *Ty) const;
390 /// getEffectiveSCEVType - Return a type with the same bitwidth as
391 /// the given type and which represents how SCEV will treat the given
392 /// type, for which isSCEVable must return true. For pointer types,
393 /// this is the pointer-sized integer type.
394 const Type *getEffectiveSCEVType(const Type *Ty) const;
396 /// getSCEV - Return a SCEV expression handle for the full generality of the
397 /// specified expression.
398 const SCEV *getSCEV(Value *V);
400 const SCEV *getConstant(ConstantInt *V);
401 const SCEV *getConstant(const APInt& Val);
402 const SCEV *getConstant(const Type *Ty, uint64_t V, bool isSigned = false);
403 const SCEV *getTruncateExpr(const SCEV *Op, const Type *Ty);
404 const SCEV *getZeroExtendExpr(const SCEV *Op, const Type *Ty);
405 const SCEV *getSignExtendExpr(const SCEV *Op, const Type *Ty);
406 const SCEV *getAnyExtendExpr(const SCEV *Op, const Type *Ty);
407 const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops);
408 const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS) {
409 SmallVector<const SCEV *, 2> Ops;
412 return getAddExpr(Ops);
414 const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1,
416 SmallVector<const SCEV *, 3> Ops;
420 return getAddExpr(Ops);
422 const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops);
423 const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS) {
424 SmallVector<const SCEV *, 2> Ops;
427 return getMulExpr(Ops);
429 const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
430 const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
432 const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
434 const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
436 SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
437 return getAddRecExpr(NewOp, L);
439 const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
440 const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
441 const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
442 const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
443 const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
444 const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
445 const SCEV *getUnknown(Value *V);
446 const SCEV *getCouldNotCompute();
448 /// getNegativeSCEV - Return the SCEV object corresponding to -V.
450 const SCEV *getNegativeSCEV(const SCEV *V);
452 /// getNotSCEV - Return the SCEV object corresponding to ~V.
454 const SCEV *getNotSCEV(const SCEV *V);
456 /// getMinusSCEV - Return LHS-RHS.
458 const SCEV *getMinusSCEV(const SCEV *LHS,
461 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
462 /// of the input value to the specified type. If the type must be
463 /// extended, it is zero extended.
464 const SCEV *getTruncateOrZeroExtend(const SCEV *V, const Type *Ty);
466 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
467 /// of the input value to the specified type. If the type must be
468 /// extended, it is sign extended.
469 const SCEV *getTruncateOrSignExtend(const SCEV *V, const Type *Ty);
471 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of
472 /// the input value to the specified type. If the type must be extended,
473 /// it is zero extended. The conversion must not be narrowing.
474 const SCEV *getNoopOrZeroExtend(const SCEV *V, const Type *Ty);
476 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of
477 /// the input value to the specified type. If the type must be extended,
478 /// it is sign extended. The conversion must not be narrowing.
479 const SCEV *getNoopOrSignExtend(const SCEV *V, const Type *Ty);
481 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
482 /// the input value to the specified type. If the type must be extended,
483 /// it is extended with unspecified bits. The conversion must not be
485 const SCEV *getNoopOrAnyExtend(const SCEV *V, const Type *Ty);
487 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
488 /// input value to the specified type. The conversion must not be
490 const SCEV *getTruncateOrNoop(const SCEV *V, const Type *Ty);
492 /// getIntegerSCEV - Given a SCEVable type, create a constant for the
493 /// specified signed integer value and return a SCEV for the constant.
494 const SCEV *getIntegerSCEV(int Val, const Type *Ty);
496 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
497 /// the types using zero-extension, and then perform a umax operation
499 const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS,
502 /// getUMinFromMismatchedTypes - Promote the operands to the wider of
503 /// the types using zero-extension, and then perform a umin operation
505 const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS,
508 /// getSCEVAtScope - Return a SCEV expression handle for the specified value
509 /// at the specified scope in the program. The L value specifies a loop
510 /// nest to evaluate the expression at, where null is the top-level or a
511 /// specified loop is immediately inside of the loop.
513 /// This method can be used to compute the exit value for a variable defined
514 /// in a loop by querying what the value will hold in the parent loop.
516 /// In the case that a relevant loop exit value cannot be computed, the
517 /// original value V is returned.
518 const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
520 /// getSCEVAtScope - This is a convenience function which does
521 /// getSCEVAtScope(getSCEV(V), L).
522 const SCEV *getSCEVAtScope(Value *V, const Loop *L);
524 /// isLoopGuardedByCond - Test whether entry to the loop is protected by
525 /// a conditional between LHS and RHS. This is used to help avoid max
526 /// expressions in loop trip counts, and to eliminate casts.
527 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
528 const SCEV *LHS, const SCEV *RHS);
530 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
531 /// protected by a conditional between LHS and RHS. This is used to
532 /// to eliminate casts.
533 bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
534 const SCEV *LHS, const SCEV *RHS);
536 /// getBackedgeTakenCount - If the specified loop has a predictable
537 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
538 /// object. The backedge-taken count is the number of times the loop header
539 /// will be branched to from within the loop. This is one less than the
540 /// trip count of the loop, since it doesn't count the first iteration,
541 /// when the header is branched to from outside the loop.
543 /// Note that it is not valid to call this method on a loop without a
544 /// loop-invariant backedge-taken count (see
545 /// hasLoopInvariantBackedgeTakenCount).
547 const SCEV *getBackedgeTakenCount(const Loop *L);
549 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
550 /// return the least SCEV value that is known never to be less than the
551 /// actual backedge taken count.
552 const SCEV *getMaxBackedgeTakenCount(const Loop *L);
554 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
555 /// has an analyzable loop-invariant backedge-taken count.
556 bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
558 /// forgetLoopBackedgeTakenCount - This method should be called by the
559 /// client when it has changed a loop in a way that may effect
560 /// ScalarEvolution's ability to compute a trip count, or if the loop
562 void forgetLoopBackedgeTakenCount(const Loop *L);
564 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S
565 /// is guaranteed to end in (at every loop iteration). It is, at the same
566 /// time, the minimum number of times S is divisible by 2. For example,
567 /// given {4,+,8} it returns 2. If S is guaranteed to be 0, it returns the
569 uint32_t GetMinTrailingZeros(const SCEV *S);
571 /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
573 ConstantRange getUnsignedRange(const SCEV *S);
575 /// getSignedRange - Determine the signed range for a particular SCEV.
577 ConstantRange getSignedRange(const SCEV *S);
579 /// isKnownNegative - Test if the given expression is known to be negative.
581 bool isKnownNegative(const SCEV *S);
583 /// isKnownPositive - Test if the given expression is known to be positive.
585 bool isKnownPositive(const SCEV *S);
587 /// isKnownNonNegative - Test if the given expression is known to be
590 bool isKnownNonNegative(const SCEV *S);
592 /// isKnownNonPositive - Test if the given expression is known to be
595 bool isKnownNonPositive(const SCEV *S);
597 /// isKnownNonZero - Test if the given expression is known to be
600 bool isKnownNonZero(const SCEV *S);
602 /// isKnownNonZero - Test if the given expression is known to satisfy
603 /// the condition described by Pred, LHS, and RHS.
605 bool isKnownPredicate(ICmpInst::Predicate Pred,
606 const SCEV *LHS, const SCEV *RHS);
608 virtual bool runOnFunction(Function &F);
609 virtual void releaseMemory();
610 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
611 void print(raw_ostream &OS, const Module* = 0) const;
612 virtual void print(std::ostream &OS, const Module* = 0) const;
613 void print(std::ostream *OS, const Module* M = 0) const {
614 if (OS) print(*OS, M);
618 FoldingSet<SCEV> UniqueSCEVs;
619 BumpPtrAllocator SCEVAllocator;