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