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