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