]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/ScalarEvolution.h
MFV: r313101
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/ADT/SetVector.h"
27 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/IR/ConstantRange.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Operator.h"
31 #include "llvm/IR/PassManager.h"
32 #include "llvm/IR/ValueHandle.h"
33 #include "llvm/IR/ValueMap.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Support/Allocator.h"
36 #include "llvm/Support/DataTypes.h"
37
38 namespace llvm {
39   class APInt;
40   class AssumptionCache;
41   class Constant;
42   class ConstantInt;
43   class DominatorTree;
44   class Type;
45   class ScalarEvolution;
46   class DataLayout;
47   class TargetLibraryInfo;
48   class LLVMContext;
49   class Operator;
50   class SCEV;
51   class SCEVAddRecExpr;
52   class SCEVConstant;
53   class SCEVExpander;
54   class SCEVPredicate;
55   class SCEVUnknown;
56   class Function;
57
58   template <> struct FoldingSetTrait<SCEV>;
59   template <> struct FoldingSetTrait<SCEVPredicate>;
60
61   /// This class represents an analyzed expression in the program.  These are
62   /// opaque objects that the client is not allowed to do much with directly.
63   ///
64   class SCEV : public FoldingSetNode {
65     friend struct FoldingSetTrait<SCEV>;
66
67     /// A reference to an Interned FoldingSetNodeID for this node.  The
68     /// ScalarEvolution's BumpPtrAllocator holds the data.
69     FoldingSetNodeIDRef FastID;
70
71     // The SCEV baseclass this node corresponds to
72     const unsigned short SCEVType;
73
74   protected:
75     /// This field is initialized to zero and may be used in subclasses to store
76     /// miscellaneous information.
77     unsigned short SubclassData;
78
79   private:
80     SCEV(const SCEV &) = delete;
81     void operator=(const SCEV &) = delete;
82
83   public:
84     /// NoWrapFlags are bitfield indices into SubclassData.
85     ///
86     /// Add and Mul expressions may have no-unsigned-wrap <NUW> or
87     /// no-signed-wrap <NSW> properties, which are derived from the IR
88     /// operator. NSW is a misnomer that we use to mean no signed overflow or
89     /// underflow.
90     ///
91     /// AddRec expressions may have a no-self-wraparound <NW> property if, in
92     /// the integer domain, abs(step) * max-iteration(loop) <=
93     /// unsigned-max(bitwidth).  This means that the recurrence will never reach
94     /// its start value if the step is non-zero.  Computing the same value on
95     /// each iteration is not considered wrapping, and recurrences with step = 0
96     /// are trivially <NW>.  <NW> is independent of the sign of step and the
97     /// value the add recurrence starts with.
98     ///
99     /// Note that NUW and NSW are also valid properties of a recurrence, and
100     /// either implies NW. For convenience, NW will be set for a recurrence
101     /// whenever either NUW or NSW are set.
102     enum NoWrapFlags { FlagAnyWrap = 0,          // No guarantee.
103                        FlagNW      = (1 << 0),   // No self-wrap.
104                        FlagNUW     = (1 << 1),   // No unsigned wrap.
105                        FlagNSW     = (1 << 2),   // No signed wrap.
106                        NoWrapMask  = (1 << 3) -1 };
107
108     explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) :
109       FastID(ID), SCEVType(SCEVTy), SubclassData(0) {}
110
111     unsigned getSCEVType() const { return SCEVType; }
112
113     /// Return the LLVM type of this SCEV expression.
114     ///
115     Type *getType() const;
116
117     /// Return true if the expression is a constant zero.
118     ///
119     bool isZero() const;
120
121     /// Return true if the expression is a constant one.
122     ///
123     bool isOne() const;
124
125     /// Return true if the expression is a constant all-ones value.
126     ///
127     bool isAllOnesValue() const;
128
129     /// Return true if the specified scev is negated, but not a constant.
130     bool isNonConstantNegative() const;
131
132     /// Print out the internal representation of this scalar to the specified
133     /// stream.  This should really only be used for debugging purposes.
134     void print(raw_ostream &OS) const;
135
136     /// This method is used for debugging.
137     ///
138     void dump() const;
139   };
140
141   // Specialize FoldingSetTrait for SCEV to avoid needing to compute
142   // temporary FoldingSetNodeID values.
143   template<> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> {
144     static void Profile(const SCEV &X, FoldingSetNodeID& ID) {
145       ID = X.FastID;
146     }
147     static bool Equals(const SCEV &X, const FoldingSetNodeID &ID,
148                        unsigned IDHash, FoldingSetNodeID &TempID) {
149       return ID == X.FastID;
150     }
151     static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) {
152       return X.FastID.ComputeHash();
153     }
154   };
155
156   inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
157     S.print(OS);
158     return OS;
159   }
160
161   /// An object of this class is returned by queries that could not be answered.
162   /// For example, if you ask for the number of iterations of a linked-list
163   /// traversal loop, you will get one of these.  None of the standard SCEV
164   /// operations are valid on this class, it is just a marker.
165   struct SCEVCouldNotCompute : public SCEV {
166     SCEVCouldNotCompute();
167
168     /// Methods for support type inquiry through isa, cast, and dyn_cast:
169     static bool classof(const SCEV *S);
170   };
171
172   /// This class represents an assumption made using SCEV expressions which can
173   /// be checked at run-time.
174   class SCEVPredicate : public FoldingSetNode {
175     friend struct FoldingSetTrait<SCEVPredicate>;
176
177     /// A reference to an Interned FoldingSetNodeID for this node.  The
178     /// ScalarEvolution's BumpPtrAllocator holds the data.
179     FoldingSetNodeIDRef FastID;
180
181   public:
182     enum SCEVPredicateKind { P_Union, P_Equal, P_Wrap };
183
184   protected:
185     SCEVPredicateKind Kind;
186     ~SCEVPredicate() = default;
187     SCEVPredicate(const SCEVPredicate&) = default;
188     SCEVPredicate &operator=(const SCEVPredicate&) = default;
189
190   public:
191     SCEVPredicate(const FoldingSetNodeIDRef ID, SCEVPredicateKind Kind);
192
193     SCEVPredicateKind getKind() const { return Kind; }
194
195     /// Returns the estimated complexity of this predicate.  This is roughly
196     /// measured in the number of run-time checks required.
197     virtual unsigned getComplexity() const { return 1; }
198
199     /// Returns true if the predicate is always true. This means that no
200     /// assumptions were made and nothing needs to be checked at run-time.
201     virtual bool isAlwaysTrue() const = 0;
202
203     /// Returns true if this predicate implies \p N.
204     virtual bool implies(const SCEVPredicate *N) const = 0;
205
206     /// Prints a textual representation of this predicate with an indentation of
207     /// \p Depth.
208     virtual void print(raw_ostream &OS, unsigned Depth = 0) const = 0;
209
210     /// Returns the SCEV to which this predicate applies, or nullptr if this is
211     /// a SCEVUnionPredicate.
212     virtual const SCEV *getExpr() const = 0;
213   };
214
215   inline raw_ostream &operator<<(raw_ostream &OS, const SCEVPredicate &P) {
216     P.print(OS);
217     return OS;
218   }
219
220   // Specialize FoldingSetTrait for SCEVPredicate to avoid needing to compute
221   // temporary FoldingSetNodeID values.
222   template <>
223   struct FoldingSetTrait<SCEVPredicate>
224       : DefaultFoldingSetTrait<SCEVPredicate> {
225
226     static void Profile(const SCEVPredicate &X, FoldingSetNodeID &ID) {
227       ID = X.FastID;
228     }
229
230     static bool Equals(const SCEVPredicate &X, const FoldingSetNodeID &ID,
231                        unsigned IDHash, FoldingSetNodeID &TempID) {
232       return ID == X.FastID;
233     }
234     static unsigned ComputeHash(const SCEVPredicate &X,
235                                 FoldingSetNodeID &TempID) {
236       return X.FastID.ComputeHash();
237     }
238   };
239
240   /// This class represents an assumption that two SCEV expressions are equal,
241   /// and this can be checked at run-time. We assume that the left hand side is
242   /// a SCEVUnknown and the right hand side a constant.
243   class SCEVEqualPredicate final : public SCEVPredicate {
244     /// We assume that LHS == RHS, where LHS is a SCEVUnknown and RHS a
245     /// constant.
246     const SCEVUnknown *LHS;
247     const SCEVConstant *RHS;
248
249   public:
250     SCEVEqualPredicate(const FoldingSetNodeIDRef ID, const SCEVUnknown *LHS,
251                        const SCEVConstant *RHS);
252
253     /// Implementation of the SCEVPredicate interface
254     bool implies(const SCEVPredicate *N) const override;
255     void print(raw_ostream &OS, unsigned Depth = 0) const override;
256     bool isAlwaysTrue() const override;
257     const SCEV *getExpr() const override;
258
259     /// Returns the left hand side of the equality.
260     const SCEVUnknown *getLHS() const { return LHS; }
261
262     /// Returns the right hand side of the equality.
263     const SCEVConstant *getRHS() const { return RHS; }
264
265     /// Methods for support type inquiry through isa, cast, and dyn_cast:
266     static inline bool classof(const SCEVPredicate *P) {
267       return P->getKind() == P_Equal;
268     }
269   };
270
271   /// This class represents an assumption made on an AddRec expression. Given an
272   /// affine AddRec expression {a,+,b}, we assume that it has the nssw or nusw
273   /// flags (defined below) in the first X iterations of the loop, where X is a
274   /// SCEV expression returned by getPredicatedBackedgeTakenCount).
275   ///
276   /// Note that this does not imply that X is equal to the backedge taken
277   /// count. This means that if we have a nusw predicate for i32 {0,+,1} with a
278   /// predicated backedge taken count of X, we only guarantee that {0,+,1} has
279   /// nusw in the first X iterations. {0,+,1} may still wrap in the loop if we
280   /// have more than X iterations.
281   class SCEVWrapPredicate final : public SCEVPredicate {
282   public:
283     /// Similar to SCEV::NoWrapFlags, but with slightly different semantics
284     /// for FlagNUSW. The increment is considered to be signed, and a + b
285     /// (where b is the increment) is considered to wrap if:
286     ///    zext(a + b) != zext(a) + sext(b)
287     ///
288     /// If Signed is a function that takes an n-bit tuple and maps to the
289     /// integer domain as the tuples value interpreted as twos complement,
290     /// and Unsigned a function that takes an n-bit tuple and maps to the
291     /// integer domain as as the base two value of input tuple, then a + b
292     /// has IncrementNUSW iff:
293     ///
294     /// 0 <= Unsigned(a) + Signed(b) < 2^n
295     ///
296     /// The IncrementNSSW flag has identical semantics with SCEV::FlagNSW.
297     ///
298     /// Note that the IncrementNUSW flag is not commutative: if base + inc
299     /// has IncrementNUSW, then inc + base doesn't neccessarily have this
300     /// property. The reason for this is that this is used for sign/zero
301     /// extending affine AddRec SCEV expressions when a SCEVWrapPredicate is
302     /// assumed. A {base,+,inc} expression is already non-commutative with
303     /// regards to base and inc, since it is interpreted as:
304     ///     (((base + inc) + inc) + inc) ...
305     enum IncrementWrapFlags {
306       IncrementAnyWrap = 0,     // No guarantee.
307       IncrementNUSW = (1 << 0), // No unsigned with signed increment wrap.
308       IncrementNSSW = (1 << 1), // No signed with signed increment wrap
309                                 // (equivalent with SCEV::NSW)
310       IncrementNoWrapMask = (1 << 2) - 1
311     };
312
313     /// Convenient IncrementWrapFlags manipulation methods.
314     static SCEVWrapPredicate::IncrementWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
315     clearFlags(SCEVWrapPredicate::IncrementWrapFlags Flags,
316                SCEVWrapPredicate::IncrementWrapFlags OffFlags) {
317       assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
318       assert((OffFlags & IncrementNoWrapMask) == OffFlags &&
319              "Invalid flags value!");
320       return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & ~OffFlags);
321     }
322
323     static SCEVWrapPredicate::IncrementWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
324     maskFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, int Mask) {
325       assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
326       assert((Mask & IncrementNoWrapMask) == Mask && "Invalid mask value!");
327
328       return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & Mask);
329     }
330
331     static SCEVWrapPredicate::IncrementWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
332     setFlags(SCEVWrapPredicate::IncrementWrapFlags Flags,
333              SCEVWrapPredicate::IncrementWrapFlags OnFlags) {
334       assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
335       assert((OnFlags & IncrementNoWrapMask) == OnFlags &&
336              "Invalid flags value!");
337
338       return (SCEVWrapPredicate::IncrementWrapFlags)(Flags | OnFlags);
339     }
340
341     /// Returns the set of SCEVWrapPredicate no wrap flags implied by a
342     /// SCEVAddRecExpr.
343     static SCEVWrapPredicate::IncrementWrapFlags
344     getImpliedFlags(const SCEVAddRecExpr *AR, ScalarEvolution &SE);
345
346   private:
347     const SCEVAddRecExpr *AR;
348     IncrementWrapFlags Flags;
349
350   public:
351     explicit SCEVWrapPredicate(const FoldingSetNodeIDRef ID,
352                                const SCEVAddRecExpr *AR,
353                                IncrementWrapFlags Flags);
354
355     /// Returns the set assumed no overflow flags.
356     IncrementWrapFlags getFlags() const { return Flags; }
357     /// Implementation of the SCEVPredicate interface
358     const SCEV *getExpr() const override;
359     bool implies(const SCEVPredicate *N) const override;
360     void print(raw_ostream &OS, unsigned Depth = 0) const override;
361     bool isAlwaysTrue() const override;
362
363     /// Methods for support type inquiry through isa, cast, and dyn_cast:
364     static inline bool classof(const SCEVPredicate *P) {
365       return P->getKind() == P_Wrap;
366     }
367   };
368
369   /// This class represents a composition of other SCEV predicates, and is the
370   /// class that most clients will interact with.  This is equivalent to a
371   /// logical "AND" of all the predicates in the union.
372   class SCEVUnionPredicate final : public SCEVPredicate {
373   private:
374     typedef DenseMap<const SCEV *, SmallVector<const SCEVPredicate *, 4>>
375         PredicateMap;
376
377     /// Vector with references to all predicates in this union.
378     SmallVector<const SCEVPredicate *, 16> Preds;
379     /// Maps SCEVs to predicates for quick look-ups.
380     PredicateMap SCEVToPreds;
381
382   public:
383     SCEVUnionPredicate();
384
385     const SmallVectorImpl<const SCEVPredicate *> &getPredicates() const {
386       return Preds;
387     }
388
389     /// Adds a predicate to this union.
390     void add(const SCEVPredicate *N);
391
392     /// Returns a reference to a vector containing all predicates which apply to
393     /// \p Expr.
394     ArrayRef<const SCEVPredicate *> getPredicatesForExpr(const SCEV *Expr);
395
396     /// Implementation of the SCEVPredicate interface
397     bool isAlwaysTrue() const override;
398     bool implies(const SCEVPredicate *N) const override;
399     void print(raw_ostream &OS, unsigned Depth) const override;
400     const SCEV *getExpr() const override;
401
402     /// We estimate the complexity of a union predicate as the size number of
403     /// predicates in the union.
404     unsigned getComplexity() const override { return Preds.size(); }
405
406     /// Methods for support type inquiry through isa, cast, and dyn_cast:
407     static inline bool classof(const SCEVPredicate *P) {
408       return P->getKind() == P_Union;
409     }
410   };
411
412   /// The main scalar evolution driver. Because client code (intentionally)
413   /// can't do much with the SCEV objects directly, they must ask this class
414   /// for services.
415   class ScalarEvolution {
416   public:
417     /// An enum describing the relationship between a SCEV and a loop.
418     enum LoopDisposition {
419       LoopVariant,    ///< The SCEV is loop-variant (unknown).
420       LoopInvariant,  ///< The SCEV is loop-invariant.
421       LoopComputable  ///< The SCEV varies predictably with the loop.
422     };
423
424     /// An enum describing the relationship between a SCEV and a basic block.
425     enum BlockDisposition {
426       DoesNotDominateBlock,  ///< The SCEV does not dominate the block.
427       DominatesBlock,        ///< The SCEV dominates the block.
428       ProperlyDominatesBlock ///< The SCEV properly dominates the block.
429     };
430
431     /// Convenient NoWrapFlags manipulation that hides enum casts and is
432     /// visible in the ScalarEvolution name space.
433     static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
434     maskFlags(SCEV::NoWrapFlags Flags, int Mask) {
435       return (SCEV::NoWrapFlags)(Flags & Mask);
436     }
437     static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
438     setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags) {
439       return (SCEV::NoWrapFlags)(Flags | OnFlags);
440     }
441     static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
442     clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags) {
443       return (SCEV::NoWrapFlags)(Flags & ~OffFlags);
444     }
445
446   private:
447     /// A CallbackVH to arrange for ScalarEvolution to be notified whenever a
448     /// Value is deleted.
449     class SCEVCallbackVH final : public CallbackVH {
450       ScalarEvolution *SE;
451       void deleted() override;
452       void allUsesReplacedWith(Value *New) override;
453     public:
454       SCEVCallbackVH(Value *V, ScalarEvolution *SE = nullptr);
455     };
456
457     friend class SCEVCallbackVH;
458     friend class SCEVExpander;
459     friend class SCEVUnknown;
460
461     /// The function we are analyzing.
462     ///
463     Function &F;
464
465     /// Does the module have any calls to the llvm.experimental.guard intrinsic
466     /// at all?  If this is false, we avoid doing work that will only help if
467     /// thare are guards present in the IR.
468     ///
469     bool HasGuards;
470
471     /// The target library information for the target we are targeting.
472     ///
473     TargetLibraryInfo &TLI;
474
475     /// The tracker for @llvm.assume intrinsics in this function.
476     AssumptionCache &AC;
477
478     /// The dominator tree.
479     ///
480     DominatorTree &DT;
481
482     /// The loop information for the function we are currently analyzing.
483     ///
484     LoopInfo &LI;
485
486     /// This SCEV is used to represent unknown trip counts and things.
487     std::unique_ptr<SCEVCouldNotCompute> CouldNotCompute;
488
489     /// The typedef for HasRecMap.
490     ///
491     typedef DenseMap<const SCEV *, bool> HasRecMapType;
492
493     /// This is a cache to record whether a SCEV contains any scAddRecExpr.
494     HasRecMapType HasRecMap;
495
496     /// The typedef for ExprValueMap.
497     ///
498     typedef std::pair<Value *, ConstantInt *> ValueOffsetPair;
499     typedef DenseMap<const SCEV *, SetVector<ValueOffsetPair>> ExprValueMapType;
500
501     /// ExprValueMap -- This map records the original values from which
502     /// the SCEV expr is generated from.
503     ///
504     /// We want to represent the mapping as SCEV -> ValueOffsetPair instead
505     /// of SCEV -> Value:
506     /// Suppose we know S1 expands to V1, and
507     ///  S1 = S2 + C_a
508     ///  S3 = S2 + C_b
509     /// where C_a and C_b are different SCEVConstants. Then we'd like to
510     /// expand S3 as V1 - C_a + C_b instead of expanding S2 literally.
511     /// It is helpful when S2 is a complex SCEV expr.
512     ///
513     /// In order to do that, we represent ExprValueMap as a mapping from
514     /// SCEV to ValueOffsetPair. We will save both S1->{V1, 0} and
515     /// S2->{V1, C_a} into the map when we create SCEV for V1. When S3
516     /// is expanded, it will first expand S2 to V1 - C_a because of
517     /// S2->{V1, C_a} in the map, then expand S3 to V1 - C_a + C_b.
518     ///
519     /// Note: S->{V, Offset} in the ExprValueMap means S can be expanded
520     /// to V - Offset.
521     ExprValueMapType ExprValueMap;
522
523     /// The typedef for ValueExprMap.
524     ///
525     typedef DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *> >
526       ValueExprMapType;
527
528     /// This is a cache of the values we have analyzed so far.
529     ///
530     ValueExprMapType ValueExprMap;
531
532     /// Mark predicate values currently being processed by isImpliedCond.
533     DenseSet<Value*> PendingLoopPredicates;
534
535     /// Set to true by isLoopBackedgeGuardedByCond when we're walking the set of
536     /// conditions dominating the backedge of a loop.
537     bool WalkingBEDominatingConds;
538
539     /// Set to true by isKnownPredicateViaSplitting when we're trying to prove a
540     /// predicate by splitting it into a set of independent predicates.
541     bool ProvingSplitPredicate;
542
543     /// Information about the number of loop iterations for which a loop exit's
544     /// branch condition evaluates to the not-taken path.  This is a temporary
545     /// pair of exact and max expressions that are eventually summarized in
546     /// ExitNotTakenInfo and BackedgeTakenInfo.
547     struct ExitLimit {
548       const SCEV *Exact;
549       const SCEV *Max;
550
551       /// A predicate union guard for this ExitLimit. The result is only
552       /// valid if this predicate evaluates to 'true' at run-time.
553       SCEVUnionPredicate Pred;
554
555       /*implicit*/ ExitLimit(const SCEV *E) : Exact(E), Max(E) {}
556
557       ExitLimit(const SCEV *E, const SCEV *M, SCEVUnionPredicate &P)
558           : Exact(E), Max(M), Pred(P) {
559         assert((isa<SCEVCouldNotCompute>(Exact) ||
560                 !isa<SCEVCouldNotCompute>(Max)) &&
561                "Exact is not allowed to be less precise than Max");
562       }
563
564       /// Test whether this ExitLimit contains any computed information, or
565       /// whether it's all SCEVCouldNotCompute values.
566       bool hasAnyInfo() const {
567         return !isa<SCEVCouldNotCompute>(Exact) ||
568           !isa<SCEVCouldNotCompute>(Max);
569       }
570
571       /// Test whether this ExitLimit contains all information.
572       bool hasFullInfo() const { return !isa<SCEVCouldNotCompute>(Exact); }
573     };
574
575     /// Forward declaration of ExitNotTakenExtras
576     struct ExitNotTakenExtras;
577
578     /// Information about the number of times a particular loop exit may be
579     /// reached before exiting the loop.
580     struct ExitNotTakenInfo {
581       AssertingVH<BasicBlock> ExitingBlock;
582       const SCEV *ExactNotTaken;
583
584       ExitNotTakenExtras *ExtraInfo;
585       bool Complete;
586
587       ExitNotTakenInfo()
588           : ExitingBlock(nullptr), ExactNotTaken(nullptr), ExtraInfo(nullptr),
589             Complete(true) {}
590
591       ExitNotTakenInfo(BasicBlock *ExitBlock, const SCEV *Expr,
592                        ExitNotTakenExtras *Ptr)
593           : ExitingBlock(ExitBlock), ExactNotTaken(Expr), ExtraInfo(Ptr),
594             Complete(true) {}
595
596       /// Return true if all loop exits are computable.
597       bool isCompleteList() const { return Complete; }
598
599       /// Sets the incomplete property, indicating that one of the loop exits
600       /// doesn't have a corresponding ExitNotTakenInfo entry.
601       void setIncomplete() { Complete = false; }
602
603       /// Returns a pointer to the predicate associated with this information,
604       /// or nullptr if this doesn't exist (meaning always true).
605       SCEVUnionPredicate *getPred() const {
606         if (ExtraInfo)
607           return &ExtraInfo->Pred;
608
609         return nullptr;
610       }
611
612       /// Return true if the SCEV predicate associated with this information
613       /// is always true.
614       bool hasAlwaysTruePred() const {
615         return !getPred() || getPred()->isAlwaysTrue();
616       }
617
618       /// Defines a simple forward iterator for ExitNotTakenInfo.
619       class ExitNotTakenInfoIterator
620           : public std::iterator<std::forward_iterator_tag, ExitNotTakenInfo> {
621         const ExitNotTakenInfo *Start;
622         unsigned Position;
623
624       public:
625         ExitNotTakenInfoIterator(const ExitNotTakenInfo *Start,
626                                  unsigned Position)
627             : Start(Start), Position(Position) {}
628
629         const ExitNotTakenInfo &operator*() const {
630           if (Position == 0)
631             return *Start;
632
633           return Start->ExtraInfo->Exits[Position - 1];
634         }
635
636         const ExitNotTakenInfo *operator->() const {
637           if (Position == 0)
638             return Start;
639
640           return &Start->ExtraInfo->Exits[Position - 1];
641         }
642
643         bool operator==(const ExitNotTakenInfoIterator &RHS) const {
644           return Start == RHS.Start && Position == RHS.Position;
645         }
646
647         bool operator!=(const ExitNotTakenInfoIterator &RHS) const {
648           return Start != RHS.Start || Position != RHS.Position;
649         }
650
651         ExitNotTakenInfoIterator &operator++() { // Preincrement
652           if (!Start)
653             return *this;
654
655           unsigned Elements =
656               Start->ExtraInfo ? Start->ExtraInfo->Exits.size() + 1 : 1;
657
658           ++Position;
659
660           // We've run out of elements.
661           if (Position == Elements) {
662             Start = nullptr;
663             Position = 0;
664           }
665
666           return *this;
667         }
668         ExitNotTakenInfoIterator operator++(int) { // Postincrement
669           ExitNotTakenInfoIterator Tmp = *this;
670           ++*this;
671           return Tmp;
672         }
673       };
674
675       /// Iterators
676       ExitNotTakenInfoIterator begin() const {
677         return ExitNotTakenInfoIterator(this, 0);
678       }
679       ExitNotTakenInfoIterator end() const {
680         return ExitNotTakenInfoIterator(nullptr, 0);
681       }
682     };
683
684     /// Describes the extra information that a ExitNotTakenInfo can have.
685     struct ExitNotTakenExtras {
686       /// The predicate associated with the ExitNotTakenInfo struct.
687       SCEVUnionPredicate Pred;
688
689       /// The extra exits in the loop. Only the ExitNotTakenExtras structure
690       /// pointed to by the first ExitNotTakenInfo struct (associated with the
691       /// first loop exit) will populate this vector to prevent having
692       /// redundant information.
693       SmallVector<ExitNotTakenInfo, 4> Exits;
694     };
695
696     /// A struct containing the information attached to a backedge.
697     struct EdgeInfo {
698       EdgeInfo(BasicBlock *Block, const SCEV *Taken, SCEVUnionPredicate &P) :
699           ExitBlock(Block), Taken(Taken), Pred(std::move(P)) {}
700
701       /// The exit basic block.
702       BasicBlock *ExitBlock;
703
704       /// The (exact) number of time we take the edge back.
705       const SCEV *Taken;
706
707       /// The SCEV predicated associated with Taken. If Pred doesn't evaluate
708       /// to true, the information in Taken is not valid (or equivalent with
709       /// a CouldNotCompute.
710       SCEVUnionPredicate Pred;
711     };
712
713     /// Information about the backedge-taken count of a loop. This currently
714     /// includes an exact count and a maximum count.
715     ///
716     class BackedgeTakenInfo {
717       /// A list of computable exits and their not-taken counts.  Loops almost
718       /// never have more than one computable exit.
719       ExitNotTakenInfo ExitNotTaken;
720
721       /// An expression indicating the least maximum backedge-taken count of the
722       /// loop that is known, or a SCEVCouldNotCompute. This expression is only
723       /// valid if the predicates associated with all loop exits are true.
724       const SCEV *Max;
725
726     public:
727       BackedgeTakenInfo() : Max(nullptr) {}
728
729       /// Initialize BackedgeTakenInfo from a list of exact exit counts.
730       BackedgeTakenInfo(SmallVectorImpl<EdgeInfo> &ExitCounts, bool Complete,
731                         const SCEV *MaxCount);
732
733       /// Test whether this BackedgeTakenInfo contains any computed information,
734       /// or whether it's all SCEVCouldNotCompute values.
735       bool hasAnyInfo() const {
736         return ExitNotTaken.ExitingBlock || !isa<SCEVCouldNotCompute>(Max);
737       }
738
739       /// Test whether this BackedgeTakenInfo contains complete information.
740       bool hasFullInfo() const { return ExitNotTaken.isCompleteList(); }
741
742       /// Return an expression indicating the exact backedge-taken count of the
743       /// loop if it is known or SCEVCouldNotCompute otherwise. This is the
744       /// number of times the loop header can be guaranteed to execute, minus
745       /// one.
746       ///
747       /// If the SCEV predicate associated with the answer can be different
748       /// from AlwaysTrue, we must add a (non null) Predicates argument.
749       /// The SCEV predicate associated with the answer will be added to
750       /// Predicates. A run-time check needs to be emitted for the SCEV
751       /// predicate in order for the answer to be valid.
752       ///
753       /// Note that we should always know if we need to pass a predicate
754       /// argument or not from the way the ExitCounts vector was computed.
755       /// If we allowed SCEV predicates to be generated when populating this
756       /// vector, this information can contain them and therefore a
757       /// SCEVPredicate argument should be added to getExact.
758       const SCEV *getExact(ScalarEvolution *SE,
759                            SCEVUnionPredicate *Predicates = nullptr) const;
760
761       /// Return the number of times this loop exit may fall through to the back
762       /// edge, or SCEVCouldNotCompute. The loop is guaranteed not to exit via
763       /// this block before this number of iterations, but may exit via another
764       /// block.
765       const SCEV *getExact(BasicBlock *ExitingBlock, ScalarEvolution *SE) const;
766
767       /// Get the max backedge taken count for the loop.
768       const SCEV *getMax(ScalarEvolution *SE) const;
769
770       /// Return true if any backedge taken count expressions refer to the given
771       /// subexpression.
772       bool hasOperand(const SCEV *S, ScalarEvolution *SE) const;
773
774       /// Invalidate this result and free associated memory.
775       void clear();
776     };
777
778     /// Cache the backedge-taken count of the loops for this function as they
779     /// are computed.
780     DenseMap<const Loop *, BackedgeTakenInfo> BackedgeTakenCounts;
781
782     /// Cache the predicated backedge-taken count of the loops for this
783     /// function as they are computed.
784     DenseMap<const Loop *, BackedgeTakenInfo> PredicatedBackedgeTakenCounts;
785
786     /// This map contains entries for all of the PHI instructions that we
787     /// attempt to compute constant evolutions for.  This allows us to avoid
788     /// potentially expensive recomputation of these properties.  An instruction
789     /// maps to null if we are unable to compute its exit value.
790     DenseMap<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
791
792     /// This map contains entries for all the expressions that we attempt to
793     /// compute getSCEVAtScope information for, which can be expensive in
794     /// extreme cases.
795     DenseMap<const SCEV *,
796              SmallVector<std::pair<const Loop *, const SCEV *>, 2> > ValuesAtScopes;
797
798     /// Memoized computeLoopDisposition results.
799     DenseMap<const SCEV *,
800              SmallVector<PointerIntPair<const Loop *, 2, LoopDisposition>, 2>>
801         LoopDispositions;
802
803     /// Cache for \c loopHasNoAbnormalExits.
804     DenseMap<const Loop *, bool> LoopHasNoAbnormalExits;
805
806     /// Returns true if \p L contains no instruction that can abnormally exit
807     /// the loop (i.e. via throwing an exception, by terminating the thread
808     /// cleanly or by infinite looping in a called function).  Strictly
809     /// speaking, the last one is not leaving the loop, but is identical to
810     /// leaving the loop for reasoning about undefined behavior.
811     bool loopHasNoAbnormalExits(const Loop *L);
812
813     /// Compute a LoopDisposition value.
814     LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L);
815
816     /// Memoized computeBlockDisposition results.
817     DenseMap<
818         const SCEV *,
819         SmallVector<PointerIntPair<const BasicBlock *, 2, BlockDisposition>, 2>>
820         BlockDispositions;
821
822     /// Compute a BlockDisposition value.
823     BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB);
824
825     /// Memoized results from getRange
826     DenseMap<const SCEV *, ConstantRange> UnsignedRanges;
827
828     /// Memoized results from getRange
829     DenseMap<const SCEV *, ConstantRange> SignedRanges;
830
831     /// Used to parameterize getRange
832     enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED };
833
834     /// Set the memoized range for the given SCEV.
835     const ConstantRange &setRange(const SCEV *S, RangeSignHint Hint,
836                                   const ConstantRange &CR) {
837       DenseMap<const SCEV *, ConstantRange> &Cache =
838           Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges;
839
840       auto Pair = Cache.insert({S, CR});
841       if (!Pair.second)
842         Pair.first->second = CR;
843       return Pair.first->second;
844     }
845
846     /// Determine the range for a particular SCEV.
847     ConstantRange getRange(const SCEV *S, RangeSignHint Hint);
848
849     /// Determines the range for the affine SCEVAddRecExpr {\p Start,+,\p Stop}.
850     /// Helper for \c getRange.
851     ConstantRange getRangeForAffineAR(const SCEV *Start, const SCEV *Stop,
852                                       const SCEV *MaxBECount,
853                                       unsigned BitWidth);
854
855     /// Try to compute a range for the affine SCEVAddRecExpr {\p Start,+,\p
856     /// Stop} by "factoring out" a ternary expression from the add recurrence.
857     /// Helper called by \c getRange.
858     ConstantRange getRangeViaFactoring(const SCEV *Start, const SCEV *Stop,
859                                        const SCEV *MaxBECount,
860                                        unsigned BitWidth);
861
862     /// We know that there is no SCEV for the specified value.  Analyze the
863     /// expression.
864     const SCEV *createSCEV(Value *V);
865
866     /// Provide the special handling we need to analyze PHI SCEVs.
867     const SCEV *createNodeForPHI(PHINode *PN);
868
869     /// Helper function called from createNodeForPHI.
870     const SCEV *createAddRecFromPHI(PHINode *PN);
871
872     /// Helper function called from createNodeForPHI.
873     const SCEV *createNodeFromSelectLikePHI(PHINode *PN);
874
875     /// Provide special handling for a select-like instruction (currently this
876     /// is either a select instruction or a phi node).  \p I is the instruction
877     /// being processed, and it is assumed equivalent to "Cond ? TrueVal :
878     /// FalseVal".
879     const SCEV *createNodeForSelectOrPHI(Instruction *I, Value *Cond,
880                                          Value *TrueVal, Value *FalseVal);
881
882     /// Provide the special handling we need to analyze GEP SCEVs.
883     const SCEV *createNodeForGEP(GEPOperator *GEP);
884
885     /// Implementation code for getSCEVAtScope; called at most once for each
886     /// SCEV+Loop pair.
887     ///
888     const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
889
890     /// This looks up computed SCEV values for all instructions that depend on
891     /// the given instruction and removes them from the ValueExprMap map if they
892     /// reference SymName. This is used during PHI resolution.
893     void forgetSymbolicName(Instruction *I, const SCEV *SymName);
894
895     /// Return the BackedgeTakenInfo for the given loop, lazily computing new
896     /// values if the loop hasn't been analyzed yet. The returned result is
897     /// guaranteed not to be predicated.
898     const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
899
900     /// Similar to getBackedgeTakenInfo, but will add predicates as required
901     /// with the purpose of returning complete information.
902     const BackedgeTakenInfo &getPredicatedBackedgeTakenInfo(const Loop *L);
903
904     /// Compute the number of times the specified loop will iterate.
905     /// If AllowPredicates is set, we will create new SCEV predicates as
906     /// necessary in order to return an exact answer.
907     BackedgeTakenInfo computeBackedgeTakenCount(const Loop *L,
908                                                 bool AllowPredicates = false);
909
910     /// Compute the number of times the backedge of the specified loop will
911     /// execute if it exits via the specified block. If AllowPredicates is set,
912     /// this call will try to use a minimal set of SCEV predicates in order to
913     /// return an exact answer.
914     ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
915                                bool AllowPredicates = false);
916
917     /// Compute the number of times the backedge of the specified loop will
918     /// execute if its exit condition were a conditional branch of ExitCond,
919     /// TBB, and FBB.
920     ///
921     /// \p ControlsExit is true if ExitCond directly controls the exit
922     /// branch. In this case, we can assume that the loop exits only if the
923     /// condition is true and can infer that failing to meet the condition prior
924     /// to integer wraparound results in undefined behavior.
925     ///
926     /// If \p AllowPredicates is set, this call will try to use a minimal set of
927     /// SCEV predicates in order to return an exact answer.
928     ExitLimit computeExitLimitFromCond(const Loop *L,
929                                        Value *ExitCond,
930                                        BasicBlock *TBB,
931                                        BasicBlock *FBB,
932                                        bool ControlsExit,
933                                        bool AllowPredicates = false);
934
935     /// Compute the number of times the backedge of the specified loop will
936     /// execute if its exit condition were a conditional branch of the ICmpInst
937     /// ExitCond, TBB, and FBB. If AllowPredicates is set, this call will try
938     /// to use a minimal set of SCEV predicates in order to return an exact
939     /// answer.
940     ExitLimit computeExitLimitFromICmp(const Loop *L,
941                                        ICmpInst *ExitCond,
942                                        BasicBlock *TBB,
943                                        BasicBlock *FBB,
944                                        bool IsSubExpr,
945                                        bool AllowPredicates = false);
946
947     /// Compute the number of times the backedge of the specified loop will
948     /// execute if its exit condition were a switch with a single exiting case
949     /// to ExitingBB.
950     ExitLimit
951     computeExitLimitFromSingleExitSwitch(const Loop *L, SwitchInst *Switch,
952                                BasicBlock *ExitingBB, bool IsSubExpr);
953
954     /// Given an exit condition of 'icmp op load X, cst', try to see if we can
955     /// compute the backedge-taken count.
956     ExitLimit computeLoadConstantCompareExitLimit(LoadInst *LI,
957                                                   Constant *RHS,
958                                                   const Loop *L,
959                                                   ICmpInst::Predicate p);
960
961     /// Compute the exit limit of a loop that is controlled by a
962     /// "(IV >> 1) != 0" type comparison.  We cannot compute the exact trip
963     /// count in these cases (since SCEV has no way of expressing them), but we
964     /// can still sometimes compute an upper bound.
965     ///
966     /// Return an ExitLimit for a loop whose backedge is guarded by `LHS Pred
967     /// RHS`.
968     ExitLimit computeShiftCompareExitLimit(Value *LHS, Value *RHS,
969                                            const Loop *L,
970                                            ICmpInst::Predicate Pred);
971
972     /// If the loop is known to execute a constant number of times (the
973     /// condition evolves only from constants), try to evaluate a few iterations
974     /// of the loop until we get the exit condition gets a value of ExitWhen
975     /// (true or false).  If we cannot evaluate the exit count of the loop,
976     /// return CouldNotCompute.
977     const SCEV *computeExitCountExhaustively(const Loop *L,
978                                              Value *Cond,
979                                              bool ExitWhen);
980
981     /// Return the number of times an exit condition comparing the specified
982     /// value to zero will execute.  If not computable, return CouldNotCompute.
983     /// If AllowPredicates is set, this call will try to use a minimal set of
984     /// SCEV predicates in order to return an exact answer.
985     ExitLimit howFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr,
986                            bool AllowPredicates = false);
987
988     /// Return the number of times an exit condition checking the specified
989     /// value for nonzero will execute.  If not computable, return
990     /// CouldNotCompute.
991     ExitLimit howFarToNonZero(const SCEV *V, const Loop *L);
992
993     /// Return the number of times an exit condition containing the specified
994     /// less-than comparison will execute.  If not computable, return
995     /// CouldNotCompute.
996     ///
997     /// \p isSigned specifies whether the less-than is signed.
998     ///
999     /// \p ControlsExit is true when the LHS < RHS condition directly controls
1000     /// the branch (loops exits only if condition is true). In this case, we can
1001     /// use NoWrapFlags to skip overflow checks.
1002     ///
1003     /// If \p AllowPredicates is set, this call will try to use a minimal set of
1004     /// SCEV predicates in order to return an exact answer.
1005     ExitLimit howManyLessThans(const SCEV *LHS, const SCEV *RHS, const Loop *L,
1006                                bool isSigned, bool ControlsExit,
1007                                bool AllowPredicates = false);
1008
1009     ExitLimit howManyGreaterThans(const SCEV *LHS, const SCEV *RHS,
1010                                   const Loop *L, bool isSigned, bool IsSubExpr,
1011                                   bool AllowPredicates = false);
1012
1013     /// Return a predecessor of BB (which may not be an immediate predecessor)
1014     /// which has exactly one successor from which BB is reachable, or null if
1015     /// no such block is found.
1016     std::pair<BasicBlock *, BasicBlock *>
1017     getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
1018
1019     /// Test whether the condition described by Pred, LHS, and RHS is true
1020     /// whenever the given FoundCondValue value evaluates to true.
1021     bool isImpliedCond(ICmpInst::Predicate Pred,
1022                        const SCEV *LHS, const SCEV *RHS,
1023                        Value *FoundCondValue,
1024                        bool Inverse);
1025
1026     /// Test whether the condition described by Pred, LHS, and RHS is true
1027     /// whenever the condition described by FoundPred, FoundLHS, FoundRHS is
1028     /// true.
1029     bool isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS,
1030                        const SCEV *RHS, ICmpInst::Predicate FoundPred,
1031                        const SCEV *FoundLHS, const SCEV *FoundRHS);
1032
1033     /// Test whether the condition described by Pred, LHS, and RHS is true
1034     /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1035     /// true.
1036     bool isImpliedCondOperands(ICmpInst::Predicate Pred,
1037                                const SCEV *LHS, const SCEV *RHS,
1038                                const SCEV *FoundLHS, const SCEV *FoundRHS);
1039
1040     /// Test whether the condition described by Pred, LHS, and RHS is true
1041     /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1042     /// true.
1043     bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
1044                                      const SCEV *LHS, const SCEV *RHS,
1045                                      const SCEV *FoundLHS,
1046                                      const SCEV *FoundRHS);
1047
1048     /// Test whether the condition described by Pred, LHS, and RHS is true
1049     /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1050     /// true.  Utility function used by isImpliedCondOperands.  Tries to get
1051     /// cases like "X `sgt` 0 => X - 1 `sgt` -1".
1052     bool isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred,
1053                                         const SCEV *LHS, const SCEV *RHS,
1054                                         const SCEV *FoundLHS,
1055                                         const SCEV *FoundRHS);
1056
1057     /// Return true if the condition denoted by \p LHS \p Pred \p RHS is implied
1058     /// by a call to \c @llvm.experimental.guard in \p BB.
1059     bool isImpliedViaGuard(BasicBlock *BB, ICmpInst::Predicate Pred,
1060                            const SCEV *LHS, const SCEV *RHS);
1061
1062     /// Test whether the condition described by Pred, LHS, and RHS is true
1063     /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1064     /// true.
1065     ///
1066     /// This routine tries to rule out certain kinds of integer overflow, and
1067     /// then tries to reason about arithmetic properties of the predicates.
1068     bool isImpliedCondOperandsViaNoOverflow(ICmpInst::Predicate Pred,
1069                                             const SCEV *LHS, const SCEV *RHS,
1070                                             const SCEV *FoundLHS,
1071                                             const SCEV *FoundRHS);
1072
1073     /// If we know that the specified Phi is in the header of its containing
1074     /// loop, we know the loop executes a constant number of times, and the PHI
1075     /// node is just a recurrence involving constants, fold it.
1076     Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
1077                                                 const Loop *L);
1078
1079     /// Test if the given expression is known to satisfy the condition described
1080     /// by Pred and the known constant ranges of LHS and RHS.
1081     ///
1082     bool isKnownPredicateViaConstantRanges(ICmpInst::Predicate Pred,
1083                                            const SCEV *LHS, const SCEV *RHS);
1084
1085     /// Try to prove the condition described by "LHS Pred RHS" by ruling out
1086     /// integer overflow.
1087     ///
1088     /// For instance, this will return true for "A s< (A + C)<nsw>" if C is
1089     /// positive.
1090     bool isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred,
1091                                        const SCEV *LHS, const SCEV *RHS);
1092
1093     /// Try to split Pred LHS RHS into logical conjunctions (and's) and try to
1094     /// prove them individually.
1095     bool isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, const SCEV *LHS,
1096                                       const SCEV *RHS);
1097
1098     /// Try to match the Expr as "(L + R)<Flags>".
1099     bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R,
1100                         SCEV::NoWrapFlags &Flags);
1101
1102     /// Return true if More == (Less + C), where C is a constant.  This is
1103     /// intended to be used as a cheaper substitute for full SCEV subtraction.
1104     bool computeConstantDifference(const SCEV *Less, const SCEV *More,
1105                                    APInt &C);
1106
1107     /// Drop memoized information computed for S.
1108     void forgetMemoizedResults(const SCEV *S);
1109
1110     /// Return an existing SCEV for V if there is one, otherwise return nullptr.
1111     const SCEV *getExistingSCEV(Value *V);
1112
1113     /// Return false iff given SCEV contains a SCEVUnknown with NULL value-
1114     /// pointer.
1115     bool checkValidity(const SCEV *S) const;
1116
1117     /// Return true if `ExtendOpTy`({`Start`,+,`Step`}) can be proved to be
1118     /// equal to {`ExtendOpTy`(`Start`),+,`ExtendOpTy`(`Step`)}.  This is
1119     /// equivalent to proving no signed (resp. unsigned) wrap in
1120     /// {`Start`,+,`Step`} if `ExtendOpTy` is `SCEVSignExtendExpr`
1121     /// (resp. `SCEVZeroExtendExpr`).
1122     ///
1123     template<typename ExtendOpTy>
1124     bool proveNoWrapByVaryingStart(const SCEV *Start, const SCEV *Step,
1125                                    const Loop *L);
1126
1127     /// Try to prove NSW or NUW on \p AR relying on ConstantRange manipulation.
1128     SCEV::NoWrapFlags proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR);
1129
1130     bool isMonotonicPredicateImpl(const SCEVAddRecExpr *LHS,
1131                                   ICmpInst::Predicate Pred, bool &Increasing);
1132
1133     /// Return true if, for all loop invariant X, the predicate "LHS `Pred` X"
1134     /// is monotonically increasing or decreasing.  In the former case set
1135     /// `Increasing` to true and in the latter case set `Increasing` to false.
1136     ///
1137     /// A predicate is said to be monotonically increasing if may go from being
1138     /// false to being true as the loop iterates, but never the other way
1139     /// around.  A predicate is said to be monotonically decreasing if may go
1140     /// from being true to being false as the loop iterates, but never the other
1141     /// way around.
1142     bool isMonotonicPredicate(const SCEVAddRecExpr *LHS,
1143                               ICmpInst::Predicate Pred, bool &Increasing);
1144
1145     /// Return SCEV no-wrap flags that can be proven based on reasoning about
1146     /// how poison produced from no-wrap flags on this value (e.g. a nuw add)
1147     /// would trigger undefined behavior on overflow.
1148     SCEV::NoWrapFlags getNoWrapFlagsFromUB(const Value *V);
1149
1150     /// Return true if the SCEV corresponding to \p I is never poison.  Proving
1151     /// this is more complex than proving that just \p I is never poison, since
1152     /// SCEV commons expressions across control flow, and you can have cases
1153     /// like:
1154     ///
1155     ///   idx0 = a + b;
1156     ///   ptr[idx0] = 100;
1157     ///   if (<condition>) {
1158     ///     idx1 = a +nsw b;
1159     ///     ptr[idx1] = 200;
1160     ///   }
1161     ///
1162     /// where the SCEV expression (+ a b) is guaranteed to not be poison (and
1163     /// hence not sign-overflow) only if "<condition>" is true.  Since both
1164     /// `idx0` and `idx1` will be mapped to the same SCEV expression, (+ a b),
1165     /// it is not okay to annotate (+ a b) with <nsw> in the above example.
1166     bool isSCEVExprNeverPoison(const Instruction *I);
1167
1168     /// This is like \c isSCEVExprNeverPoison but it specifically works for
1169     /// instructions that will get mapped to SCEV add recurrences.  Return true
1170     /// if \p I will never generate poison under the assumption that \p I is an
1171     /// add recurrence on the loop \p L.
1172     bool isAddRecNeverPoison(const Instruction *I, const Loop *L);
1173
1174   public:
1175     ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC,
1176                     DominatorTree &DT, LoopInfo &LI);
1177     ~ScalarEvolution();
1178     ScalarEvolution(ScalarEvolution &&Arg);
1179
1180     LLVMContext &getContext() const { return F.getContext(); }
1181
1182     /// Test if values of the given type are analyzable within the SCEV
1183     /// framework. This primarily includes integer types, and it can optionally
1184     /// include pointer types if the ScalarEvolution class has access to
1185     /// target-specific information.
1186     bool isSCEVable(Type *Ty) const;
1187
1188     /// Return the size in bits of the specified type, for which isSCEVable must
1189     /// return true.
1190     uint64_t getTypeSizeInBits(Type *Ty) const;
1191
1192     /// Return a type with the same bitwidth as the given type and which
1193     /// represents how SCEV will treat the given type, for which isSCEVable must
1194     /// return true. For pointer types, this is the pointer-sized integer type.
1195     Type *getEffectiveSCEVType(Type *Ty) const;
1196
1197     /// Return true if the SCEV is a scAddRecExpr or it contains
1198     /// scAddRecExpr. The result will be cached in HasRecMap.
1199     ///
1200     bool containsAddRecurrence(const SCEV *S);
1201
1202     /// Return the Value set from which the SCEV expr is generated.
1203     SetVector<ValueOffsetPair> *getSCEVValues(const SCEV *S);
1204
1205     /// Erase Value from ValueExprMap and ExprValueMap.
1206     void eraseValueFromMap(Value *V);
1207
1208     /// Return a SCEV expression for the full generality of the specified
1209     /// expression.
1210     const SCEV *getSCEV(Value *V);
1211
1212     const SCEV *getConstant(ConstantInt *V);
1213     const SCEV *getConstant(const APInt& Val);
1214     const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false);
1215     const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty);
1216     const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty);
1217     const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty);
1218     const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty);
1219     const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
1220                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
1221     const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
1222                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
1223       SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
1224       return getAddExpr(Ops, Flags);
1225     }
1226     const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
1227                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
1228       SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2};
1229       return getAddExpr(Ops, Flags);
1230     }
1231     const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
1232                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
1233     const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
1234                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
1235       SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
1236       return getMulExpr(Ops, Flags);
1237     }
1238     const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
1239                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
1240       SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2};
1241       return getMulExpr(Ops, Flags);
1242     }
1243     const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
1244     const SCEV *getUDivExactExpr(const SCEV *LHS, const SCEV *RHS);
1245     const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
1246                               const Loop *L, SCEV::NoWrapFlags Flags);
1247     const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
1248                               const Loop *L, SCEV::NoWrapFlags Flags);
1249     const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
1250                               const Loop *L, SCEV::NoWrapFlags Flags) {
1251       SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
1252       return getAddRecExpr(NewOp, L, Flags);
1253     }
1254     /// Returns an expression for a GEP
1255     ///
1256     /// \p PointeeType The type used as the basis for the pointer arithmetics
1257     /// \p BaseExpr The expression for the pointer operand.
1258     /// \p IndexExprs The expressions for the indices.
1259     /// \p InBounds Whether the GEP is in bounds.
1260     const SCEV *getGEPExpr(Type *PointeeType, const SCEV *BaseExpr,
1261                            const SmallVectorImpl<const SCEV *> &IndexExprs,
1262                            bool InBounds = false);
1263     const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
1264     const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
1265     const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
1266     const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
1267     const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
1268     const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
1269     const SCEV *getUnknown(Value *V);
1270     const SCEV *getCouldNotCompute();
1271
1272     /// Return a SCEV for the constant 0 of a specific type.
1273     const SCEV *getZero(Type *Ty) { return getConstant(Ty, 0); }
1274
1275     /// Return a SCEV for the constant 1 of a specific type.
1276     const SCEV *getOne(Type *Ty) { return getConstant(Ty, 1); }
1277
1278     /// Return an expression for sizeof AllocTy that is type IntTy
1279     ///
1280     const SCEV *getSizeOfExpr(Type *IntTy, Type *AllocTy);
1281
1282     /// Return an expression for offsetof on the given field with type IntTy
1283     ///
1284     const SCEV *getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo);
1285
1286     /// Return the SCEV object corresponding to -V.
1287     ///
1288     const SCEV *getNegativeSCEV(const SCEV *V,
1289                                 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
1290
1291     /// Return the SCEV object corresponding to ~V.
1292     ///
1293     const SCEV *getNotSCEV(const SCEV *V);
1294
1295     /// Return LHS-RHS.  Minus is represented in SCEV as A+B*-1.
1296     const SCEV *getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
1297                              SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
1298
1299     /// Return a SCEV corresponding to a conversion of the input value to the
1300     /// specified type.  If the type must be extended, it is zero extended.
1301     const SCEV *getTruncateOrZeroExtend(const SCEV *V, Type *Ty);
1302
1303     /// Return a SCEV corresponding to a conversion of the input value to the
1304     /// specified type.  If the type must be extended, it is sign extended.
1305     const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty);
1306
1307     /// Return a SCEV corresponding to a conversion of the input value to the
1308     /// specified type.  If the type must be extended, it is zero extended.  The
1309     /// conversion must not be narrowing.
1310     const SCEV *getNoopOrZeroExtend(const SCEV *V, Type *Ty);
1311
1312     /// Return a SCEV corresponding to a conversion of the input value to the
1313     /// specified type.  If the type must be extended, it is sign extended.  The
1314     /// conversion must not be narrowing.
1315     const SCEV *getNoopOrSignExtend(const SCEV *V, Type *Ty);
1316
1317     /// Return a SCEV corresponding to a conversion of the input value to the
1318     /// specified type. If the type must be extended, it is extended with
1319     /// unspecified bits. The conversion must not be narrowing.
1320     const SCEV *getNoopOrAnyExtend(const SCEV *V, Type *Ty);
1321
1322     /// Return a SCEV corresponding to a conversion of the input value to the
1323     /// specified type.  The conversion must not be widening.
1324     const SCEV *getTruncateOrNoop(const SCEV *V, Type *Ty);
1325
1326     /// Promote the operands to the wider of the types using zero-extension, and
1327     /// then perform a umax operation with them.
1328     const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS,
1329                                            const SCEV *RHS);
1330
1331     /// Promote the operands to the wider of the types using zero-extension, and
1332     /// then perform a umin operation with them.
1333     const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS,
1334                                            const SCEV *RHS);
1335
1336     /// Transitively follow the chain of pointer-type operands until reaching a
1337     /// SCEV that does not have a single pointer operand. This returns a
1338     /// SCEVUnknown pointer for well-formed pointer-type expressions, but corner
1339     /// cases do exist.
1340     const SCEV *getPointerBase(const SCEV *V);
1341
1342     /// Return a SCEV expression for the specified value at the specified scope
1343     /// in the program.  The L value specifies a loop nest to evaluate the
1344     /// expression at, where null is the top-level or a specified loop is
1345     /// immediately inside of the loop.
1346     ///
1347     /// This method can be used to compute the exit value for a variable defined
1348     /// in a loop by querying what the value will hold in the parent loop.
1349     ///
1350     /// In the case that a relevant loop exit value cannot be computed, the
1351     /// original value V is returned.
1352     const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
1353
1354     /// This is a convenience function which does getSCEVAtScope(getSCEV(V), L).
1355     const SCEV *getSCEVAtScope(Value *V, const Loop *L);
1356
1357     /// Test whether entry to the loop is protected by a conditional between LHS
1358     /// and RHS.  This is used to help avoid max expressions in loop trip
1359     /// counts, and to eliminate casts.
1360     bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
1361                                   const SCEV *LHS, const SCEV *RHS);
1362
1363     /// Test whether the backedge of the loop is protected by a conditional
1364     /// between LHS and RHS.  This is used to to eliminate casts.
1365     bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
1366                                      const SCEV *LHS, const SCEV *RHS);
1367
1368     /// Returns the maximum trip count of the loop if it is a single-exit
1369     /// loop and we can compute a small maximum for that loop.
1370     ///
1371     /// Implemented in terms of the \c getSmallConstantTripCount overload with
1372     /// the single exiting block passed to it. See that routine for details.
1373     unsigned getSmallConstantTripCount(Loop *L);
1374
1375     /// Returns the maximum trip count of this loop as a normal unsigned
1376     /// value. Returns 0 if the trip count is unknown or not constant. This
1377     /// "trip count" assumes that control exits via ExitingBlock. More
1378     /// precisely, it is the number of times that control may reach ExitingBlock
1379     /// before taking the branch. For loops with multiple exits, it may not be
1380     /// the number times that the loop header executes if the loop exits
1381     /// prematurely via another branch.
1382     unsigned getSmallConstantTripCount(Loop *L, BasicBlock *ExitingBlock);
1383
1384     /// Returns the largest constant divisor of the trip count of the
1385     /// loop if it is a single-exit loop and we can compute a small maximum for
1386     /// that loop.
1387     ///
1388     /// Implemented in terms of the \c getSmallConstantTripMultiple overload with
1389     /// the single exiting block passed to it. See that routine for details.
1390     unsigned getSmallConstantTripMultiple(Loop *L);
1391
1392     /// Returns the largest constant divisor of the trip count of this loop as a
1393     /// normal unsigned value, if possible. This means that the actual trip
1394     /// count is always a multiple of the returned value (don't forget the trip
1395     /// count could very well be zero as well!). As explained in the comments
1396     /// for getSmallConstantTripCount, this assumes that control exits the loop
1397     /// via ExitingBlock.
1398     unsigned getSmallConstantTripMultiple(Loop *L, BasicBlock *ExitingBlock);
1399
1400     /// Get the expression for the number of loop iterations for which this loop
1401     /// is guaranteed not to exit via ExitingBlock. Otherwise return
1402     /// SCEVCouldNotCompute.
1403     const SCEV *getExitCount(Loop *L, BasicBlock *ExitingBlock);
1404
1405     /// If the specified loop has a predictable backedge-taken count, return it,
1406     /// otherwise return a SCEVCouldNotCompute object. The backedge-taken count
1407     /// is the number of times the loop header will be branched to from within
1408     /// the loop. This is one less than the trip count of the loop, since it
1409     /// doesn't count the first iteration, when the header is branched to from
1410     /// outside the loop.
1411     ///
1412     /// Note that it is not valid to call this method on a loop without a
1413     /// loop-invariant backedge-taken count (see
1414     /// hasLoopInvariantBackedgeTakenCount).
1415     ///
1416     const SCEV *getBackedgeTakenCount(const Loop *L);
1417
1418     /// Similar to getBackedgeTakenCount, except it will add a set of
1419     /// SCEV predicates to Predicates that are required to be true in order for
1420     /// the answer to be correct. Predicates can be checked with run-time
1421     /// checks and can be used to perform loop versioning.
1422     const SCEV *getPredicatedBackedgeTakenCount(const Loop *L,
1423                                                 SCEVUnionPredicate &Predicates);
1424
1425     /// Similar to getBackedgeTakenCount, except return the least SCEV value
1426     /// that is known never to be less than the actual backedge taken count.
1427     const SCEV *getMaxBackedgeTakenCount(const Loop *L);
1428
1429     /// Return true if the specified loop has an analyzable loop-invariant
1430     /// backedge-taken count.
1431     bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
1432
1433     /// This method should be called by the client when it has changed a loop in
1434     /// a way that may effect ScalarEvolution's ability to compute a trip count,
1435     /// or if the loop is deleted.  This call is potentially expensive for large
1436     /// loop bodies.
1437     void forgetLoop(const Loop *L);
1438
1439     /// This method should be called by the client when it has changed a value
1440     /// in a way that may effect its value, or which may disconnect it from a
1441     /// def-use chain linking it to a loop.
1442     void forgetValue(Value *V);
1443
1444     /// Called when the client has changed the disposition of values in
1445     /// this loop.
1446     ///
1447     /// We don't have a way to invalidate per-loop dispositions. Clear and
1448     /// recompute is simpler.
1449     void forgetLoopDispositions(const Loop *L) { LoopDispositions.clear(); }
1450
1451     /// Determine the minimum number of zero bits that S is guaranteed to end in
1452     /// (at every loop iteration).  It is, at the same time, the minimum number
1453     /// of times S is divisible by 2.  For example, given {4,+,8} it returns 2.
1454     /// If S is guaranteed to be 0, it returns the bitwidth of S.
1455     uint32_t GetMinTrailingZeros(const SCEV *S);
1456
1457     /// Determine the unsigned range for a particular SCEV.
1458     ///
1459     ConstantRange getUnsignedRange(const SCEV *S) {
1460       return getRange(S, HINT_RANGE_UNSIGNED);
1461     }
1462
1463     /// Determine the signed range for a particular SCEV.
1464     ///
1465     ConstantRange getSignedRange(const SCEV *S) {
1466       return getRange(S, HINT_RANGE_SIGNED);
1467     }
1468
1469     /// Test if the given expression is known to be negative.
1470     ///
1471     bool isKnownNegative(const SCEV *S);
1472
1473     /// Test if the given expression is known to be positive.
1474     ///
1475     bool isKnownPositive(const SCEV *S);
1476
1477     /// Test if the given expression is known to be non-negative.
1478     ///
1479     bool isKnownNonNegative(const SCEV *S);
1480
1481     /// Test if the given expression is known to be non-positive.
1482     ///
1483     bool isKnownNonPositive(const SCEV *S);
1484
1485     /// Test if the given expression is known to be non-zero.
1486     ///
1487     bool isKnownNonZero(const SCEV *S);
1488
1489     /// Test if the given expression is known to satisfy the condition described
1490     /// by Pred, LHS, and RHS.
1491     ///
1492     bool isKnownPredicate(ICmpInst::Predicate Pred,
1493                           const SCEV *LHS, const SCEV *RHS);
1494
1495     /// Return true if the result of the predicate LHS `Pred` RHS is loop
1496     /// invariant with respect to L.  Set InvariantPred, InvariantLHS and
1497     /// InvariantLHS so that InvariantLHS `InvariantPred` InvariantRHS is the
1498     /// loop invariant form of LHS `Pred` RHS.
1499     bool isLoopInvariantPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
1500                                   const SCEV *RHS, const Loop *L,
1501                                   ICmpInst::Predicate &InvariantPred,
1502                                   const SCEV *&InvariantLHS,
1503                                   const SCEV *&InvariantRHS);
1504
1505     /// Simplify LHS and RHS in a comparison with predicate Pred. Return true
1506     /// iff any changes were made. If the operands are provably equal or
1507     /// unequal, LHS and RHS are set to the same value and Pred is set to either
1508     /// ICMP_EQ or ICMP_NE.
1509     ///
1510     bool SimplifyICmpOperands(ICmpInst::Predicate &Pred,
1511                               const SCEV *&LHS,
1512                               const SCEV *&RHS,
1513                               unsigned Depth = 0);
1514
1515     /// Return the "disposition" of the given SCEV with respect to the given
1516     /// loop.
1517     LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L);
1518
1519     /// Return true if the value of the given SCEV is unchanging in the
1520     /// specified loop.
1521     bool isLoopInvariant(const SCEV *S, const Loop *L);
1522
1523     /// Return true if the given SCEV changes value in a known way in the
1524     /// specified loop.  This property being true implies that the value is
1525     /// variant in the loop AND that we can emit an expression to compute the
1526     /// value of the expression at any particular loop iteration.
1527     bool hasComputableLoopEvolution(const SCEV *S, const Loop *L);
1528
1529     /// Return the "disposition" of the given SCEV with respect to the given
1530     /// block.
1531     BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB);
1532
1533     /// Return true if elements that makes up the given SCEV dominate the
1534     /// specified basic block.
1535     bool dominates(const SCEV *S, const BasicBlock *BB);
1536
1537     /// Return true if elements that makes up the given SCEV properly dominate
1538     /// the specified basic block.
1539     bool properlyDominates(const SCEV *S, const BasicBlock *BB);
1540
1541     /// Test whether the given SCEV has Op as a direct or indirect operand.
1542     bool hasOperand(const SCEV *S, const SCEV *Op) const;
1543
1544     /// Return the size of an element read or written by Inst.
1545     const SCEV *getElementSize(Instruction *Inst);
1546
1547     /// Compute the array dimensions Sizes from the set of Terms extracted from
1548     /// the memory access function of this SCEVAddRecExpr (second step of
1549     /// delinearization).
1550     void findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
1551                              SmallVectorImpl<const SCEV *> &Sizes,
1552                              const SCEV *ElementSize) const;
1553
1554     void print(raw_ostream &OS) const;
1555     void verify() const;
1556
1557     /// Collect parametric terms occurring in step expressions (first step of
1558     /// delinearization).
1559     void collectParametricTerms(const SCEV *Expr,
1560                                 SmallVectorImpl<const SCEV *> &Terms);
1561
1562
1563
1564     /// Return in Subscripts the access functions for each dimension in Sizes
1565     /// (third step of delinearization).
1566     void computeAccessFunctions(const SCEV *Expr,
1567                                 SmallVectorImpl<const SCEV *> &Subscripts,
1568                                 SmallVectorImpl<const SCEV *> &Sizes);
1569
1570     /// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
1571     /// subscripts and sizes of an array access.
1572     ///
1573     /// The delinearization is a 3 step process: the first two steps compute the
1574     /// sizes of each subscript and the third step computes the access functions
1575     /// for the delinearized array:
1576     ///
1577     /// 1. Find the terms in the step functions
1578     /// 2. Compute the array size
1579     /// 3. Compute the access function: divide the SCEV by the array size
1580     ///    starting with the innermost dimensions found in step 2. The Quotient
1581     ///    is the SCEV to be divided in the next step of the recursion. The
1582     ///    Remainder is the subscript of the innermost dimension. Loop over all
1583     ///    array dimensions computed in step 2.
1584     ///
1585     /// To compute a uniform array size for several memory accesses to the same
1586     /// object, one can collect in step 1 all the step terms for all the memory
1587     /// accesses, and compute in step 2 a unique array shape. This guarantees
1588     /// that the array shape will be the same across all memory accesses.
1589     ///
1590     /// FIXME: We could derive the result of steps 1 and 2 from a description of
1591     /// the array shape given in metadata.
1592     ///
1593     /// Example:
1594     ///
1595     /// A[][n][m]
1596     ///
1597     /// for i
1598     ///   for j
1599     ///     for k
1600     ///       A[j+k][2i][5i] =
1601     ///
1602     /// The initial SCEV:
1603     ///
1604     /// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k]
1605     ///
1606     /// 1. Find the different terms in the step functions:
1607     /// -> [2*m, 5, n*m, n*m]
1608     ///
1609     /// 2. Compute the array size: sort and unique them
1610     /// -> [n*m, 2*m, 5]
1611     /// find the GCD of all the terms = 1
1612     /// divide by the GCD and erase constant terms
1613     /// -> [n*m, 2*m]
1614     /// GCD = m
1615     /// divide by GCD -> [n, 2]
1616     /// remove constant terms
1617     /// -> [n]
1618     /// size of the array is A[unknown][n][m]
1619     ///
1620     /// 3. Compute the access function
1621     /// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m
1622     /// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k
1623     /// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k
1624     /// The remainder is the subscript of the innermost array dimension: [5i].
1625     ///
1626     /// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n
1627     /// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k
1628     /// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k
1629     /// The Remainder is the subscript of the next array dimension: [2i].
1630     ///
1631     /// The subscript of the outermost dimension is the Quotient: [j+k].
1632     ///
1633     /// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
1634     void delinearize(const SCEV *Expr,
1635                      SmallVectorImpl<const SCEV *> &Subscripts,
1636                      SmallVectorImpl<const SCEV *> &Sizes,
1637                      const SCEV *ElementSize);
1638
1639     /// Return the DataLayout associated with the module this SCEV instance is
1640     /// operating on.
1641     const DataLayout &getDataLayout() const {
1642       return F.getParent()->getDataLayout();
1643     }
1644
1645     const SCEVPredicate *getEqualPredicate(const SCEVUnknown *LHS,
1646                                            const SCEVConstant *RHS);
1647
1648     const SCEVPredicate *
1649     getWrapPredicate(const SCEVAddRecExpr *AR,
1650                      SCEVWrapPredicate::IncrementWrapFlags AddedFlags);
1651
1652     /// Re-writes the SCEV according to the Predicates in \p A.
1653     const SCEV *rewriteUsingPredicate(const SCEV *S, const Loop *L,
1654                                       SCEVUnionPredicate &A);
1655     /// Tries to convert the \p S expression to an AddRec expression,
1656     /// adding additional predicates to \p Preds as required.
1657     const SCEVAddRecExpr *
1658     convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L,
1659                                       SCEVUnionPredicate &Preds);
1660
1661   private:
1662     /// Compute the backedge taken count knowing the interval difference, the
1663     /// stride and presence of the equality in the comparison.
1664     const SCEV *computeBECount(const SCEV *Delta, const SCEV *Stride,
1665                                bool Equality);
1666
1667     /// Verify if an linear IV with positive stride can overflow when in a
1668     /// less-than comparison, knowing the invariant term of the comparison,
1669     /// the stride and the knowledge of NSW/NUW flags on the recurrence.
1670     bool doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
1671                             bool IsSigned, bool NoWrap);
1672
1673     /// Verify if an linear IV with negative stride can overflow when in a
1674     /// greater-than comparison, knowing the invariant term of the comparison,
1675     /// the stride and the knowledge of NSW/NUW flags on the recurrence.
1676     bool doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
1677                             bool IsSigned, bool NoWrap);
1678
1679   private:
1680     FoldingSet<SCEV> UniqueSCEVs;
1681     FoldingSet<SCEVPredicate> UniquePreds;
1682     BumpPtrAllocator SCEVAllocator;
1683
1684     /// The head of a linked list of all SCEVUnknown values that have been
1685     /// allocated. This is used by releaseMemory to locate them all and call
1686     /// their destructors.
1687     SCEVUnknown *FirstUnknown;
1688   };
1689
1690   /// Analysis pass that exposes the \c ScalarEvolution for a function.
1691   class ScalarEvolutionAnalysis
1692       : public AnalysisInfoMixin<ScalarEvolutionAnalysis> {
1693     friend AnalysisInfoMixin<ScalarEvolutionAnalysis>;
1694     static char PassID;
1695
1696   public:
1697     typedef ScalarEvolution Result;
1698
1699     ScalarEvolution run(Function &F, AnalysisManager<Function> &AM);
1700   };
1701
1702   /// Printer pass for the \c ScalarEvolutionAnalysis results.
1703   class ScalarEvolutionPrinterPass
1704       : public PassInfoMixin<ScalarEvolutionPrinterPass> {
1705     raw_ostream &OS;
1706
1707   public:
1708     explicit ScalarEvolutionPrinterPass(raw_ostream &OS) : OS(OS) {}
1709     PreservedAnalyses run(Function &F, AnalysisManager<Function> &AM);
1710   };
1711
1712   class ScalarEvolutionWrapperPass : public FunctionPass {
1713     std::unique_ptr<ScalarEvolution> SE;
1714
1715   public:
1716     static char ID;
1717
1718     ScalarEvolutionWrapperPass();
1719
1720     ScalarEvolution &getSE() { return *SE; }
1721     const ScalarEvolution &getSE() const { return *SE; }
1722
1723     bool runOnFunction(Function &F) override;
1724     void releaseMemory() override;
1725     void getAnalysisUsage(AnalysisUsage &AU) const override;
1726     void print(raw_ostream &OS, const Module * = nullptr) const override;
1727     void verifyAnalysis() const override;
1728   };
1729
1730   /// An interface layer with SCEV used to manage how we see SCEV expressions
1731   /// for values in the context of existing predicates. We can add new
1732   /// predicates, but we cannot remove them.
1733   ///
1734   /// This layer has multiple purposes:
1735   ///   - provides a simple interface for SCEV versioning.
1736   ///   - guarantees that the order of transformations applied on a SCEV
1737   ///     expression for a single Value is consistent across two different
1738   ///     getSCEV calls. This means that, for example, once we've obtained
1739   ///     an AddRec expression for a certain value through expression
1740   ///     rewriting, we will continue to get an AddRec expression for that
1741   ///     Value.
1742   ///   - lowers the number of expression rewrites.
1743   class PredicatedScalarEvolution {
1744   public:
1745     PredicatedScalarEvolution(ScalarEvolution &SE, Loop &L);
1746     const SCEVUnionPredicate &getUnionPredicate() const;
1747
1748     /// Returns the SCEV expression of V, in the context of the current SCEV
1749     /// predicate.  The order of transformations applied on the expression of V
1750     /// returned by ScalarEvolution is guaranteed to be preserved, even when
1751     /// adding new predicates.
1752     const SCEV *getSCEV(Value *V);
1753
1754     /// Get the (predicated) backedge count for the analyzed loop.
1755     const SCEV *getBackedgeTakenCount();
1756
1757     /// Adds a new predicate.
1758     void addPredicate(const SCEVPredicate &Pred);
1759
1760     /// Attempts to produce an AddRecExpr for V by adding additional SCEV
1761     /// predicates. If we can't transform the expression into an AddRecExpr we
1762     /// return nullptr and not add additional SCEV predicates to the current
1763     /// context.
1764     const SCEVAddRecExpr *getAsAddRec(Value *V);
1765
1766     /// Proves that V doesn't overflow by adding SCEV predicate.
1767     void setNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags);
1768
1769     /// Returns true if we've proved that V doesn't wrap by means of a SCEV
1770     /// predicate.
1771     bool hasNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags);
1772
1773     /// Returns the ScalarEvolution analysis used.
1774     ScalarEvolution *getSE() const { return &SE; }
1775
1776     /// We need to explicitly define the copy constructor because of FlagsMap.
1777     PredicatedScalarEvolution(const PredicatedScalarEvolution&);
1778
1779     /// Print the SCEV mappings done by the Predicated Scalar Evolution.
1780     /// The printed text is indented by \p Depth.
1781     void print(raw_ostream &OS, unsigned Depth) const;
1782
1783   private:
1784     /// Increments the version number of the predicate.  This needs to be called
1785     /// every time the SCEV predicate changes.
1786     void updateGeneration();
1787
1788     /// Holds a SCEV and the version number of the SCEV predicate used to
1789     /// perform the rewrite of the expression.
1790     typedef std::pair<unsigned, const SCEV *> RewriteEntry;
1791
1792     /// Maps a SCEV to the rewrite result of that SCEV at a certain version
1793     /// number. If this number doesn't match the current Generation, we will
1794     /// need to do a rewrite. To preserve the transformation order of previous
1795     /// rewrites, we will rewrite the previous result instead of the original
1796     /// SCEV.
1797     DenseMap<const SCEV *, RewriteEntry> RewriteMap;
1798
1799     /// Records what NoWrap flags we've added to a Value *.
1800     ValueMap<Value *, SCEVWrapPredicate::IncrementWrapFlags> FlagsMap;
1801
1802     /// The ScalarEvolution analysis.
1803     ScalarEvolution &SE;
1804
1805     /// The analyzed Loop.
1806     const Loop &L;
1807
1808     /// The SCEVPredicate that forms our context. We will rewrite all
1809     /// expressions assuming that this predicate true.
1810     SCEVUnionPredicate Preds;
1811
1812     /// Marks the version of the SCEV predicate used. When rewriting a SCEV
1813     /// expression we mark it with the version of the predicate. We use this to
1814     /// figure out if the predicate has changed from the last rewrite of the
1815     /// SCEV. If so, we need to perform a new rewrite.
1816     unsigned Generation;
1817
1818     /// The backedge taken count.
1819     const SCEV *BackedgeCount;
1820   };
1821 }
1822
1823 #endif