[ScalarEvolution] Get rid of NDEBUG in header (correctly this time).
[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/ADT/DenseSet.h"
25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/IR/ConstantRange.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Operator.h"
30 #include "llvm/IR/PassManager.h"
31 #include "llvm/IR/ValueHandle.h"
32 #include "llvm/Pass.h"
33 #include "llvm/Support/Allocator.h"
34 #include "llvm/Support/DataTypes.h"
35 #include <map>
36
37 namespace llvm {
38   class APInt;
39   class AssumptionCache;
40   class Constant;
41   class ConstantInt;
42   class DominatorTree;
43   class Type;
44   class ScalarEvolution;
45   class DataLayout;
46   class TargetLibraryInfo;
47   class LLVMContext;
48   class Loop;
49   class LoopInfo;
50   class Operator;
51   class SCEVUnknown;
52   class SCEVAddRecExpr;
53   class SCEV;
54   template<> struct FoldingSetTrait<SCEV>;
55
56   /// This class represents an analyzed expression in the program.  These are
57   /// opaque objects that the client is not allowed to do much with directly.
58   ///
59   class SCEV : public FoldingSetNode {
60     friend struct FoldingSetTrait<SCEV>;
61
62     /// A reference to an Interned FoldingSetNodeID for this node.  The
63     /// ScalarEvolution's BumpPtrAllocator holds the data.
64     FoldingSetNodeIDRef FastID;
65
66     // The SCEV baseclass this node corresponds to
67     const unsigned short SCEVType;
68
69   protected:
70     /// This field is initialized to zero and may be used in subclasses to store
71     /// miscellaneous information.
72     unsigned short SubclassData;
73
74   private:
75     SCEV(const SCEV &) = delete;
76     void operator=(const SCEV &) = delete;
77
78   public:
79     /// NoWrapFlags are bitfield indices into SubclassData.
80     ///
81     /// Add and Mul expressions may have no-unsigned-wrap <NUW> or
82     /// no-signed-wrap <NSW> properties, which are derived from the IR
83     /// operator. NSW is a misnomer that we use to mean no signed overflow or
84     /// underflow.
85     ///
86     /// AddRec expressions may have a no-self-wraparound <NW> property if, in
87     /// the integer domain, abs(step) * max-iteration(loop) <=
88     /// unsigned-max(bitwidth).  This means that the recurrence will never reach
89     /// its start value if the step is non-zero.  Computing the same value on
90     /// each iteration is not considered wrapping, and recurrences with step = 0
91     /// are trivially <NW>.  <NW> is independent of the sign of step and the
92     /// value the add recurrence starts with.
93     ///
94     /// Note that NUW and NSW are also valid properties of a recurrence, and
95     /// either implies NW. For convenience, NW will be set for a recurrence
96     /// whenever either NUW or NSW are set.
97     enum NoWrapFlags { FlagAnyWrap = 0,          // No guarantee.
98                        FlagNW      = (1 << 0),   // No self-wrap.
99                        FlagNUW     = (1 << 1),   // No unsigned wrap.
100                        FlagNSW     = (1 << 2),   // No signed wrap.
101                        NoWrapMask  = (1 << 3) -1 };
102
103     explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) :
104       FastID(ID), SCEVType(SCEVTy), SubclassData(0) {}
105
106     unsigned getSCEVType() const { return SCEVType; }
107
108     /// Return the LLVM type of this SCEV expression.
109     ///
110     Type *getType() const;
111
112     /// Return true if the expression is a constant zero.
113     ///
114     bool isZero() const;
115
116     /// Return true if the expression is a constant one.
117     ///
118     bool isOne() const;
119
120     /// Return true if the expression is a constant all-ones value.
121     ///
122     bool isAllOnesValue() const;
123
124     /// Return true if the specified scev is negated, but not a constant.
125     bool isNonConstantNegative() const;
126
127     /// Print out the internal representation of this scalar to the specified
128     /// stream.  This should really only be used for debugging purposes.
129     void print(raw_ostream &OS) const;
130
131     /// This method is used for debugging.
132     ///
133     void dump() const;
134   };
135
136   // Specialize FoldingSetTrait for SCEV to avoid needing to compute
137   // temporary FoldingSetNodeID values.
138   template<> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> {
139     static void Profile(const SCEV &X, FoldingSetNodeID& ID) {
140       ID = X.FastID;
141     }
142     static bool Equals(const SCEV &X, const FoldingSetNodeID &ID,
143                        unsigned IDHash, FoldingSetNodeID &TempID) {
144       return ID == X.FastID;
145     }
146     static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) {
147       return X.FastID.ComputeHash();
148     }
149   };
150
151   inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
152     S.print(OS);
153     return OS;
154   }
155
156   /// An object of this class is returned by queries that could not be answered.
157   /// For example, if you ask for the number of iterations of a linked-list
158   /// traversal loop, you will get one of these.  None of the standard SCEV
159   /// operations are valid on this class, it is just a marker.
160   struct SCEVCouldNotCompute : public SCEV {
161     SCEVCouldNotCompute();
162
163     /// Methods for support type inquiry through isa, cast, and dyn_cast:
164     static bool classof(const SCEV *S);
165   };
166
167   /// The main scalar evolution driver. Because client code (intentionally)
168   /// can't do much with the SCEV objects directly, they must ask this class
169   /// for services.
170   class ScalarEvolution {
171   public:
172     /// An enum describing the relationship between a SCEV and a loop.
173     enum LoopDisposition {
174       LoopVariant,    ///< The SCEV is loop-variant (unknown).
175       LoopInvariant,  ///< The SCEV is loop-invariant.
176       LoopComputable  ///< The SCEV varies predictably with the loop.
177     };
178
179     /// An enum describing the relationship between a SCEV and a basic block.
180     enum BlockDisposition {
181       DoesNotDominateBlock,  ///< The SCEV does not dominate the block.
182       DominatesBlock,        ///< The SCEV dominates the block.
183       ProperlyDominatesBlock ///< The SCEV properly dominates the block.
184     };
185
186     /// Convenient NoWrapFlags manipulation that hides enum casts and is
187     /// visible in the ScalarEvolution name space.
188     static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
189     maskFlags(SCEV::NoWrapFlags Flags, int Mask) {
190       return (SCEV::NoWrapFlags)(Flags & Mask);
191     }
192     static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
193     setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags) {
194       return (SCEV::NoWrapFlags)(Flags | OnFlags);
195     }
196     static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
197     clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags) {
198       return (SCEV::NoWrapFlags)(Flags & ~OffFlags);
199     }
200
201   private:
202     /// A CallbackVH to arrange for ScalarEvolution to be notified whenever a
203     /// Value is deleted.
204     class SCEVCallbackVH final : public CallbackVH {
205       ScalarEvolution *SE;
206       void deleted() override;
207       void allUsesReplacedWith(Value *New) override;
208     public:
209       SCEVCallbackVH(Value *V, ScalarEvolution *SE = nullptr);
210     };
211
212     friend class SCEVCallbackVH;
213     friend class SCEVExpander;
214     friend class SCEVUnknown;
215
216     /// The function we are analyzing.
217     ///
218     Function &F;
219
220     /// The target library information for the target we are targeting.
221     ///
222     TargetLibraryInfo &TLI;
223
224     /// The tracker for @llvm.assume intrinsics in this function.
225     AssumptionCache &AC;
226
227     /// The dominator tree.
228     ///
229     DominatorTree &DT;
230
231     /// The loop information for the function we are currently analyzing.
232     ///
233     LoopInfo &LI;
234
235     /// This SCEV is used to represent unknown trip counts and things.
236     std::unique_ptr<SCEVCouldNotCompute> CouldNotCompute;
237
238     /// The typedef for ValueExprMap.
239     ///
240     typedef DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *> >
241       ValueExprMapType;
242
243     /// This is a cache of the values we have analyzed so far.
244     ///
245     ValueExprMapType ValueExprMap;
246
247     /// Mark predicate values currently being processed by isImpliedCond.
248     DenseSet<Value*> PendingLoopPredicates;
249
250     /// Set to true by isLoopBackedgeGuardedByCond when we're walking the set of
251     /// conditions dominating the backedge of a loop.
252     bool WalkingBEDominatingConds;
253
254     /// Set to true by isKnownPredicateViaSplitting when we're trying to prove a
255     /// predicate by splitting it into a set of independent predicates.
256     bool ProvingSplitPredicate;
257
258     /// Information about the number of loop iterations for which a loop exit's
259     /// branch condition evaluates to the not-taken path.  This is a temporary
260     /// pair of exact and max expressions that are eventually summarized in
261     /// ExitNotTakenInfo and BackedgeTakenInfo.
262     struct ExitLimit {
263       const SCEV *Exact;
264       const SCEV *Max;
265
266       /*implicit*/ ExitLimit(const SCEV *E) : Exact(E), Max(E) {}
267
268       ExitLimit(const SCEV *E, const SCEV *M) : Exact(E), Max(M) {}
269
270       /// Test whether this ExitLimit contains any computed information, or
271       /// whether it's all SCEVCouldNotCompute values.
272       bool hasAnyInfo() const {
273         return !isa<SCEVCouldNotCompute>(Exact) ||
274           !isa<SCEVCouldNotCompute>(Max);
275       }
276     };
277
278     /// Information about the number of times a particular loop exit may be
279     /// reached before exiting the loop.
280     struct ExitNotTakenInfo {
281       AssertingVH<BasicBlock> ExitingBlock;
282       const SCEV *ExactNotTaken;
283       PointerIntPair<ExitNotTakenInfo*, 1> NextExit;
284
285       ExitNotTakenInfo() : ExitingBlock(nullptr), ExactNotTaken(nullptr) {}
286
287       /// Return true if all loop exits are computable.
288       bool isCompleteList() const {
289         return NextExit.getInt() == 0;
290       }
291
292       void setIncomplete() { NextExit.setInt(1); }
293
294       /// Return a pointer to the next exit's not-taken info.
295       ExitNotTakenInfo *getNextExit() const {
296         return NextExit.getPointer();
297       }
298
299       void setNextExit(ExitNotTakenInfo *ENT) { NextExit.setPointer(ENT); }
300     };
301
302     /// Information about the backedge-taken count of a loop. This currently
303     /// includes an exact count and a maximum count.
304     ///
305     class BackedgeTakenInfo {
306       /// A list of computable exits and their not-taken counts.  Loops almost
307       /// never have more than one computable exit.
308       ExitNotTakenInfo ExitNotTaken;
309
310       /// An expression indicating the least maximum backedge-taken count of the
311       /// loop that is known, or a SCEVCouldNotCompute.
312       const SCEV *Max;
313
314     public:
315       BackedgeTakenInfo() : Max(nullptr) {}
316
317       /// Initialize BackedgeTakenInfo from a list of exact exit counts.
318       BackedgeTakenInfo(
319         SmallVectorImpl< std::pair<BasicBlock *, const SCEV *> > &ExitCounts,
320         bool Complete, const SCEV *MaxCount);
321
322       /// Test whether this BackedgeTakenInfo contains any computed information,
323       /// or whether it's all SCEVCouldNotCompute values.
324       bool hasAnyInfo() const {
325         return ExitNotTaken.ExitingBlock || !isa<SCEVCouldNotCompute>(Max);
326       }
327
328       /// Return an expression indicating the exact backedge-taken count of the
329       /// loop if it is known, or SCEVCouldNotCompute otherwise. This is the
330       /// number of times the loop header can be guaranteed to execute, minus
331       /// one.
332       const SCEV *getExact(ScalarEvolution *SE) const;
333
334       /// Return the number of times this loop exit may fall through to the back
335       /// edge, or SCEVCouldNotCompute. The loop is guaranteed not to exit via
336       /// this block before this number of iterations, but may exit via another
337       /// block.
338       const SCEV *getExact(BasicBlock *ExitingBlock, ScalarEvolution *SE) const;
339
340       /// Get the max backedge taken count for the loop.
341       const SCEV *getMax(ScalarEvolution *SE) const;
342
343       /// Return true if any backedge taken count expressions refer to the given
344       /// subexpression.
345       bool hasOperand(const SCEV *S, ScalarEvolution *SE) const;
346
347       /// Invalidate this result and free associated memory.
348       void clear();
349     };
350
351     /// Cache the backedge-taken count of the loops for this function as they
352     /// are computed.
353     DenseMap<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
354
355     /// This map contains entries for all of the PHI instructions that we
356     /// attempt to compute constant evolutions for.  This allows us to avoid
357     /// potentially expensive recomputation of these properties.  An instruction
358     /// maps to null if we are unable to compute its exit value.
359     DenseMap<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
360
361     /// This map contains entries for all the expressions that we attempt to
362     /// compute getSCEVAtScope information for, which can be expensive in
363     /// extreme cases.
364     DenseMap<const SCEV *,
365              SmallVector<std::pair<const Loop *, const SCEV *>, 2> > ValuesAtScopes;
366
367     /// Memoized computeLoopDisposition results.
368     DenseMap<const SCEV *,
369              SmallVector<PointerIntPair<const Loop *, 2, LoopDisposition>, 2>>
370         LoopDispositions;
371
372     /// Compute a LoopDisposition value.
373     LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L);
374
375     /// Memoized computeBlockDisposition results.
376     DenseMap<
377         const SCEV *,
378         SmallVector<PointerIntPair<const BasicBlock *, 2, BlockDisposition>, 2>>
379         BlockDispositions;
380
381     /// Compute a BlockDisposition value.
382     BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB);
383
384     /// Memoized results from getRange
385     DenseMap<const SCEV *, ConstantRange> UnsignedRanges;
386
387     /// Memoized results from getRange
388     DenseMap<const SCEV *, ConstantRange> SignedRanges;
389
390     /// Used to parameterize getRange
391     enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED };
392
393     /// Set the memoized range for the given SCEV.
394     const ConstantRange &setRange(const SCEV *S, RangeSignHint Hint,
395                                   const ConstantRange &CR) {
396       DenseMap<const SCEV *, ConstantRange> &Cache =
397           Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges;
398
399       std::pair<DenseMap<const SCEV *, ConstantRange>::iterator, bool> Pair =
400           Cache.insert(std::make_pair(S, CR));
401       if (!Pair.second)
402         Pair.first->second = CR;
403       return Pair.first->second;
404     }
405
406     /// Determine the range for a particular SCEV.
407     ConstantRange getRange(const SCEV *S, RangeSignHint Hint);
408
409     /// We know that there is no SCEV for the specified value.  Analyze the
410     /// expression.
411     const SCEV *createSCEV(Value *V);
412
413     /// Provide the special handling we need to analyze PHI SCEVs.
414     const SCEV *createNodeForPHI(PHINode *PN);
415
416     /// Helper function called from createNodeForPHI.
417     const SCEV *createAddRecFromPHI(PHINode *PN);
418
419     /// Helper function called from createNodeForPHI.
420     const SCEV *createNodeFromSelectLikePHI(PHINode *PN);
421
422     /// Provide special handling for a select-like instruction (currently this
423     /// is either a select instruction or a phi node).  \p I is the instruction
424     /// being processed, and it is assumed equivalent to "Cond ? TrueVal :
425     /// FalseVal".
426     const SCEV *createNodeForSelectOrPHI(Instruction *I, Value *Cond,
427                                          Value *TrueVal, Value *FalseVal);
428
429     /// Provide the special handling we need to analyze GEP SCEVs.
430     const SCEV *createNodeForGEP(GEPOperator *GEP);
431
432     /// Implementation code for getSCEVAtScope; called at most once for each
433     /// SCEV+Loop pair.
434     ///
435     const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
436
437     /// This looks up computed SCEV values for all instructions that depend on
438     /// the given instruction and removes them from the ValueExprMap map if they
439     /// reference SymName. This is used during PHI resolution.
440     void ForgetSymbolicName(Instruction *I, const SCEV *SymName);
441
442     /// Return the BackedgeTakenInfo for the given loop, lazily computing new
443     /// values if the loop hasn't been analyzed yet.
444     const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
445
446     /// Compute the number of times the specified loop will iterate.
447     BackedgeTakenInfo computeBackedgeTakenCount(const Loop *L);
448
449     /// Compute the number of times the backedge of the specified loop will
450     /// execute if it exits via the specified block.
451     ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock);
452
453     /// Compute the number of times the backedge of the specified loop will
454     /// execute if its exit condition were a conditional branch of ExitCond,
455     /// TBB, and FBB.
456     ExitLimit computeExitLimitFromCond(const Loop *L,
457                                        Value *ExitCond,
458                                        BasicBlock *TBB,
459                                        BasicBlock *FBB,
460                                        bool IsSubExpr);
461
462     /// Compute the number of times the backedge of the specified loop will
463     /// execute if its exit condition were a conditional branch of the ICmpInst
464     /// ExitCond, TBB, and FBB.
465     ExitLimit computeExitLimitFromICmp(const Loop *L,
466                                        ICmpInst *ExitCond,
467                                        BasicBlock *TBB,
468                                        BasicBlock *FBB,
469                                        bool IsSubExpr);
470
471     /// Compute the number of times the backedge of the specified loop will
472     /// execute if its exit condition were a switch with a single exiting case
473     /// to ExitingBB.
474     ExitLimit
475     computeExitLimitFromSingleExitSwitch(const Loop *L, SwitchInst *Switch,
476                                BasicBlock *ExitingBB, bool IsSubExpr);
477
478     /// Given an exit condition of 'icmp op load X, cst', try to see if we can
479     /// compute the backedge-taken count.
480     ExitLimit computeLoadConstantCompareExitLimit(LoadInst *LI,
481                                                   Constant *RHS,
482                                                   const Loop *L,
483                                                   ICmpInst::Predicate p);
484
485     /// If the loop is known to execute a constant number of times (the
486     /// condition evolves only from constants), try to evaluate a few iterations
487     /// of the loop until we get the exit condition gets a value of ExitWhen
488     /// (true or false).  If we cannot evaluate the exit count of the loop,
489     /// return CouldNotCompute.
490     const SCEV *computeExitCountExhaustively(const Loop *L,
491                                              Value *Cond,
492                                              bool ExitWhen);
493
494     /// Return the number of times an exit condition comparing the specified
495     /// value to zero will execute.  If not computable, return CouldNotCompute.
496     ExitLimit HowFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr);
497
498     /// Return the number of times an exit condition checking the specified
499     /// value for nonzero will execute.  If not computable, return
500     /// CouldNotCompute.
501     ExitLimit HowFarToNonZero(const SCEV *V, const Loop *L);
502
503     /// Return the number of times an exit condition containing the specified
504     /// less-than comparison will execute.  If not computable, return
505     /// CouldNotCompute. isSigned specifies whether the less-than is signed.
506     ExitLimit HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
507                                const Loop *L, bool isSigned, bool IsSubExpr);
508     ExitLimit HowManyGreaterThans(const SCEV *LHS, const SCEV *RHS,
509                                   const Loop *L, bool isSigned, bool IsSubExpr);
510
511     /// Return a predecessor of BB (which may not be an immediate predecessor)
512     /// which has exactly one successor from which BB is reachable, or null if
513     /// no such block is found.
514     std::pair<BasicBlock *, BasicBlock *>
515     getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
516
517     /// Test whether the condition described by Pred, LHS, and RHS is true
518     /// whenever the given FoundCondValue value evaluates to true.
519     bool isImpliedCond(ICmpInst::Predicate Pred,
520                        const SCEV *LHS, const SCEV *RHS,
521                        Value *FoundCondValue,
522                        bool Inverse);
523
524     /// Test whether the condition described by Pred, LHS, and RHS is true
525     /// whenever the condition described by FoundPred, FoundLHS, FoundRHS is
526     /// true.
527     bool isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS,
528                        const SCEV *RHS, ICmpInst::Predicate FoundPred,
529                        const SCEV *FoundLHS, const SCEV *FoundRHS);
530
531     /// Test whether the condition described by Pred, LHS, and RHS is true
532     /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
533     /// true.
534     bool isImpliedCondOperands(ICmpInst::Predicate Pred,
535                                const SCEV *LHS, const SCEV *RHS,
536                                const SCEV *FoundLHS, const SCEV *FoundRHS);
537
538     /// Test whether the condition described by Pred, LHS, and RHS is true
539     /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
540     /// true.
541     bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
542                                      const SCEV *LHS, const SCEV *RHS,
543                                      const SCEV *FoundLHS,
544                                      const SCEV *FoundRHS);
545
546     /// Test whether the condition described by Pred, LHS, and RHS is true
547     /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
548     /// true.  Utility function used by isImpliedCondOperands.
549     bool isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred,
550                                         const SCEV *LHS, const SCEV *RHS,
551                                         const SCEV *FoundLHS,
552                                         const SCEV *FoundRHS);
553
554     /// Test whether the condition described by Pred, LHS, and RHS is true
555     /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
556     /// true.
557     ///
558     /// This routine tries to rule out certain kinds of integer overflow, and
559     /// then tries to reason about arithmetic properties of the predicates.
560     bool isImpliedCondOperandsViaNoOverflow(ICmpInst::Predicate Pred,
561                                             const SCEV *LHS, const SCEV *RHS,
562                                             const SCEV *FoundLHS,
563                                             const SCEV *FoundRHS);
564
565     /// If we know that the specified Phi is in the header of its containing
566     /// loop, we know the loop executes a constant number of times, and the PHI
567     /// node is just a recurrence involving constants, fold it.
568     Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
569                                                 const Loop *L);
570
571     /// Test if the given expression is known to satisfy the condition described
572     /// by Pred and the known constant ranges of LHS and RHS.
573     ///
574     bool isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
575                                     const SCEV *LHS, const SCEV *RHS);
576
577     /// Try to prove the condition described by "LHS Pred RHS" by ruling out
578     /// integer overflow.
579     ///
580     /// For instance, this will return true for "A s< (A + C)<nsw>" if C is
581     /// positive.
582     bool isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred,
583                                        const SCEV *LHS, const SCEV *RHS);
584
585     /// Try to split Pred LHS RHS into logical conjunctions (and's) and try to
586     /// prove them individually.
587     bool isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, const SCEV *LHS,
588                                       const SCEV *RHS);
589
590     /// Try to match the Expr as "(L + R)<Flags>".
591     bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R,
592                         SCEV::NoWrapFlags &Flags);
593
594     /// Return true if More == (Less + C), where C is a constant.  This is
595     /// intended to be used as a cheaper substitute for full SCEV subtraction.
596     bool computeConstantDifference(const SCEV *Less, const SCEV *More,
597                                    APInt &C);
598
599     /// Drop memoized information computed for S.
600     void forgetMemoizedResults(const SCEV *S);
601
602     /// Return an existing SCEV for V if there is one, otherwise return nullptr.
603     const SCEV *getExistingSCEV(Value *V);
604
605     /// Return false iff given SCEV contains a SCEVUnknown with NULL value-
606     /// pointer.
607     bool checkValidity(const SCEV *S) const;
608
609     /// Return true if `ExtendOpTy`({`Start`,+,`Step`}) can be proved to be
610     /// equal to {`ExtendOpTy`(`Start`),+,`ExtendOpTy`(`Step`)}.  This is
611     /// equivalent to proving no signed (resp. unsigned) wrap in
612     /// {`Start`,+,`Step`} if `ExtendOpTy` is `SCEVSignExtendExpr`
613     /// (resp. `SCEVZeroExtendExpr`).
614     ///
615     template<typename ExtendOpTy>
616     bool proveNoWrapByVaryingStart(const SCEV *Start, const SCEV *Step,
617                                    const Loop *L);
618
619     bool isMonotonicPredicateImpl(const SCEVAddRecExpr *LHS,
620                                   ICmpInst::Predicate Pred, bool &Increasing);
621
622     /// Return true if, for all loop invariant X, the predicate "LHS `Pred` X"
623     /// is monotonically increasing or decreasing.  In the former case set
624     /// `Increasing` to true and in the latter case set `Increasing` to false.
625     ///
626     /// A predicate is said to be monotonically increasing if may go from being
627     /// false to being true as the loop iterates, but never the other way
628     /// around.  A predicate is said to be monotonically decreasing if may go
629     /// from being true to being false as the loop iterates, but never the other
630     /// way around.
631     bool isMonotonicPredicate(const SCEVAddRecExpr *LHS,
632                               ICmpInst::Predicate Pred, bool &Increasing);
633
634     // Return SCEV no-wrap flags that can be proven based on reasoning
635     // about how poison produced from no-wrap flags on this value
636     // (e.g. a nuw add) would trigger undefined behavior on overflow.
637     SCEV::NoWrapFlags getNoWrapFlagsFromUB(const Value *V);
638
639   public:
640     ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC,
641                     DominatorTree &DT, LoopInfo &LI);
642     ~ScalarEvolution();
643     ScalarEvolution(ScalarEvolution &&Arg);
644
645     LLVMContext &getContext() const { return F.getContext(); }
646
647     /// Test if values of the given type are analyzable within the SCEV
648     /// framework. This primarily includes integer types, and it can optionally
649     /// include pointer types if the ScalarEvolution class has access to
650     /// target-specific information.
651     bool isSCEVable(Type *Ty) const;
652
653     /// Return the size in bits of the specified type, for which isSCEVable must
654     /// return true.
655     uint64_t getTypeSizeInBits(Type *Ty) const;
656
657     /// Return a type with the same bitwidth as the given type and which
658     /// represents how SCEV will treat the given type, for which isSCEVable must
659     /// return true. For pointer types, this is the pointer-sized integer type.
660     Type *getEffectiveSCEVType(Type *Ty) const;
661
662     /// Return a SCEV expression for the full generality of the specified
663     /// expression.
664     const SCEV *getSCEV(Value *V);
665
666     const SCEV *getConstant(ConstantInt *V);
667     const SCEV *getConstant(const APInt& Val);
668     const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false);
669     const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty);
670     const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty);
671     const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty);
672     const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty);
673     const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
674                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
675     const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
676                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
677       SmallVector<const SCEV *, 2> Ops;
678       Ops.push_back(LHS);
679       Ops.push_back(RHS);
680       return getAddExpr(Ops, Flags);
681     }
682     const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
683                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
684       SmallVector<const SCEV *, 3> Ops;
685       Ops.push_back(Op0);
686       Ops.push_back(Op1);
687       Ops.push_back(Op2);
688       return getAddExpr(Ops, Flags);
689     }
690     const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
691                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
692     const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
693                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap)
694     {
695       SmallVector<const SCEV *, 2> Ops;
696       Ops.push_back(LHS);
697       Ops.push_back(RHS);
698       return getMulExpr(Ops, Flags);
699     }
700     const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
701                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
702       SmallVector<const SCEV *, 3> Ops;
703       Ops.push_back(Op0);
704       Ops.push_back(Op1);
705       Ops.push_back(Op2);
706       return getMulExpr(Ops, Flags);
707     }
708     const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
709     const SCEV *getUDivExactExpr(const SCEV *LHS, const SCEV *RHS);
710     const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
711                               const Loop *L, SCEV::NoWrapFlags Flags);
712     const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
713                               const Loop *L, SCEV::NoWrapFlags Flags);
714     const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
715                               const Loop *L, SCEV::NoWrapFlags Flags) {
716       SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
717       return getAddRecExpr(NewOp, L, Flags);
718     }
719     /// \brief Returns an expression for a GEP
720     ///
721     /// \p PointeeType The type used as the basis for the pointer arithmetics
722     /// \p BaseExpr The expression for the pointer operand.
723     /// \p IndexExprs The expressions for the indices.
724     /// \p InBounds Whether the GEP is in bounds.
725     const SCEV *getGEPExpr(Type *PointeeType, const SCEV *BaseExpr,
726                            const SmallVectorImpl<const SCEV *> &IndexExprs,
727                            bool InBounds = false);
728     const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
729     const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
730     const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
731     const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
732     const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
733     const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
734     const SCEV *getUnknown(Value *V);
735     const SCEV *getCouldNotCompute();
736
737     /// \brief Return a SCEV for the constant 0 of a specific type.
738     const SCEV *getZero(Type *Ty) { return getConstant(Ty, 0); }
739
740     /// \brief Return a SCEV for the constant 1 of a specific type.
741     const SCEV *getOne(Type *Ty) { return getConstant(Ty, 1); }
742
743     /// Return an expression for sizeof AllocTy that is type IntTy
744     ///
745     const SCEV *getSizeOfExpr(Type *IntTy, Type *AllocTy);
746
747     /// Return an expression for offsetof on the given field with type IntTy
748     ///
749     const SCEV *getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo);
750
751     /// Return the SCEV object corresponding to -V.
752     ///
753     const SCEV *getNegativeSCEV(const SCEV *V,
754                                 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
755
756     /// Return the SCEV object corresponding to ~V.
757     ///
758     const SCEV *getNotSCEV(const SCEV *V);
759
760     /// Return LHS-RHS.  Minus is represented in SCEV as A+B*-1.
761     const SCEV *getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
762                              SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
763
764     /// Return a SCEV corresponding to a conversion of the input value to the
765     /// specified type.  If the type must be extended, it is zero extended.
766     const SCEV *getTruncateOrZeroExtend(const SCEV *V, Type *Ty);
767
768     /// Return a SCEV corresponding to a conversion of the input value to the
769     /// specified type.  If the type must be extended, it is sign extended.
770     const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty);
771
772     /// Return a SCEV corresponding to a conversion of the input value to the
773     /// specified type.  If the type must be extended, it is zero extended.  The
774     /// conversion must not be narrowing.
775     const SCEV *getNoopOrZeroExtend(const SCEV *V, Type *Ty);
776
777     /// Return a SCEV corresponding to a conversion of the input value to the
778     /// specified type.  If the type must be extended, it is sign extended.  The
779     /// conversion must not be narrowing.
780     const SCEV *getNoopOrSignExtend(const SCEV *V, Type *Ty);
781
782     /// Return a SCEV corresponding to a conversion of the input value to the
783     /// specified type. If the type must be extended, it is extended with
784     /// unspecified bits. The conversion must not be narrowing.
785     const SCEV *getNoopOrAnyExtend(const SCEV *V, Type *Ty);
786
787     /// Return a SCEV corresponding to a conversion of the input value to the
788     /// specified type.  The conversion must not be widening.
789     const SCEV *getTruncateOrNoop(const SCEV *V, Type *Ty);
790
791     /// Promote the operands to the wider of the types using zero-extension, and
792     /// then perform a umax operation with them.
793     const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS,
794                                            const SCEV *RHS);
795
796     /// Promote the operands to the wider of the types using zero-extension, and
797     /// then perform a umin operation with them.
798     const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS,
799                                            const SCEV *RHS);
800
801     /// Transitively follow the chain of pointer-type operands until reaching a
802     /// SCEV that does not have a single pointer operand. This returns a
803     /// SCEVUnknown pointer for well-formed pointer-type expressions, but corner
804     /// cases do exist.
805     const SCEV *getPointerBase(const SCEV *V);
806
807     /// Return a SCEV expression for the specified value at the specified scope
808     /// in the program.  The L value specifies a loop nest to evaluate the
809     /// expression at, where null is the top-level or a specified loop is
810     /// immediately inside of the loop.
811     ///
812     /// This method can be used to compute the exit value for a variable defined
813     /// in a loop by querying what the value will hold in the parent loop.
814     ///
815     /// In the case that a relevant loop exit value cannot be computed, the
816     /// original value V is returned.
817     const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
818
819     /// This is a convenience function which does getSCEVAtScope(getSCEV(V), L).
820     const SCEV *getSCEVAtScope(Value *V, const Loop *L);
821
822     /// Test whether entry to the loop is protected by a conditional between LHS
823     /// and RHS.  This is used to help avoid max expressions in loop trip
824     /// counts, and to eliminate casts.
825     bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
826                                   const SCEV *LHS, const SCEV *RHS);
827
828     /// Test whether the backedge of the loop is protected by a conditional
829     /// between LHS and RHS.  This is used to to eliminate casts.
830     bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
831                                      const SCEV *LHS, const SCEV *RHS);
832
833     /// \brief Returns the maximum trip count of the loop if it is a single-exit
834     /// loop and we can compute a small maximum for that loop.
835     ///
836     /// Implemented in terms of the \c getSmallConstantTripCount overload with
837     /// the single exiting block passed to it. See that routine for details.
838     unsigned getSmallConstantTripCount(Loop *L);
839
840     /// Returns the maximum trip count of this loop as a normal unsigned
841     /// value. Returns 0 if the trip count is unknown or not constant. This
842     /// "trip count" assumes that control exits via ExitingBlock. More
843     /// precisely, it is the number of times that control may reach ExitingBlock
844     /// before taking the branch. For loops with multiple exits, it may not be
845     /// the number times that the loop header executes if the loop exits
846     /// prematurely via another branch.
847     unsigned getSmallConstantTripCount(Loop *L, BasicBlock *ExitingBlock);
848
849     /// \brief Returns the largest constant divisor of the trip count of the
850     /// loop if it is a single-exit loop and we can compute a small maximum for
851     /// that loop.
852     ///
853     /// Implemented in terms of the \c getSmallConstantTripMultiple overload with
854     /// the single exiting block passed to it. See that routine for details.
855     unsigned getSmallConstantTripMultiple(Loop *L);
856
857     /// Returns the largest constant divisor of the trip count of this loop as a
858     /// normal unsigned value, if possible. This means that the actual trip
859     /// count is always a multiple of the returned value (don't forget the trip
860     /// count could very well be zero as well!). As explained in the comments
861     /// for getSmallConstantTripCount, this assumes that control exits the loop
862     /// via ExitingBlock.
863     unsigned getSmallConstantTripMultiple(Loop *L, BasicBlock *ExitingBlock);
864
865     /// Get the expression for the number of loop iterations for which this loop
866     /// is guaranteed not to exit via ExitingBlock. Otherwise return
867     /// SCEVCouldNotCompute.
868     const SCEV *getExitCount(Loop *L, BasicBlock *ExitingBlock);
869
870     /// If the specified loop has a predictable backedge-taken count, return it,
871     /// otherwise return a SCEVCouldNotCompute object. The backedge-taken count
872     /// is the number of times the loop header will be branched to from within
873     /// the loop. This is one less than the trip count of the loop, since it
874     /// doesn't count the first iteration, when the header is branched to from
875     /// outside the loop.
876     ///
877     /// Note that it is not valid to call this method on a loop without a
878     /// loop-invariant backedge-taken count (see
879     /// hasLoopInvariantBackedgeTakenCount).
880     ///
881     const SCEV *getBackedgeTakenCount(const Loop *L);
882
883     /// Similar to getBackedgeTakenCount, except return the least SCEV value
884     /// that is known never to be less than the actual backedge taken count.
885     const SCEV *getMaxBackedgeTakenCount(const Loop *L);
886
887     /// Return true if the specified loop has an analyzable loop-invariant
888     /// backedge-taken count.
889     bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
890
891     /// This method should be called by the client when it has changed a loop in
892     /// a way that may effect ScalarEvolution's ability to compute a trip count,
893     /// or if the loop is deleted.  This call is potentially expensive for large
894     /// loop bodies.
895     void forgetLoop(const Loop *L);
896
897     /// This method should be called by the client when it has changed a value
898     /// in a way that may effect its value, or which may disconnect it from a
899     /// def-use chain linking it to a loop.
900     void forgetValue(Value *V);
901
902     /// \brief Called when the client has changed the disposition of values in
903     /// this loop.
904     ///
905     /// We don't have a way to invalidate per-loop dispositions. Clear and
906     /// recompute is simpler.
907     void forgetLoopDispositions(const Loop *L) { LoopDispositions.clear(); }
908
909     /// Determine the minimum number of zero bits that S is guaranteed to end in
910     /// (at every loop iteration).  It is, at the same time, the minimum number
911     /// of times S is divisible by 2.  For example, given {4,+,8} it returns 2.
912     /// If S is guaranteed to be 0, it returns the bitwidth of S.
913     uint32_t GetMinTrailingZeros(const SCEV *S);
914
915     /// Determine the unsigned range for a particular SCEV.
916     ///
917     ConstantRange getUnsignedRange(const SCEV *S) {
918       return getRange(S, HINT_RANGE_UNSIGNED);
919     }
920
921     /// Determine the signed range for a particular SCEV.
922     ///
923     ConstantRange getSignedRange(const SCEV *S) {
924       return getRange(S, HINT_RANGE_SIGNED);
925     }
926
927     /// Test if the given expression is known to be negative.
928     ///
929     bool isKnownNegative(const SCEV *S);
930
931     /// Test if the given expression is known to be positive.
932     ///
933     bool isKnownPositive(const SCEV *S);
934
935     /// Test if the given expression is known to be non-negative.
936     ///
937     bool isKnownNonNegative(const SCEV *S);
938
939     /// Test if the given expression is known to be non-positive.
940     ///
941     bool isKnownNonPositive(const SCEV *S);
942
943     /// Test if the given expression is known to be non-zero.
944     ///
945     bool isKnownNonZero(const SCEV *S);
946
947     /// Test if the given expression is known to satisfy the condition described
948     /// by Pred, LHS, and RHS.
949     ///
950     bool isKnownPredicate(ICmpInst::Predicate Pred,
951                           const SCEV *LHS, const SCEV *RHS);
952
953     /// Return true if the result of the predicate LHS `Pred` RHS is loop
954     /// invariant with respect to L.  Set InvariantPred, InvariantLHS and
955     /// InvariantLHS so that InvariantLHS `InvariantPred` InvariantRHS is the
956     /// loop invariant form of LHS `Pred` RHS.
957     bool isLoopInvariantPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
958                                   const SCEV *RHS, const Loop *L,
959                                   ICmpInst::Predicate &InvariantPred,
960                                   const SCEV *&InvariantLHS,
961                                   const SCEV *&InvariantRHS);
962
963     /// Simplify LHS and RHS in a comparison with predicate Pred. Return true
964     /// iff any changes were made. If the operands are provably equal or
965     /// unequal, LHS and RHS are set to the same value and Pred is set to either
966     /// ICMP_EQ or ICMP_NE.
967     ///
968     bool SimplifyICmpOperands(ICmpInst::Predicate &Pred,
969                               const SCEV *&LHS,
970                               const SCEV *&RHS,
971                               unsigned Depth = 0);
972
973     /// Return the "disposition" of the given SCEV with respect to the given
974     /// loop.
975     LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L);
976
977     /// Return true if the value of the given SCEV is unchanging in the
978     /// specified loop.
979     bool isLoopInvariant(const SCEV *S, const Loop *L);
980
981     /// Return true if the given SCEV changes value in a known way in the
982     /// specified loop.  This property being true implies that the value is
983     /// variant in the loop AND that we can emit an expression to compute the
984     /// value of the expression at any particular loop iteration.
985     bool hasComputableLoopEvolution(const SCEV *S, const Loop *L);
986
987     /// Return the "disposition" of the given SCEV with respect to the given
988     /// block.
989     BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB);
990
991     /// Return true if elements that makes up the given SCEV dominate the
992     /// specified basic block.
993     bool dominates(const SCEV *S, const BasicBlock *BB);
994
995     /// Return true if elements that makes up the given SCEV properly dominate
996     /// the specified basic block.
997     bool properlyDominates(const SCEV *S, const BasicBlock *BB);
998
999     /// Test whether the given SCEV has Op as a direct or indirect operand.
1000     bool hasOperand(const SCEV *S, const SCEV *Op) const;
1001
1002     /// Return the size of an element read or written by Inst.
1003     const SCEV *getElementSize(Instruction *Inst);
1004
1005     /// Compute the array dimensions Sizes from the set of Terms extracted from
1006     /// the memory access function of this SCEVAddRecExpr.
1007     void findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
1008                              SmallVectorImpl<const SCEV *> &Sizes,
1009                              const SCEV *ElementSize) const;
1010
1011     void print(raw_ostream &OS) const;
1012     void verify() const;
1013
1014     /// Collect parametric terms occurring in step expressions.
1015     void collectParametricTerms(const SCEV *Expr,
1016                                 SmallVectorImpl<const SCEV *> &Terms);
1017
1018
1019
1020     /// Return in Subscripts the access functions for each dimension in Sizes.
1021     void computeAccessFunctions(const SCEV *Expr,
1022                                 SmallVectorImpl<const SCEV *> &Subscripts,
1023                                 SmallVectorImpl<const SCEV *> &Sizes);
1024
1025     /// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
1026     /// subscripts and sizes of an array access.
1027     ///
1028     /// The delinearization is a 3 step process: the first two steps compute the
1029     /// sizes of each subscript and the third step computes the access functions
1030     /// for the delinearized array:
1031     ///
1032     /// 1. Find the terms in the step functions
1033     /// 2. Compute the array size
1034     /// 3. Compute the access function: divide the SCEV by the array size
1035     ///    starting with the innermost dimensions found in step 2. The Quotient
1036     ///    is the SCEV to be divided in the next step of the recursion. The
1037     ///    Remainder is the subscript of the innermost dimension. Loop over all
1038     ///    array dimensions computed in step 2.
1039     ///
1040     /// To compute a uniform array size for several memory accesses to the same
1041     /// object, one can collect in step 1 all the step terms for all the memory
1042     /// accesses, and compute in step 2 a unique array shape. This guarantees
1043     /// that the array shape will be the same across all memory accesses.
1044     ///
1045     /// FIXME: We could derive the result of steps 1 and 2 from a description of
1046     /// the array shape given in metadata.
1047     ///
1048     /// Example:
1049     ///
1050     /// A[][n][m]
1051     ///
1052     /// for i
1053     ///   for j
1054     ///     for k
1055     ///       A[j+k][2i][5i] =
1056     ///
1057     /// The initial SCEV:
1058     ///
1059     /// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k]
1060     ///
1061     /// 1. Find the different terms in the step functions:
1062     /// -> [2*m, 5, n*m, n*m]
1063     ///
1064     /// 2. Compute the array size: sort and unique them
1065     /// -> [n*m, 2*m, 5]
1066     /// find the GCD of all the terms = 1
1067     /// divide by the GCD and erase constant terms
1068     /// -> [n*m, 2*m]
1069     /// GCD = m
1070     /// divide by GCD -> [n, 2]
1071     /// remove constant terms
1072     /// -> [n]
1073     /// size of the array is A[unknown][n][m]
1074     ///
1075     /// 3. Compute the access function
1076     /// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m
1077     /// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k
1078     /// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k
1079     /// The remainder is the subscript of the innermost array dimension: [5i].
1080     ///
1081     /// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n
1082     /// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k
1083     /// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k
1084     /// The Remainder is the subscript of the next array dimension: [2i].
1085     ///
1086     /// The subscript of the outermost dimension is the Quotient: [j+k].
1087     ///
1088     /// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
1089     void delinearize(const SCEV *Expr,
1090                      SmallVectorImpl<const SCEV *> &Subscripts,
1091                      SmallVectorImpl<const SCEV *> &Sizes,
1092                      const SCEV *ElementSize);
1093
1094   private:
1095     /// Compute the backedge taken count knowing the interval difference, the
1096     /// stride and presence of the equality in the comparison.
1097     const SCEV *computeBECount(const SCEV *Delta, const SCEV *Stride,
1098                                bool Equality);
1099
1100     /// Verify if an linear IV with positive stride can overflow when in a
1101     /// less-than comparison, knowing the invariant term of the comparison,
1102     /// the stride and the knowledge of NSW/NUW flags on the recurrence.
1103     bool doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
1104                             bool IsSigned, bool NoWrap);
1105
1106     /// Verify if an linear IV with negative stride can overflow when in a
1107     /// greater-than comparison, knowing the invariant term of the comparison,
1108     /// the stride and the knowledge of NSW/NUW flags on the recurrence.
1109     bool doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
1110                             bool IsSigned, bool NoWrap);
1111
1112   private:
1113     FoldingSet<SCEV> UniqueSCEVs;
1114     BumpPtrAllocator SCEVAllocator;
1115
1116     /// The head of a linked list of all SCEVUnknown values that have been
1117     /// allocated. This is used by releaseMemory to locate them all and call
1118     /// their destructors.
1119     SCEVUnknown *FirstUnknown;
1120   };
1121
1122   /// \brief Analysis pass that exposes the \c ScalarEvolution for a function.
1123   class ScalarEvolutionAnalysis {
1124     static char PassID;
1125
1126   public:
1127     typedef ScalarEvolution Result;
1128
1129     /// \brief Opaque, unique identifier for this analysis pass.
1130     static void *ID() { return (void *)&PassID; }
1131
1132     /// \brief Provide a name for the analysis for debugging and logging.
1133     static StringRef name() { return "ScalarEvolutionAnalysis"; }
1134
1135     ScalarEvolution run(Function &F, AnalysisManager<Function> *AM);
1136   };
1137
1138   /// \brief Printer pass for the \c ScalarEvolutionAnalysis results.
1139   class ScalarEvolutionPrinterPass {
1140     raw_ostream &OS;
1141
1142   public:
1143     explicit ScalarEvolutionPrinterPass(raw_ostream &OS) : OS(OS) {}
1144     PreservedAnalyses run(Function &F, AnalysisManager<Function> *AM);
1145
1146     static StringRef name() { return "ScalarEvolutionPrinterPass"; }
1147   };
1148
1149   class ScalarEvolutionWrapperPass : public FunctionPass {
1150     std::unique_ptr<ScalarEvolution> SE;
1151
1152   public:
1153     static char ID;
1154
1155     ScalarEvolutionWrapperPass();
1156
1157     ScalarEvolution &getSE() { return *SE; }
1158     const ScalarEvolution &getSE() const { return *SE; }
1159
1160     bool runOnFunction(Function &F) override;
1161     void releaseMemory() override;
1162     void getAnalysisUsage(AnalysisUsage &AU) const override;
1163     void print(raw_ostream &OS, const Module * = nullptr) const override;
1164     void verifyAnalysis() const override;
1165   };
1166 }
1167
1168 #endif