ScalarEvolution.h revision ab37f50838350e1104579fbd1f7c8820473485a5
1//===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// The ScalarEvolution class is an LLVM pass which can be used to analyze and
11// categorize 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.
15//
16// This analysis is primarily useful for induction variable substitution and
17// strength reduction.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
22#define LLVM_ANALYSIS_SCALAREVOLUTION_H
23
24#include "llvm/Pass.h"
25#include "llvm/Instructions.h"
26#include "llvm/Function.h"
27#include "llvm/System/DataTypes.h"
28#include "llvm/Support/ValueHandle.h"
29#include "llvm/Support/Allocator.h"
30#include "llvm/Support/ConstantRange.h"
31#include "llvm/ADT/FoldingSet.h"
32#include "llvm/ADT/DenseMap.h"
33#include <map>
34
35namespace llvm {
36  class APInt;
37  class Constant;
38  class ConstantInt;
39  class DominatorTree;
40  class Type;
41  class ScalarEvolution;
42  class TargetData;
43  class LLVMContext;
44  class Loop;
45  class LoopInfo;
46  class Operator;
47  class SCEVUnknown;
48
49  /// SCEV - This class represents an analyzed expression in the program.  These
50  /// are opaque objects that the client is not allowed to do much with
51  /// directly.
52  ///
53  class SCEV : public FoldingSetNode {
54    /// FastID - A reference to an Interned FoldingSetNodeID for this node.
55    /// The ScalarEvolution's BumpPtrAllocator holds the data.
56    FoldingSetNodeIDRef FastID;
57
58    // The SCEV baseclass this node corresponds to
59    const unsigned short SCEVType;
60
61  protected:
62    /// SubclassData - This field is initialized to zero and may be used in
63    /// subclasses to store miscellaneous information.
64    unsigned short SubclassData;
65
66  private:
67    SCEV(const SCEV &);            // DO NOT IMPLEMENT
68    void operator=(const SCEV &);  // DO NOT IMPLEMENT
69  protected:
70    virtual ~SCEV();
71  public:
72    explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) :
73      FastID(ID), SCEVType(SCEVTy), SubclassData(0) {}
74
75    unsigned getSCEVType() const { return SCEVType; }
76
77    /// Profile - FoldingSet support.
78    void Profile(FoldingSetNodeID& ID) { ID = FastID; }
79
80    /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
81    /// the specified loop.
82    virtual bool isLoopInvariant(const Loop *L) const = 0;
83
84    /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
85    /// known way in the specified loop.  This property being true implies that
86    /// the value is variant in the loop AND that we can emit an expression to
87    /// compute the value of the expression at any particular loop iteration.
88    virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
89
90    /// getType - Return the LLVM type of this SCEV expression.
91    ///
92    virtual const Type *getType() const = 0;
93
94    /// isZero - Return true if the expression is a constant zero.
95    ///
96    bool isZero() const;
97
98    /// isOne - Return true if the expression is a constant one.
99    ///
100    bool isOne() const;
101
102    /// isAllOnesValue - Return true if the expression is a constant
103    /// all-ones value.
104    ///
105    bool isAllOnesValue() const;
106
107    /// hasOperand - Test whether this SCEV has Op as a direct or
108    /// indirect operand.
109    virtual bool hasOperand(const SCEV *Op) const = 0;
110
111    /// dominates - Return true if elements that makes up this SCEV dominates
112    /// the specified basic block.
113    virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0;
114
115    /// properlyDominates - Return true if elements that makes up this SCEV
116    /// properly dominate the specified basic block.
117    virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const = 0;
118
119    /// print - Print out the internal representation of this scalar to the
120    /// specified stream.  This should really only be used for debugging
121    /// purposes.
122    virtual void print(raw_ostream &OS) const = 0;
123
124    /// dump - This method is used for debugging.
125    ///
126    void dump() const;
127  };
128
129  inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
130    S.print(OS);
131    return OS;
132  }
133
134  /// SCEVCouldNotCompute - An object of this class is returned by queries that
135  /// could not be answered.  For example, if you ask for the number of
136  /// iterations of a linked-list traversal loop, you will get one of these.
137  /// None of the standard SCEV operations are valid on this class, it is just a
138  /// marker.
139  struct SCEVCouldNotCompute : public SCEV {
140    SCEVCouldNotCompute();
141
142    // None of these methods are valid for this object.
143    virtual bool isLoopInvariant(const Loop *L) const;
144    virtual const Type *getType() const;
145    virtual bool hasComputableLoopEvolution(const Loop *L) const;
146    virtual void print(raw_ostream &OS) const;
147    virtual bool hasOperand(const SCEV *Op) const;
148
149    virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
150      return true;
151    }
152
153    virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
154      return true;
155    }
156
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);
160  };
161
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.
165  ///
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 {
170      ScalarEvolution *SE;
171      virtual void deleted();
172      virtual void allUsesReplacedWith(Value *New);
173    public:
174      SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0);
175    };
176
177    friend class SCEVCallbackVH;
178    friend class SCEVExpander;
179    friend class SCEVUnknown;
180
181    /// F - The function we are analyzing.
182    ///
183    Function *F;
184
185    /// LI - The loop information for the function we are currently analyzing.
186    ///
187    LoopInfo *LI;
188
189    /// TD - The target data information for the target we are targeting.
190    ///
191    TargetData *TD;
192
193    /// DT - The dominator tree.
194    ///
195    DominatorTree *DT;
196
197    /// CouldNotCompute - This SCEV is used to represent unknown trip
198    /// counts and things.
199    SCEVCouldNotCompute CouldNotCompute;
200
201    /// Scalars - This is a cache of the scalars we have analyzed so far.
202    ///
203    std::map<SCEVCallbackVH, const SCEV *> Scalars;
204
205    /// BackedgeTakenInfo - Information about the backedge-taken count
206    /// of a loop. This currently includes an exact count and a maximum count.
207    ///
208    struct BackedgeTakenInfo {
209      /// Exact - An expression indicating the exact backedge-taken count of
210      /// the loop if it is known, or a SCEVCouldNotCompute otherwise.
211      const SCEV *Exact;
212
213      /// Max - An expression indicating the least maximum backedge-taken
214      /// count of the loop that is known, or a SCEVCouldNotCompute.
215      const SCEV *Max;
216
217      /*implicit*/ BackedgeTakenInfo(const SCEV *exact) :
218        Exact(exact), Max(exact) {}
219
220      BackedgeTakenInfo(const SCEV *exact, const SCEV *max) :
221        Exact(exact), Max(max) {}
222
223      /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any
224      /// computed information, or whether it's all SCEVCouldNotCompute
225      /// values.
226      bool hasAnyInfo() const {
227        return !isa<SCEVCouldNotCompute>(Exact) ||
228               !isa<SCEVCouldNotCompute>(Max);
229      }
230    };
231
232    /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for
233    /// this function as they are computed.
234    std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
235
236    /// ConstantEvolutionLoopExitValue - This map contains entries for all of
237    /// the PHI instructions that we attempt to compute constant evolutions for.
238    /// This allows us to avoid potentially expensive recomputation of these
239    /// properties.  An instruction maps to null if we are unable to compute its
240    /// exit value.
241    std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
242
243    /// ValuesAtScopes - This map contains entries for all the expressions
244    /// that we attempt to compute getSCEVAtScope information for, which can
245    /// be expensive in extreme cases.
246    std::map<const SCEV *,
247             std::map<const Loop *, const SCEV *> > ValuesAtScopes;
248
249    /// createSCEV - We know that there is no SCEV for the specified value.
250    /// Analyze the expression.
251    const SCEV *createSCEV(Value *V);
252
253    /// createNodeForPHI - Provide the special handling we need to analyze PHI
254    /// SCEVs.
255    const SCEV *createNodeForPHI(PHINode *PN);
256
257    /// createNodeForGEP - Provide the special handling we need to analyze GEP
258    /// SCEVs.
259    const SCEV *createNodeForGEP(GEPOperator *GEP);
260
261    /// computeSCEVAtScope - Implementation code for getSCEVAtScope; called
262    /// at most once for each SCEV+Loop pair.
263    ///
264    const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
265
266    /// ForgetSymbolicValue - This looks up computed SCEV values for all
267    /// instructions that depend on the given instruction and removes them from
268    /// the Scalars map if they reference SymName. This is used during PHI
269    /// resolution.
270    void ForgetSymbolicName(Instruction *I, const SCEV *SymName);
271
272    /// getBECount - Subtract the end and start values and divide by the step,
273    /// rounding up, to get the number of times the backedge is executed. Return
274    /// CouldNotCompute if an intermediate computation overflows.
275    const SCEV *getBECount(const SCEV *Start,
276                           const SCEV *End,
277                           const SCEV *Step,
278                           bool NoWrap);
279
280    /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given
281    /// loop, lazily computing new values if the loop hasn't been analyzed
282    /// yet.
283    const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
284
285    /// ComputeBackedgeTakenCount - Compute the number of times the specified
286    /// loop will iterate.
287    BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L);
288
289    /// ComputeBackedgeTakenCountFromExit - Compute the number of times the
290    /// backedge of the specified loop will execute if it exits via the
291    /// specified block.
292    BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L,
293                                                      BasicBlock *ExitingBlock);
294
295    /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
296    /// backedge of the specified loop will execute if its exit condition
297    /// were a conditional branch of ExitCond, TBB, and FBB.
298    BackedgeTakenInfo
299      ComputeBackedgeTakenCountFromExitCond(const Loop *L,
300                                            Value *ExitCond,
301                                            BasicBlock *TBB,
302                                            BasicBlock *FBB);
303
304    /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of
305    /// times the backedge of the specified loop will execute if its exit
306    /// condition were a conditional branch of the ICmpInst ExitCond, TBB,
307    /// and FBB.
308    BackedgeTakenInfo
309      ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
310                                                ICmpInst *ExitCond,
311                                                BasicBlock *TBB,
312                                                BasicBlock *FBB);
313
314    /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
315    /// of 'icmp op load X, cst', try to see if we can compute the
316    /// backedge-taken count.
317    BackedgeTakenInfo
318      ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
319                                                   Constant *RHS,
320                                                   const Loop *L,
321                                                   ICmpInst::Predicate p);
322
323    /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute
324    /// a constant number of times (the condition evolves only from constants),
325    /// try to evaluate a few iterations of the loop until we get the exit
326    /// condition gets a value of ExitWhen (true or false).  If we cannot
327    /// evaluate the backedge-taken count of the loop, return CouldNotCompute.
328    const SCEV *ComputeBackedgeTakenCountExhaustively(const Loop *L,
329                                                      Value *Cond,
330                                                      bool ExitWhen);
331
332    /// HowFarToZero - Return the number of times a backedge comparing the
333    /// specified value to zero will execute.  If not computable, return
334    /// CouldNotCompute.
335    BackedgeTakenInfo HowFarToZero(const SCEV *V, const Loop *L);
336
337    /// HowFarToNonZero - Return the number of times a backedge checking the
338    /// specified value for nonzero will execute.  If not computable, return
339    /// CouldNotCompute.
340    BackedgeTakenInfo HowFarToNonZero(const SCEV *V, const Loop *L);
341
342    /// HowManyLessThans - Return the number of times a backedge containing the
343    /// specified less-than comparison will execute.  If not computable, return
344    /// CouldNotCompute. isSigned specifies whether the less-than is signed.
345    BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
346                                       const Loop *L, bool isSigned);
347
348    /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
349    /// (which may not be an immediate predecessor) which has exactly one
350    /// successor from which BB is reachable, or null if no such block is
351    /// found.
352    std::pair<BasicBlock *, BasicBlock *>
353    getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
354
355    /// isImpliedCond - Test whether the condition described by Pred, LHS,
356    /// and RHS is true whenever the given Cond value evaluates to true.
357    bool isImpliedCond(Value *Cond, ICmpInst::Predicate Pred,
358                       const SCEV *LHS, const SCEV *RHS,
359                       bool Inverse);
360
361    /// isImpliedCondOperands - Test whether the condition described by Pred,
362    /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS,
363    /// and FoundRHS is true.
364    bool isImpliedCondOperands(ICmpInst::Predicate Pred,
365                               const SCEV *LHS, const SCEV *RHS,
366                               const SCEV *FoundLHS, const SCEV *FoundRHS);
367
368    /// isImpliedCondOperandsHelper - Test whether the condition described by
369    /// Pred, LHS, and RHS is true whenever the condition described by Pred,
370    /// FoundLHS, and FoundRHS is true.
371    bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
372                                     const SCEV *LHS, const SCEV *RHS,
373                                     const SCEV *FoundLHS, const SCEV *FoundRHS);
374
375    /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
376    /// in the header of its containing loop, we know the loop executes a
377    /// constant number of times, and the PHI node is just a recurrence
378    /// involving constants, fold it.
379    Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
380                                                const Loop *L);
381
382    /// isKnownPredicateWithRanges - Test if the given expression is known to
383    /// satisfy the condition described by Pred and the known constant ranges
384    /// of LHS and RHS.
385    ///
386    bool isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
387                                    const SCEV *LHS, const SCEV *RHS);
388
389  public:
390    static char ID; // Pass identification, replacement for typeid
391    ScalarEvolution();
392
393    LLVMContext &getContext() const { return F->getContext(); }
394
395    /// isSCEVable - Test if values of the given type are analyzable within
396    /// the SCEV framework. This primarily includes integer types, and it
397    /// can optionally include pointer types if the ScalarEvolution class
398    /// has access to target-specific information.
399    bool isSCEVable(const Type *Ty) const;
400
401    /// getTypeSizeInBits - Return the size in bits of the specified type,
402    /// for which isSCEVable must return true.
403    uint64_t getTypeSizeInBits(const Type *Ty) const;
404
405    /// getEffectiveSCEVType - Return a type with the same bitwidth as
406    /// the given type and which represents how SCEV will treat the given
407    /// type, for which isSCEVable must return true. For pointer types,
408    /// this is the pointer-sized integer type.
409    const Type *getEffectiveSCEVType(const Type *Ty) const;
410
411    /// getSCEV - Return a SCEV expression for the full generality of the
412    /// specified expression.
413    const SCEV *getSCEV(Value *V);
414
415    const SCEV *getConstant(ConstantInt *V);
416    const SCEV *getConstant(const APInt& Val);
417    const SCEV *getConstant(const Type *Ty, uint64_t V, bool isSigned = false);
418    const SCEV *getTruncateExpr(const SCEV *Op, const Type *Ty);
419    const SCEV *getZeroExtendExpr(const SCEV *Op, const Type *Ty);
420    const SCEV *getSignExtendExpr(const SCEV *Op, const Type *Ty);
421    const SCEV *getAnyExtendExpr(const SCEV *Op, const Type *Ty);
422    const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
423                           bool HasNUW = false, bool HasNSW = false);
424    const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
425                           bool HasNUW = false, bool HasNSW = false) {
426      SmallVector<const SCEV *, 2> Ops;
427      Ops.push_back(LHS);
428      Ops.push_back(RHS);
429      return getAddExpr(Ops, HasNUW, HasNSW);
430    }
431    const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1,
432                           const SCEV *Op2,
433                           bool HasNUW = false, bool HasNSW = false) {
434      SmallVector<const SCEV *, 3> Ops;
435      Ops.push_back(Op0);
436      Ops.push_back(Op1);
437      Ops.push_back(Op2);
438      return getAddExpr(Ops, HasNUW, HasNSW);
439    }
440    const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
441                           bool HasNUW = false, bool HasNSW = false);
442    const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
443                           bool HasNUW = false, bool HasNSW = false) {
444      SmallVector<const SCEV *, 2> Ops;
445      Ops.push_back(LHS);
446      Ops.push_back(RHS);
447      return getMulExpr(Ops, HasNUW, HasNSW);
448    }
449    const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
450    const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
451                              const Loop *L,
452                              bool HasNUW = false, bool HasNSW = false);
453    const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
454                              const Loop *L,
455                              bool HasNUW = false, bool HasNSW = false);
456    const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
457                              const Loop *L,
458                              bool HasNUW = false, bool HasNSW = false) {
459      SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
460      return getAddRecExpr(NewOp, L, HasNUW, HasNSW);
461    }
462    const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
463    const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
464    const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
465    const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
466    const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
467    const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
468    const SCEV *getUnknown(Value *V);
469    const SCEV *getCouldNotCompute();
470
471    /// getSizeOfExpr - Return an expression for sizeof on the given type.
472    ///
473    const SCEV *getSizeOfExpr(const Type *AllocTy);
474
475    /// getAlignOfExpr - Return an expression for alignof on the given type.
476    ///
477    const SCEV *getAlignOfExpr(const Type *AllocTy);
478
479    /// getOffsetOfExpr - Return an expression for offsetof on the given field.
480    ///
481    const SCEV *getOffsetOfExpr(const StructType *STy, unsigned FieldNo);
482
483    /// getOffsetOfExpr - Return an expression for offsetof on the given field.
484    ///
485    const SCEV *getOffsetOfExpr(const Type *CTy, Constant *FieldNo);
486
487    /// getNegativeSCEV - Return the SCEV object corresponding to -V.
488    ///
489    const SCEV *getNegativeSCEV(const SCEV *V);
490
491    /// getNotSCEV - Return the SCEV object corresponding to ~V.
492    ///
493    const SCEV *getNotSCEV(const SCEV *V);
494
495    /// getMinusSCEV - Return LHS-RHS.
496    ///
497    const SCEV *getMinusSCEV(const SCEV *LHS,
498                             const SCEV *RHS);
499
500    /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
501    /// of the input value to the specified type.  If the type must be
502    /// extended, it is zero extended.
503    const SCEV *getTruncateOrZeroExtend(const SCEV *V, const Type *Ty);
504
505    /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
506    /// of the input value to the specified type.  If the type must be
507    /// extended, it is sign extended.
508    const SCEV *getTruncateOrSignExtend(const SCEV *V, const Type *Ty);
509
510    /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of
511    /// the input value to the specified type.  If the type must be extended,
512    /// it is zero extended.  The conversion must not be narrowing.
513    const SCEV *getNoopOrZeroExtend(const SCEV *V, const Type *Ty);
514
515    /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of
516    /// the input value to the specified type.  If the type must be extended,
517    /// it is sign extended.  The conversion must not be narrowing.
518    const SCEV *getNoopOrSignExtend(const SCEV *V, const Type *Ty);
519
520    /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
521    /// the input value to the specified type. If the type must be extended,
522    /// it is extended with unspecified bits. The conversion must not be
523    /// narrowing.
524    const SCEV *getNoopOrAnyExtend(const SCEV *V, const Type *Ty);
525
526    /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
527    /// input value to the specified type.  The conversion must not be
528    /// widening.
529    const SCEV *getTruncateOrNoop(const SCEV *V, const Type *Ty);
530
531    /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
532    /// the types using zero-extension, and then perform a umax operation
533    /// with them.
534    const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS,
535                                           const SCEV *RHS);
536
537    /// getUMinFromMismatchedTypes - Promote the operands to the wider of
538    /// the types using zero-extension, and then perform a umin operation
539    /// with them.
540    const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS,
541                                           const SCEV *RHS);
542
543    /// getSCEVAtScope - Return a SCEV expression for the specified value
544    /// at the specified scope in the program.  The L value specifies a loop
545    /// nest to evaluate the expression at, where null is the top-level or a
546    /// specified loop is immediately inside of the loop.
547    ///
548    /// This method can be used to compute the exit value for a variable defined
549    /// in a loop by querying what the value will hold in the parent loop.
550    ///
551    /// In the case that a relevant loop exit value cannot be computed, the
552    /// original value V is returned.
553    const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
554
555    /// getSCEVAtScope - This is a convenience function which does
556    /// getSCEVAtScope(getSCEV(V), L).
557    const SCEV *getSCEVAtScope(Value *V, const Loop *L);
558
559    /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected
560    /// by a conditional between LHS and RHS.  This is used to help avoid max
561    /// expressions in loop trip counts, and to eliminate casts.
562    bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
563                                  const SCEV *LHS, const SCEV *RHS);
564
565    /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
566    /// protected by a conditional between LHS and RHS.  This is used to
567    /// to eliminate casts.
568    bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
569                                     const SCEV *LHS, const SCEV *RHS);
570
571    /// getBackedgeTakenCount - If the specified loop has a predictable
572    /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
573    /// object. The backedge-taken count is the number of times the loop header
574    /// will be branched to from within the loop. This is one less than the
575    /// trip count of the loop, since it doesn't count the first iteration,
576    /// when the header is branched to from outside the loop.
577    ///
578    /// Note that it is not valid to call this method on a loop without a
579    /// loop-invariant backedge-taken count (see
580    /// hasLoopInvariantBackedgeTakenCount).
581    ///
582    const SCEV *getBackedgeTakenCount(const Loop *L);
583
584    /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
585    /// return the least SCEV value that is known never to be less than the
586    /// actual backedge taken count.
587    const SCEV *getMaxBackedgeTakenCount(const Loop *L);
588
589    /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
590    /// has an analyzable loop-invariant backedge-taken count.
591    bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
592
593    /// forgetLoop - This method should be called by the client when it has
594    /// changed a loop in a way that may effect ScalarEvolution's ability to
595    /// compute a trip count, or if the loop is deleted.
596    void forgetLoop(const Loop *L);
597
598    /// forgetValue - This method should be called by the client when it has
599    /// changed a value in a way that may effect its value, or which may
600    /// disconnect it from a def-use chain linking it to a loop.
601    void forgetValue(Value *V);
602
603    /// GetMinTrailingZeros - Determine the minimum number of zero bits that S
604    /// is guaranteed to end in (at every loop iteration).  It is, at the same
605    /// time, the minimum number of times S is divisible by 2.  For example,
606    /// given {4,+,8} it returns 2.  If S is guaranteed to be 0, it returns the
607    /// bitwidth of S.
608    uint32_t GetMinTrailingZeros(const SCEV *S);
609
610    /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
611    ///
612    ConstantRange getUnsignedRange(const SCEV *S);
613
614    /// getSignedRange - Determine the signed range for a particular SCEV.
615    ///
616    ConstantRange getSignedRange(const SCEV *S);
617
618    /// isKnownNegative - Test if the given expression is known to be negative.
619    ///
620    bool isKnownNegative(const SCEV *S);
621
622    /// isKnownPositive - Test if the given expression is known to be positive.
623    ///
624    bool isKnownPositive(const SCEV *S);
625
626    /// isKnownNonNegative - Test if the given expression is known to be
627    /// non-negative.
628    ///
629    bool isKnownNonNegative(const SCEV *S);
630
631    /// isKnownNonPositive - Test if the given expression is known to be
632    /// non-positive.
633    ///
634    bool isKnownNonPositive(const SCEV *S);
635
636    /// isKnownNonZero - Test if the given expression is known to be
637    /// non-zero.
638    ///
639    bool isKnownNonZero(const SCEV *S);
640
641    /// isKnownPredicate - Test if the given expression is known to satisfy
642    /// the condition described by Pred, LHS, and RHS.
643    ///
644    bool isKnownPredicate(ICmpInst::Predicate Pred,
645                          const SCEV *LHS, const SCEV *RHS);
646
647    /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with
648    /// predicate Pred. Return true iff any changes were made. If the
649    /// operands are provably equal or inequal, LHS and RHS are set to
650    /// the same value and Pred is set to either ICMP_EQ or ICMP_NE.
651    ///
652    bool SimplifyICmpOperands(ICmpInst::Predicate &Pred,
653                              const SCEV *&LHS,
654                              const SCEV *&RHS);
655
656    virtual bool runOnFunction(Function &F);
657    virtual void releaseMemory();
658    virtual void getAnalysisUsage(AnalysisUsage &AU) const;
659    virtual void print(raw_ostream &OS, const Module* = 0) const;
660
661  private:
662    FoldingSet<SCEV> UniqueSCEVs;
663    BumpPtrAllocator SCEVAllocator;
664
665    /// FirstUnknown - The head of a linked list of all SCEVUnknown
666    /// values that have been allocated. This is used by releaseMemory
667    /// to locate them all and call their destructors.
668    SCEVUnknown *FirstUnknown;
669  };
670}
671
672#endif
673