Home | History | Annotate | Line # | Download | only in Analysis
      1 //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 //
      9 // The ScalarEvolution class is an LLVM pass which can be used to analyze and
     10 // categorize scalar expressions in loops.  It specializes in recognizing
     11 // general induction variables, representing them with the abstract and opaque
     12 // SCEV class.  Given this analysis, trip counts of loops and other important
     13 // properties can be obtained.
     14 //
     15 // This analysis is primarily useful for induction variable substitution and
     16 // strength reduction.
     17 //
     18 //===----------------------------------------------------------------------===//
     19 
     20 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
     21 #define LLVM_ANALYSIS_SCALAREVOLUTION_H
     22 
     23 #include "llvm/ADT/APInt.h"
     24 #include "llvm/ADT/ArrayRef.h"
     25 #include "llvm/ADT/DenseMap.h"
     26 #include "llvm/ADT/DenseMapInfo.h"
     27 #include "llvm/ADT/FoldingSet.h"
     28 #include "llvm/ADT/Hashing.h"
     29 #include "llvm/ADT/Optional.h"
     30 #include "llvm/ADT/PointerIntPair.h"
     31 #include "llvm/ADT/SetVector.h"
     32 #include "llvm/ADT/SmallPtrSet.h"
     33 #include "llvm/ADT/SmallVector.h"
     34 #include "llvm/IR/ConstantRange.h"
     35 #include "llvm/IR/Function.h"
     36 #include "llvm/IR/InstrTypes.h"
     37 #include "llvm/IR/Instructions.h"
     38 #include "llvm/IR/Operator.h"
     39 #include "llvm/IR/PassManager.h"
     40 #include "llvm/IR/ValueHandle.h"
     41 #include "llvm/IR/ValueMap.h"
     42 #include "llvm/Pass.h"
     43 #include "llvm/Support/Allocator.h"
     44 #include "llvm/Support/Casting.h"
     45 #include "llvm/Support/Compiler.h"
     46 #include <algorithm>
     47 #include <cassert>
     48 #include <cstdint>
     49 #include <memory>
     50 #include <utility>
     51 
     52 namespace llvm {
     53 
     54 class AssumptionCache;
     55 class BasicBlock;
     56 class Constant;
     57 class ConstantInt;
     58 class DataLayout;
     59 class DominatorTree;
     60 class GEPOperator;
     61 class Instruction;
     62 class LLVMContext;
     63 class Loop;
     64 class LoopInfo;
     65 class raw_ostream;
     66 class ScalarEvolution;
     67 class SCEVAddRecExpr;
     68 class SCEVUnknown;
     69 class StructType;
     70 class TargetLibraryInfo;
     71 class Type;
     72 class Value;
     73 enum SCEVTypes : unsigned short;
     74 
     75 /// This class represents an analyzed expression in the program.  These are
     76 /// opaque objects that the client is not allowed to do much with directly.
     77 ///
     78 class SCEV : public FoldingSetNode {
     79   friend struct FoldingSetTrait<SCEV>;
     80 
     81   /// A reference to an Interned FoldingSetNodeID for this node.  The
     82   /// ScalarEvolution's BumpPtrAllocator holds the data.
     83   FoldingSetNodeIDRef FastID;
     84 
     85   // The SCEV baseclass this node corresponds to
     86   const SCEVTypes SCEVType;
     87 
     88 protected:
     89   // Estimated complexity of this node's expression tree size.
     90   const unsigned short ExpressionSize;
     91 
     92   /// This field is initialized to zero and may be used in subclasses to store
     93   /// miscellaneous information.
     94   unsigned short SubclassData = 0;
     95 
     96 public:
     97   /// NoWrapFlags are bitfield indices into SubclassData.
     98   ///
     99   /// Add and Mul expressions may have no-unsigned-wrap <NUW> or
    100   /// no-signed-wrap <NSW> properties, which are derived from the IR
    101   /// operator. NSW is a misnomer that we use to mean no signed overflow or
    102   /// underflow.
    103   ///
    104   /// AddRec expressions may have a no-self-wraparound <NW> property if, in
    105   /// the integer domain, abs(step) * max-iteration(loop) <=
    106   /// unsigned-max(bitwidth).  This means that the recurrence will never reach
    107   /// its start value if the step is non-zero.  Computing the same value on
    108   /// each iteration is not considered wrapping, and recurrences with step = 0
    109   /// are trivially <NW>.  <NW> is independent of the sign of step and the
    110   /// value the add recurrence starts with.
    111   ///
    112   /// Note that NUW and NSW are also valid properties of a recurrence, and
    113   /// either implies NW. For convenience, NW will be set for a recurrence
    114   /// whenever either NUW or NSW are set.
    115   enum NoWrapFlags {
    116     FlagAnyWrap = 0,    // No guarantee.
    117     FlagNW = (1 << 0),  // No self-wrap.
    118     FlagNUW = (1 << 1), // No unsigned wrap.
    119     FlagNSW = (1 << 2), // No signed wrap.
    120     NoWrapMask = (1 << 3) - 1
    121   };
    122 
    123   explicit SCEV(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy,
    124                 unsigned short ExpressionSize)
    125       : FastID(ID), SCEVType(SCEVTy), ExpressionSize(ExpressionSize) {}
    126   SCEV(const SCEV &) = delete;
    127   SCEV &operator=(const SCEV &) = delete;
    128 
    129   SCEVTypes getSCEVType() const { return SCEVType; }
    130 
    131   /// Return the LLVM type of this SCEV expression.
    132   Type *getType() const;
    133 
    134   /// Return true if the expression is a constant zero.
    135   bool isZero() const;
    136 
    137   /// Return true if the expression is a constant one.
    138   bool isOne() const;
    139 
    140   /// Return true if the expression is a constant all-ones value.
    141   bool isAllOnesValue() const;
    142 
    143   /// Return true if the specified scev is negated, but not a constant.
    144   bool isNonConstantNegative() const;
    145 
    146   // Returns estimated size of the mathematical expression represented by this
    147   // SCEV. The rules of its calculation are following:
    148   // 1) Size of a SCEV without operands (like constants and SCEVUnknown) is 1;
    149   // 2) Size SCEV with operands Op1, Op2, ..., OpN is calculated by formula:
    150   //    (1 + Size(Op1) + ... + Size(OpN)).
    151   // This value gives us an estimation of time we need to traverse through this
    152   // SCEV and all its operands recursively. We may use it to avoid performing
    153   // heavy transformations on SCEVs of excessive size for sake of saving the
    154   // compilation time.
    155   unsigned short getExpressionSize() const {
    156     return ExpressionSize;
    157   }
    158 
    159   /// Print out the internal representation of this scalar to the specified
    160   /// stream.  This should really only be used for debugging purposes.
    161   void print(raw_ostream &OS) const;
    162 
    163   /// This method is used for debugging.
    164   void dump() const;
    165 };
    166 
    167 // Specialize FoldingSetTrait for SCEV to avoid needing to compute
    168 // temporary FoldingSetNodeID values.
    169 template <> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> {
    170   static void Profile(const SCEV &X, FoldingSetNodeID &ID) { ID = X.FastID; }
    171 
    172   static bool Equals(const SCEV &X, const FoldingSetNodeID &ID, unsigned IDHash,
    173                      FoldingSetNodeID &TempID) {
    174     return ID == X.FastID;
    175   }
    176 
    177   static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) {
    178     return X.FastID.ComputeHash();
    179   }
    180 };
    181 
    182 inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
    183   S.print(OS);
    184   return OS;
    185 }
    186 
    187 /// An object of this class is returned by queries that could not be answered.
    188 /// For example, if you ask for the number of iterations of a linked-list
    189 /// traversal loop, you will get one of these.  None of the standard SCEV
    190 /// operations are valid on this class, it is just a marker.
    191 struct SCEVCouldNotCompute : public SCEV {
    192   SCEVCouldNotCompute();
    193 
    194   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    195   static bool classof(const SCEV *S);
    196 };
    197 
    198 /// This class represents an assumption made using SCEV expressions which can
    199 /// be checked at run-time.
    200 class SCEVPredicate : public FoldingSetNode {
    201   friend struct FoldingSetTrait<SCEVPredicate>;
    202 
    203   /// A reference to an Interned FoldingSetNodeID for this node.  The
    204   /// ScalarEvolution's BumpPtrAllocator holds the data.
    205   FoldingSetNodeIDRef FastID;
    206 
    207 public:
    208   enum SCEVPredicateKind { P_Union, P_Equal, P_Wrap };
    209 
    210 protected:
    211   SCEVPredicateKind Kind;
    212   ~SCEVPredicate() = default;
    213   SCEVPredicate(const SCEVPredicate &) = default;
    214   SCEVPredicate &operator=(const SCEVPredicate &) = default;
    215 
    216 public:
    217   SCEVPredicate(const FoldingSetNodeIDRef ID, SCEVPredicateKind Kind);
    218 
    219   SCEVPredicateKind getKind() const { return Kind; }
    220 
    221   /// Returns the estimated complexity of this predicate.  This is roughly
    222   /// measured in the number of run-time checks required.
    223   virtual unsigned getComplexity() const { return 1; }
    224 
    225   /// Returns true if the predicate is always true. This means that no
    226   /// assumptions were made and nothing needs to be checked at run-time.
    227   virtual bool isAlwaysTrue() const = 0;
    228 
    229   /// Returns true if this predicate implies \p N.
    230   virtual bool implies(const SCEVPredicate *N) const = 0;
    231 
    232   /// Prints a textual representation of this predicate with an indentation of
    233   /// \p Depth.
    234   virtual void print(raw_ostream &OS, unsigned Depth = 0) const = 0;
    235 
    236   /// Returns the SCEV to which this predicate applies, or nullptr if this is
    237   /// a SCEVUnionPredicate.
    238   virtual const SCEV *getExpr() const = 0;
    239 };
    240 
    241 inline raw_ostream &operator<<(raw_ostream &OS, const SCEVPredicate &P) {
    242   P.print(OS);
    243   return OS;
    244 }
    245 
    246 // Specialize FoldingSetTrait for SCEVPredicate to avoid needing to compute
    247 // temporary FoldingSetNodeID values.
    248 template <>
    249 struct FoldingSetTrait<SCEVPredicate> : DefaultFoldingSetTrait<SCEVPredicate> {
    250   static void Profile(const SCEVPredicate &X, FoldingSetNodeID &ID) {
    251     ID = X.FastID;
    252   }
    253 
    254   static bool Equals(const SCEVPredicate &X, const FoldingSetNodeID &ID,
    255                      unsigned IDHash, FoldingSetNodeID &TempID) {
    256     return ID == X.FastID;
    257   }
    258 
    259   static unsigned ComputeHash(const SCEVPredicate &X,
    260                               FoldingSetNodeID &TempID) {
    261     return X.FastID.ComputeHash();
    262   }
    263 };
    264 
    265 /// This class represents an assumption that two SCEV expressions are equal,
    266 /// and this can be checked at run-time.
    267 class SCEVEqualPredicate final : public SCEVPredicate {
    268   /// We assume that LHS == RHS.
    269   const SCEV *LHS;
    270   const SCEV *RHS;
    271 
    272 public:
    273   SCEVEqualPredicate(const FoldingSetNodeIDRef ID, const SCEV *LHS,
    274                      const SCEV *RHS);
    275 
    276   /// Implementation of the SCEVPredicate interface
    277   bool implies(const SCEVPredicate *N) const override;
    278   void print(raw_ostream &OS, unsigned Depth = 0) const override;
    279   bool isAlwaysTrue() const override;
    280   const SCEV *getExpr() const override;
    281 
    282   /// Returns the left hand side of the equality.
    283   const SCEV *getLHS() const { return LHS; }
    284 
    285   /// Returns the right hand side of the equality.
    286   const SCEV *getRHS() const { return RHS; }
    287 
    288   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    289   static bool classof(const SCEVPredicate *P) {
    290     return P->getKind() == P_Equal;
    291   }
    292 };
    293 
    294 /// This class represents an assumption made on an AddRec expression. Given an
    295 /// affine AddRec expression {a,+,b}, we assume that it has the nssw or nusw
    296 /// flags (defined below) in the first X iterations of the loop, where X is a
    297 /// SCEV expression returned by getPredicatedBackedgeTakenCount).
    298 ///
    299 /// Note that this does not imply that X is equal to the backedge taken
    300 /// count. This means that if we have a nusw predicate for i32 {0,+,1} with a
    301 /// predicated backedge taken count of X, we only guarantee that {0,+,1} has
    302 /// nusw in the first X iterations. {0,+,1} may still wrap in the loop if we
    303 /// have more than X iterations.
    304 class SCEVWrapPredicate final : public SCEVPredicate {
    305 public:
    306   /// Similar to SCEV::NoWrapFlags, but with slightly different semantics
    307   /// for FlagNUSW. The increment is considered to be signed, and a + b
    308   /// (where b is the increment) is considered to wrap if:
    309   ///    zext(a + b) != zext(a) + sext(b)
    310   ///
    311   /// If Signed is a function that takes an n-bit tuple and maps to the
    312   /// integer domain as the tuples value interpreted as twos complement,
    313   /// and Unsigned a function that takes an n-bit tuple and maps to the
    314   /// integer domain as as the base two value of input tuple, then a + b
    315   /// has IncrementNUSW iff:
    316   ///
    317   /// 0 <= Unsigned(a) + Signed(b) < 2^n
    318   ///
    319   /// The IncrementNSSW flag has identical semantics with SCEV::FlagNSW.
    320   ///
    321   /// Note that the IncrementNUSW flag is not commutative: if base + inc
    322   /// has IncrementNUSW, then inc + base doesn't neccessarily have this
    323   /// property. The reason for this is that this is used for sign/zero
    324   /// extending affine AddRec SCEV expressions when a SCEVWrapPredicate is
    325   /// assumed. A {base,+,inc} expression is already non-commutative with
    326   /// regards to base and inc, since it is interpreted as:
    327   ///     (((base + inc) + inc) + inc) ...
    328   enum IncrementWrapFlags {
    329     IncrementAnyWrap = 0,     // No guarantee.
    330     IncrementNUSW = (1 << 0), // No unsigned with signed increment wrap.
    331     IncrementNSSW = (1 << 1), // No signed with signed increment wrap
    332                               // (equivalent with SCEV::NSW)
    333     IncrementNoWrapMask = (1 << 2) - 1
    334   };
    335 
    336   /// Convenient IncrementWrapFlags manipulation methods.
    337   LLVM_NODISCARD static SCEVWrapPredicate::IncrementWrapFlags
    338   clearFlags(SCEVWrapPredicate::IncrementWrapFlags Flags,
    339              SCEVWrapPredicate::IncrementWrapFlags OffFlags) {
    340     assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
    341     assert((OffFlags & IncrementNoWrapMask) == OffFlags &&
    342            "Invalid flags value!");
    343     return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & ~OffFlags);
    344   }
    345 
    346   LLVM_NODISCARD static SCEVWrapPredicate::IncrementWrapFlags
    347   maskFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, int Mask) {
    348     assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
    349     assert((Mask & IncrementNoWrapMask) == Mask && "Invalid mask value!");
    350 
    351     return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & Mask);
    352   }
    353 
    354   LLVM_NODISCARD static SCEVWrapPredicate::IncrementWrapFlags
    355   setFlags(SCEVWrapPredicate::IncrementWrapFlags Flags,
    356            SCEVWrapPredicate::IncrementWrapFlags OnFlags) {
    357     assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
    358     assert((OnFlags & IncrementNoWrapMask) == OnFlags &&
    359            "Invalid flags value!");
    360 
    361     return (SCEVWrapPredicate::IncrementWrapFlags)(Flags | OnFlags);
    362   }
    363 
    364   /// Returns the set of SCEVWrapPredicate no wrap flags implied by a
    365   /// SCEVAddRecExpr.
    366   LLVM_NODISCARD static SCEVWrapPredicate::IncrementWrapFlags
    367   getImpliedFlags(const SCEVAddRecExpr *AR, ScalarEvolution &SE);
    368 
    369 private:
    370   const SCEVAddRecExpr *AR;
    371   IncrementWrapFlags Flags;
    372 
    373 public:
    374   explicit SCEVWrapPredicate(const FoldingSetNodeIDRef ID,
    375                              const SCEVAddRecExpr *AR,
    376                              IncrementWrapFlags Flags);
    377 
    378   /// Returns the set assumed no overflow flags.
    379   IncrementWrapFlags getFlags() const { return Flags; }
    380 
    381   /// Implementation of the SCEVPredicate interface
    382   const SCEV *getExpr() const override;
    383   bool implies(const SCEVPredicate *N) const override;
    384   void print(raw_ostream &OS, unsigned Depth = 0) const override;
    385   bool isAlwaysTrue() const override;
    386 
    387   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    388   static bool classof(const SCEVPredicate *P) {
    389     return P->getKind() == P_Wrap;
    390   }
    391 };
    392 
    393 /// This class represents a composition of other SCEV predicates, and is the
    394 /// class that most clients will interact with.  This is equivalent to a
    395 /// logical "AND" of all the predicates in the union.
    396 ///
    397 /// NB! Unlike other SCEVPredicate sub-classes this class does not live in the
    398 /// ScalarEvolution::Preds folding set.  This is why the \c add function is sound.
    399 class SCEVUnionPredicate final : public SCEVPredicate {
    400 private:
    401   using PredicateMap =
    402       DenseMap<const SCEV *, SmallVector<const SCEVPredicate *, 4>>;
    403 
    404   /// Vector with references to all predicates in this union.
    405   SmallVector<const SCEVPredicate *, 16> Preds;
    406 
    407   /// Maps SCEVs to predicates for quick look-ups.
    408   PredicateMap SCEVToPreds;
    409 
    410 public:
    411   SCEVUnionPredicate();
    412 
    413   const SmallVectorImpl<const SCEVPredicate *> &getPredicates() const {
    414     return Preds;
    415   }
    416 
    417   /// Adds a predicate to this union.
    418   void add(const SCEVPredicate *N);
    419 
    420   /// Returns a reference to a vector containing all predicates which apply to
    421   /// \p Expr.
    422   ArrayRef<const SCEVPredicate *> getPredicatesForExpr(const SCEV *Expr);
    423 
    424   /// Implementation of the SCEVPredicate interface
    425   bool isAlwaysTrue() const override;
    426   bool implies(const SCEVPredicate *N) const override;
    427   void print(raw_ostream &OS, unsigned Depth) const override;
    428   const SCEV *getExpr() const override;
    429 
    430   /// We estimate the complexity of a union predicate as the size number of
    431   /// predicates in the union.
    432   unsigned getComplexity() const override { return Preds.size(); }
    433 
    434   /// Methods for support type inquiry through isa, cast, and dyn_cast:
    435   static bool classof(const SCEVPredicate *P) {
    436     return P->getKind() == P_Union;
    437   }
    438 };
    439 
    440 /// The main scalar evolution driver. Because client code (intentionally)
    441 /// can't do much with the SCEV objects directly, they must ask this class
    442 /// for services.
    443 class ScalarEvolution {
    444   friend class ScalarEvolutionsTest;
    445 
    446 public:
    447   /// An enum describing the relationship between a SCEV and a loop.
    448   enum LoopDisposition {
    449     LoopVariant,   ///< The SCEV is loop-variant (unknown).
    450     LoopInvariant, ///< The SCEV is loop-invariant.
    451     LoopComputable ///< The SCEV varies predictably with the loop.
    452   };
    453 
    454   /// An enum describing the relationship between a SCEV and a basic block.
    455   enum BlockDisposition {
    456     DoesNotDominateBlock,  ///< The SCEV does not dominate the block.
    457     DominatesBlock,        ///< The SCEV dominates the block.
    458     ProperlyDominatesBlock ///< The SCEV properly dominates the block.
    459   };
    460 
    461   /// Convenient NoWrapFlags manipulation that hides enum casts and is
    462   /// visible in the ScalarEvolution name space.
    463   LLVM_NODISCARD static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags,
    464                                                     int Mask) {
    465     return (SCEV::NoWrapFlags)(Flags & Mask);
    466   }
    467   LLVM_NODISCARD static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags,
    468                                                    SCEV::NoWrapFlags OnFlags) {
    469     return (SCEV::NoWrapFlags)(Flags | OnFlags);
    470   }
    471   LLVM_NODISCARD static SCEV::NoWrapFlags
    472   clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags) {
    473     return (SCEV::NoWrapFlags)(Flags & ~OffFlags);
    474   }
    475 
    476   ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC,
    477                   DominatorTree &DT, LoopInfo &LI);
    478   ScalarEvolution(ScalarEvolution &&Arg);
    479   ~ScalarEvolution();
    480 
    481   LLVMContext &getContext() const { return F.getContext(); }
    482 
    483   /// Test if values of the given type are analyzable within the SCEV
    484   /// framework. This primarily includes integer types, and it can optionally
    485   /// include pointer types if the ScalarEvolution class has access to
    486   /// target-specific information.
    487   bool isSCEVable(Type *Ty) const;
    488 
    489   /// Return the size in bits of the specified type, for which isSCEVable must
    490   /// return true.
    491   uint64_t getTypeSizeInBits(Type *Ty) const;
    492 
    493   /// Return a type with the same bitwidth as the given type and which
    494   /// represents how SCEV will treat the given type, for which isSCEVable must
    495   /// return true. For pointer types, this is the pointer-sized integer type.
    496   Type *getEffectiveSCEVType(Type *Ty) const;
    497 
    498   // Returns a wider type among {Ty1, Ty2}.
    499   Type *getWiderType(Type *Ty1, Type *Ty2) const;
    500 
    501   /// Return true if the SCEV is a scAddRecExpr or it contains
    502   /// scAddRecExpr. The result will be cached in HasRecMap.
    503   bool containsAddRecurrence(const SCEV *S);
    504 
    505   /// Erase Value from ValueExprMap and ExprValueMap.
    506   void eraseValueFromMap(Value *V);
    507 
    508   /// Return a SCEV expression for the full generality of the specified
    509   /// expression.
    510   const SCEV *getSCEV(Value *V);
    511 
    512   const SCEV *getConstant(ConstantInt *V);
    513   const SCEV *getConstant(const APInt &Val);
    514   const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false);
    515   const SCEV *getLosslessPtrToIntExpr(const SCEV *Op, unsigned Depth = 0);
    516   const SCEV *getPtrToIntExpr(const SCEV *Op, Type *Ty);
    517   const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0);
    518   const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0);
    519   const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0);
    520   const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty);
    521   const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
    522                          SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
    523                          unsigned Depth = 0);
    524   const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
    525                          SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
    526                          unsigned Depth = 0) {
    527     SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
    528     return getAddExpr(Ops, Flags, Depth);
    529   }
    530   const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
    531                          SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
    532                          unsigned Depth = 0) {
    533     SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2};
    534     return getAddExpr(Ops, Flags, Depth);
    535   }
    536   const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
    537                          SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
    538                          unsigned Depth = 0);
    539   const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
    540                          SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
    541                          unsigned Depth = 0) {
    542     SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
    543     return getMulExpr(Ops, Flags, Depth);
    544   }
    545   const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
    546                          SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
    547                          unsigned Depth = 0) {
    548     SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2};
    549     return getMulExpr(Ops, Flags, Depth);
    550   }
    551   const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
    552   const SCEV *getUDivExactExpr(const SCEV *LHS, const SCEV *RHS);
    553   const SCEV *getURemExpr(const SCEV *LHS, const SCEV *RHS);
    554   const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L,
    555                             SCEV::NoWrapFlags Flags);
    556   const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
    557                             const Loop *L, SCEV::NoWrapFlags Flags);
    558   const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
    559                             const Loop *L, SCEV::NoWrapFlags Flags) {
    560     SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
    561     return getAddRecExpr(NewOp, L, Flags);
    562   }
    563 
    564   /// Checks if \p SymbolicPHI can be rewritten as an AddRecExpr under some
    565   /// Predicates. If successful return these <AddRecExpr, Predicates>;
    566   /// The function is intended to be called from PSCEV (the caller will decide
    567   /// whether to actually add the predicates and carry out the rewrites).
    568   Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
    569   createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI);
    570 
    571   /// Returns an expression for a GEP
    572   ///
    573   /// \p GEP The GEP. The indices contained in the GEP itself are ignored,
    574   /// instead we use IndexExprs.
    575   /// \p IndexExprs The expressions for the indices.
    576   const SCEV *getGEPExpr(GEPOperator *GEP,
    577                          const SmallVectorImpl<const SCEV *> &IndexExprs);
    578   const SCEV *getAbsExpr(const SCEV *Op, bool IsNSW);
    579   const SCEV *getMinMaxExpr(SCEVTypes Kind,
    580                             SmallVectorImpl<const SCEV *> &Operands);
    581   const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
    582   const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
    583   const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
    584   const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
    585   const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
    586   const SCEV *getSMinExpr(SmallVectorImpl<const SCEV *> &Operands);
    587   const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
    588   const SCEV *getUMinExpr(SmallVectorImpl<const SCEV *> &Operands);
    589   const SCEV *getUnknown(Value *V);
    590   const SCEV *getCouldNotCompute();
    591 
    592   /// Return a SCEV for the constant 0 of a specific type.
    593   const SCEV *getZero(Type *Ty) { return getConstant(Ty, 0); }
    594 
    595   /// Return a SCEV for the constant 1 of a specific type.
    596   const SCEV *getOne(Type *Ty) { return getConstant(Ty, 1); }
    597 
    598   /// Return a SCEV for the constant -1 of a specific type.
    599   const SCEV *getMinusOne(Type *Ty) {
    600     return getConstant(Ty, -1, /*isSigned=*/true);
    601   }
    602 
    603   /// Return an expression for sizeof ScalableTy that is type IntTy, where
    604   /// ScalableTy is a scalable vector type.
    605   const SCEV *getSizeOfScalableVectorExpr(Type *IntTy,
    606                                           ScalableVectorType *ScalableTy);
    607 
    608   /// Return an expression for the alloc size of AllocTy that is type IntTy
    609   const SCEV *getSizeOfExpr(Type *IntTy, Type *AllocTy);
    610 
    611   /// Return an expression for the store size of StoreTy that is type IntTy
    612   const SCEV *getStoreSizeOfExpr(Type *IntTy, Type *StoreTy);
    613 
    614   /// Return an expression for offsetof on the given field with type IntTy
    615   const SCEV *getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo);
    616 
    617   /// Return the SCEV object corresponding to -V.
    618   const SCEV *getNegativeSCEV(const SCEV *V,
    619                               SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
    620 
    621   /// Return the SCEV object corresponding to ~V.
    622   const SCEV *getNotSCEV(const SCEV *V);
    623 
    624   /// Return LHS-RHS.  Minus is represented in SCEV as A+B*-1.
    625   const SCEV *getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
    626                            SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
    627                            unsigned Depth = 0);
    628 
    629   /// Return a SCEV corresponding to a conversion of the input value to the
    630   /// specified type.  If the type must be extended, it is zero extended.
    631   const SCEV *getTruncateOrZeroExtend(const SCEV *V, Type *Ty,
    632                                       unsigned Depth = 0);
    633 
    634   /// Return a SCEV corresponding to a conversion of the input value to the
    635   /// specified type.  If the type must be extended, it is sign extended.
    636   const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty,
    637                                       unsigned Depth = 0);
    638 
    639   /// Return a SCEV corresponding to a conversion of the input value to the
    640   /// specified type.  If the type must be extended, it is zero extended.  The
    641   /// conversion must not be narrowing.
    642   const SCEV *getNoopOrZeroExtend(const SCEV *V, Type *Ty);
    643 
    644   /// Return a SCEV corresponding to a conversion of the input value to the
    645   /// specified type.  If the type must be extended, it is sign extended.  The
    646   /// conversion must not be narrowing.
    647   const SCEV *getNoopOrSignExtend(const SCEV *V, Type *Ty);
    648 
    649   /// Return a SCEV corresponding to a conversion of the input value to the
    650   /// specified type. If the type must be extended, it is extended with
    651   /// unspecified bits. The conversion must not be narrowing.
    652   const SCEV *getNoopOrAnyExtend(const SCEV *V, Type *Ty);
    653 
    654   /// Return a SCEV corresponding to a conversion of the input value to the
    655   /// specified type.  The conversion must not be widening.
    656   const SCEV *getTruncateOrNoop(const SCEV *V, Type *Ty);
    657 
    658   /// Promote the operands to the wider of the types using zero-extension, and
    659   /// then perform a umax operation with them.
    660   const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS);
    661 
    662   /// Promote the operands to the wider of the types using zero-extension, and
    663   /// then perform a umin operation with them.
    664   const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS);
    665 
    666   /// Promote the operands to the wider of the types using zero-extension, and
    667   /// then perform a umin operation with them. N-ary function.
    668   const SCEV *getUMinFromMismatchedTypes(SmallVectorImpl<const SCEV *> &Ops);
    669 
    670   /// Transitively follow the chain of pointer-type operands until reaching a
    671   /// SCEV that does not have a single pointer operand. This returns a
    672   /// SCEVUnknown pointer for well-formed pointer-type expressions, but corner
    673   /// cases do exist.
    674   const SCEV *getPointerBase(const SCEV *V);
    675 
    676   /// Return a SCEV expression for the specified value at the specified scope
    677   /// in the program.  The L value specifies a loop nest to evaluate the
    678   /// expression at, where null is the top-level or a specified loop is
    679   /// immediately inside of the loop.
    680   ///
    681   /// This method can be used to compute the exit value for a variable defined
    682   /// in a loop by querying what the value will hold in the parent loop.
    683   ///
    684   /// In the case that a relevant loop exit value cannot be computed, the
    685   /// original value V is returned.
    686   const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
    687 
    688   /// This is a convenience function which does getSCEVAtScope(getSCEV(V), L).
    689   const SCEV *getSCEVAtScope(Value *V, const Loop *L);
    690 
    691   /// Test whether entry to the loop is protected by a conditional between LHS
    692   /// and RHS.  This is used to help avoid max expressions in loop trip
    693   /// counts, and to eliminate casts.
    694   bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
    695                                 const SCEV *LHS, const SCEV *RHS);
    696 
    697   /// Test whether entry to the basic block is protected by a conditional
    698   /// between LHS and RHS.
    699   bool isBasicBlockEntryGuardedByCond(const BasicBlock *BB,
    700                                       ICmpInst::Predicate Pred, const SCEV *LHS,
    701                                       const SCEV *RHS);
    702 
    703   /// Test whether the backedge of the loop is protected by a conditional
    704   /// between LHS and RHS.  This is used to eliminate casts.
    705   bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
    706                                    const SCEV *LHS, const SCEV *RHS);
    707 
    708   /// Returns the maximum trip count of the loop if it is a single-exit
    709   /// loop and we can compute a small maximum for that loop.
    710   ///
    711   /// Implemented in terms of the \c getSmallConstantTripCount overload with
    712   /// the single exiting block passed to it. See that routine for details.
    713   unsigned getSmallConstantTripCount(const Loop *L);
    714 
    715   /// Returns the maximum trip count of this loop as a normal unsigned
    716   /// value. Returns 0 if the trip count is unknown or not constant. This
    717   /// "trip count" assumes that control exits via ExitingBlock. More
    718   /// precisely, it is the number of times that control may reach ExitingBlock
    719   /// before taking the branch. For loops with multiple exits, it may not be
    720   /// the number times that the loop header executes if the loop exits
    721   /// prematurely via another branch.
    722   unsigned getSmallConstantTripCount(const Loop *L,
    723                                      const BasicBlock *ExitingBlock);
    724 
    725   /// Returns the upper bound of the loop trip count as a normal unsigned
    726   /// value.
    727   /// Returns 0 if the trip count is unknown or not constant.
    728   unsigned getSmallConstantMaxTripCount(const Loop *L);
    729 
    730   /// Returns the largest constant divisor of the trip count of the
    731   /// loop if it is a single-exit loop and we can compute a small maximum for
    732   /// that loop.
    733   ///
    734   /// Implemented in terms of the \c getSmallConstantTripMultiple overload with
    735   /// the single exiting block passed to it. See that routine for details.
    736   unsigned getSmallConstantTripMultiple(const Loop *L);
    737 
    738   /// Returns the largest constant divisor of the trip count of this loop as a
    739   /// normal unsigned value, if possible. This means that the actual trip
    740   /// count is always a multiple of the returned value (don't forget the trip
    741   /// count could very well be zero as well!). As explained in the comments
    742   /// for getSmallConstantTripCount, this assumes that control exits the loop
    743   /// via ExitingBlock.
    744   unsigned getSmallConstantTripMultiple(const Loop *L,
    745                                         const BasicBlock *ExitingBlock);
    746 
    747   /// The terms "backedge taken count" and "exit count" are used
    748   /// interchangeably to refer to the number of times the backedge of a loop
    749   /// has executed before the loop is exited.
    750   enum ExitCountKind {
    751     /// An expression exactly describing the number of times the backedge has
    752     /// executed when a loop is exited.
    753     Exact,
    754     /// A constant which provides an upper bound on the exact trip count.
    755     ConstantMaximum,
    756     /// An expression which provides an upper bound on the exact trip count.
    757     SymbolicMaximum,
    758   };
    759 
    760   /// Return the number of times the backedge executes before the given exit
    761   /// would be taken; if not exactly computable, return SCEVCouldNotCompute.
    762   /// For a single exit loop, this value is equivelent to the result of
    763   /// getBackedgeTakenCount.  The loop is guaranteed to exit (via *some* exit)
    764   /// before the backedge is executed (ExitCount + 1) times.  Note that there
    765   /// is no guarantee about *which* exit is taken on the exiting iteration.
    766   const SCEV *getExitCount(const Loop *L, const BasicBlock *ExitingBlock,
    767                            ExitCountKind Kind = Exact);
    768 
    769   /// If the specified loop has a predictable backedge-taken count, return it,
    770   /// otherwise return a SCEVCouldNotCompute object. The backedge-taken count is
    771   /// the number of times the loop header will be branched to from within the
    772   /// loop, assuming there are no abnormal exists like exception throws. This is
    773   /// one less than the trip count of the loop, since it doesn't count the first
    774   /// iteration, when the header is branched to from outside the loop.
    775   ///
    776   /// Note that it is not valid to call this method on a loop without a
    777   /// loop-invariant backedge-taken count (see
    778   /// hasLoopInvariantBackedgeTakenCount).
    779   const SCEV *getBackedgeTakenCount(const Loop *L, ExitCountKind Kind = Exact);
    780 
    781   /// Similar to getBackedgeTakenCount, except it will add a set of
    782   /// SCEV predicates to Predicates that are required to be true in order for
    783   /// the answer to be correct. Predicates can be checked with run-time
    784   /// checks and can be used to perform loop versioning.
    785   const SCEV *getPredicatedBackedgeTakenCount(const Loop *L,
    786                                               SCEVUnionPredicate &Predicates);
    787 
    788   /// When successful, this returns a SCEVConstant that is greater than or equal
    789   /// to (i.e. a "conservative over-approximation") of the value returend by
    790   /// getBackedgeTakenCount.  If such a value cannot be computed, it returns the
    791   /// SCEVCouldNotCompute object.
    792   const SCEV *getConstantMaxBackedgeTakenCount(const Loop *L) {
    793     return getBackedgeTakenCount(L, ConstantMaximum);
    794   }
    795 
    796   /// When successful, this returns a SCEV that is greater than or equal
    797   /// to (i.e. a "conservative over-approximation") of the value returend by
    798   /// getBackedgeTakenCount.  If such a value cannot be computed, it returns the
    799   /// SCEVCouldNotCompute object.
    800   const SCEV *getSymbolicMaxBackedgeTakenCount(const Loop *L) {
    801     return getBackedgeTakenCount(L, SymbolicMaximum);
    802   }
    803 
    804   /// Return true if the backedge taken count is either the value returned by
    805   /// getConstantMaxBackedgeTakenCount or zero.
    806   bool isBackedgeTakenCountMaxOrZero(const Loop *L);
    807 
    808   /// Return true if the specified loop has an analyzable loop-invariant
    809   /// backedge-taken count.
    810   bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
    811 
    812   // This method should be called by the client when it made any change that
    813   // would invalidate SCEV's answers, and the client wants to remove all loop
    814   // information held internally by ScalarEvolution. This is intended to be used
    815   // when the alternative to forget a loop is too expensive (i.e. large loop
    816   // bodies).
    817   void forgetAllLoops();
    818 
    819   /// This method should be called by the client when it has changed a loop in
    820   /// a way that may effect ScalarEvolution's ability to compute a trip count,
    821   /// or if the loop is deleted.  This call is potentially expensive for large
    822   /// loop bodies.
    823   void forgetLoop(const Loop *L);
    824 
    825   // This method invokes forgetLoop for the outermost loop of the given loop
    826   // \p L, making ScalarEvolution forget about all this subtree. This needs to
    827   // be done whenever we make a transform that may affect the parameters of the
    828   // outer loop, such as exit counts for branches.
    829   void forgetTopmostLoop(const Loop *L);
    830 
    831   /// This method should be called by the client when it has changed a value
    832   /// in a way that may effect its value, or which may disconnect it from a
    833   /// def-use chain linking it to a loop.
    834   void forgetValue(Value *V);
    835 
    836   /// Called when the client has changed the disposition of values in
    837   /// this loop.
    838   ///
    839   /// We don't have a way to invalidate per-loop dispositions. Clear and
    840   /// recompute is simpler.
    841   void forgetLoopDispositions(const Loop *L);
    842 
    843   /// Determine the minimum number of zero bits that S is guaranteed to end in
    844   /// (at every loop iteration).  It is, at the same time, the minimum number
    845   /// of times S is divisible by 2.  For example, given {4,+,8} it returns 2.
    846   /// If S is guaranteed to be 0, it returns the bitwidth of S.
    847   uint32_t GetMinTrailingZeros(const SCEV *S);
    848 
    849   /// Determine the unsigned range for a particular SCEV.
    850   /// NOTE: This returns a copy of the reference returned by getRangeRef.
    851   ConstantRange getUnsignedRange(const SCEV *S) {
    852     return getRangeRef(S, HINT_RANGE_UNSIGNED);
    853   }
    854 
    855   /// Determine the min of the unsigned range for a particular SCEV.
    856   APInt getUnsignedRangeMin(const SCEV *S) {
    857     return getRangeRef(S, HINT_RANGE_UNSIGNED).getUnsignedMin();
    858   }
    859 
    860   /// Determine the max of the unsigned range for a particular SCEV.
    861   APInt getUnsignedRangeMax(const SCEV *S) {
    862     return getRangeRef(S, HINT_RANGE_UNSIGNED).getUnsignedMax();
    863   }
    864 
    865   /// Determine the signed range for a particular SCEV.
    866   /// NOTE: This returns a copy of the reference returned by getRangeRef.
    867   ConstantRange getSignedRange(const SCEV *S) {
    868     return getRangeRef(S, HINT_RANGE_SIGNED);
    869   }
    870 
    871   /// Determine the min of the signed range for a particular SCEV.
    872   APInt getSignedRangeMin(const SCEV *S) {
    873     return getRangeRef(S, HINT_RANGE_SIGNED).getSignedMin();
    874   }
    875 
    876   /// Determine the max of the signed range for a particular SCEV.
    877   APInt getSignedRangeMax(const SCEV *S) {
    878     return getRangeRef(S, HINT_RANGE_SIGNED).getSignedMax();
    879   }
    880 
    881   /// Test if the given expression is known to be negative.
    882   bool isKnownNegative(const SCEV *S);
    883 
    884   /// Test if the given expression is known to be positive.
    885   bool isKnownPositive(const SCEV *S);
    886 
    887   /// Test if the given expression is known to be non-negative.
    888   bool isKnownNonNegative(const SCEV *S);
    889 
    890   /// Test if the given expression is known to be non-positive.
    891   bool isKnownNonPositive(const SCEV *S);
    892 
    893   /// Test if the given expression is known to be non-zero.
    894   bool isKnownNonZero(const SCEV *S);
    895 
    896   /// Splits SCEV expression \p S into two SCEVs. One of them is obtained from
    897   /// \p S by substitution of all AddRec sub-expression related to loop \p L
    898   /// with initial value of that SCEV. The second is obtained from \p S by
    899   /// substitution of all AddRec sub-expressions related to loop \p L with post
    900   /// increment of this AddRec in the loop \p L. In both cases all other AddRec
    901   /// sub-expressions (not related to \p L) remain the same.
    902   /// If the \p S contains non-invariant unknown SCEV the function returns
    903   /// CouldNotCompute SCEV in both values of std::pair.
    904   /// For example, for SCEV S={0, +, 1}<L1> + {0, +, 1}<L2> and loop L=L1
    905   /// the function returns pair:
    906   /// first = {0, +, 1}<L2>
    907   /// second = {1, +, 1}<L1> + {0, +, 1}<L2>
    908   /// We can see that for the first AddRec sub-expression it was replaced with
    909   /// 0 (initial value) for the first element and to {1, +, 1}<L1> (post
    910   /// increment value) for the second one. In both cases AddRec expression
    911   /// related to L2 remains the same.
    912   std::pair<const SCEV *, const SCEV *> SplitIntoInitAndPostInc(const Loop *L,
    913                                                                 const SCEV *S);
    914 
    915   /// We'd like to check the predicate on every iteration of the most dominated
    916   /// loop between loops used in LHS and RHS.
    917   /// To do this we use the following list of steps:
    918   /// 1. Collect set S all loops on which either LHS or RHS depend.
    919   /// 2. If S is non-empty
    920   /// a. Let PD be the element of S which is dominated by all other elements.
    921   /// b. Let E(LHS) be value of LHS on entry of PD.
    922   ///    To get E(LHS), we should just take LHS and replace all AddRecs that are
    923   ///    attached to PD on with their entry values.
    924   ///    Define E(RHS) in the same way.
    925   /// c. Let B(LHS) be value of L on backedge of PD.
    926   ///    To get B(LHS), we should just take LHS and replace all AddRecs that are
    927   ///    attached to PD on with their backedge values.
    928   ///    Define B(RHS) in the same way.
    929   /// d. Note that E(LHS) and E(RHS) are automatically available on entry of PD,
    930   ///    so we can assert on that.
    931   /// e. Return true if isLoopEntryGuardedByCond(Pred, E(LHS), E(RHS)) &&
    932   ///                   isLoopBackedgeGuardedByCond(Pred, B(LHS), B(RHS))
    933   bool isKnownViaInduction(ICmpInst::Predicate Pred, const SCEV *LHS,
    934                            const SCEV *RHS);
    935 
    936   /// Test if the given expression is known to satisfy the condition described
    937   /// by Pred, LHS, and RHS.
    938   bool isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
    939                         const SCEV *RHS);
    940 
    941   /// Check whether the condition described by Pred, LHS, and RHS is true or
    942   /// false. If we know it, return the evaluation of this condition. If neither
    943   /// is proved, return None.
    944   Optional<bool> evaluatePredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
    945                                    const SCEV *RHS);
    946 
    947   /// Test if the given expression is known to satisfy the condition described
    948   /// by Pred, LHS, and RHS in the given Context.
    949   bool isKnownPredicateAt(ICmpInst::Predicate Pred, const SCEV *LHS,
    950                         const SCEV *RHS, const Instruction *Context);
    951 
    952   /// Check whether the condition described by Pred, LHS, and RHS is true or
    953   /// false in the given \p Context. If we know it, return the evaluation of
    954   /// this condition. If neither is proved, return None.
    955   Optional<bool> evaluatePredicateAt(ICmpInst::Predicate Pred, const SCEV *LHS,
    956                                      const SCEV *RHS,
    957                                      const Instruction *Context);
    958 
    959   /// Test if the condition described by Pred, LHS, RHS is known to be true on
    960   /// every iteration of the loop of the recurrency LHS.
    961   bool isKnownOnEveryIteration(ICmpInst::Predicate Pred,
    962                                const SCEVAddRecExpr *LHS, const SCEV *RHS);
    963 
    964   /// A predicate is said to be monotonically increasing if may go from being
    965   /// false to being true as the loop iterates, but never the other way
    966   /// around.  A predicate is said to be monotonically decreasing if may go
    967   /// from being true to being false as the loop iterates, but never the other
    968   /// way around.
    969   enum MonotonicPredicateType {
    970     MonotonicallyIncreasing,
    971     MonotonicallyDecreasing
    972   };
    973 
    974   /// If, for all loop invariant X, the predicate "LHS `Pred` X" is
    975   /// monotonically increasing or decreasing, returns
    976   /// Some(MonotonicallyIncreasing) and Some(MonotonicallyDecreasing)
    977   /// respectively. If we could not prove either of these facts, returns None.
    978   Optional<MonotonicPredicateType>
    979   getMonotonicPredicateType(const SCEVAddRecExpr *LHS,
    980                             ICmpInst::Predicate Pred);
    981 
    982   struct LoopInvariantPredicate {
    983     ICmpInst::Predicate Pred;
    984     const SCEV *LHS;
    985     const SCEV *RHS;
    986 
    987     LoopInvariantPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
    988                            const SCEV *RHS)
    989         : Pred(Pred), LHS(LHS), RHS(RHS) {}
    990   };
    991   /// If the result of the predicate LHS `Pred` RHS is loop invariant with
    992   /// respect to L, return a LoopInvariantPredicate with LHS and RHS being
    993   /// invariants, available at L's entry. Otherwise, return None.
    994   Optional<LoopInvariantPredicate>
    995   getLoopInvariantPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
    996                             const SCEV *RHS, const Loop *L);
    997 
    998   /// If the result of the predicate LHS `Pred` RHS is loop invariant with
    999   /// respect to L at given Context during at least first MaxIter iterations,
   1000   /// return a LoopInvariantPredicate with LHS and RHS being invariants,
   1001   /// available at L's entry. Otherwise, return None. The predicate should be
   1002   /// the loop's exit condition.
   1003   Optional<LoopInvariantPredicate>
   1004   getLoopInvariantExitCondDuringFirstIterations(ICmpInst::Predicate Pred,
   1005                                                 const SCEV *LHS,
   1006                                                 const SCEV *RHS, const Loop *L,
   1007                                                 const Instruction *Context,
   1008                                                 const SCEV *MaxIter);
   1009 
   1010   /// Simplify LHS and RHS in a comparison with predicate Pred. Return true
   1011   /// iff any changes were made. If the operands are provably equal or
   1012   /// unequal, LHS and RHS are set to the same value and Pred is set to either
   1013   /// ICMP_EQ or ICMP_NE.
   1014   bool SimplifyICmpOperands(ICmpInst::Predicate &Pred, const SCEV *&LHS,
   1015                             const SCEV *&RHS, unsigned Depth = 0);
   1016 
   1017   /// Return the "disposition" of the given SCEV with respect to the given
   1018   /// loop.
   1019   LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L);
   1020 
   1021   /// Return true if the value of the given SCEV is unchanging in the
   1022   /// specified loop.
   1023   bool isLoopInvariant(const SCEV *S, const Loop *L);
   1024 
   1025   /// Determine if the SCEV can be evaluated at loop's entry. It is true if it
   1026   /// doesn't depend on a SCEVUnknown of an instruction which is dominated by
   1027   /// the header of loop L.
   1028   bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L);
   1029 
   1030   /// Return true if the given SCEV changes value in a known way in the
   1031   /// specified loop.  This property being true implies that the value is
   1032   /// variant in the loop AND that we can emit an expression to compute the
   1033   /// value of the expression at any particular loop iteration.
   1034   bool hasComputableLoopEvolution(const SCEV *S, const Loop *L);
   1035 
   1036   /// Return the "disposition" of the given SCEV with respect to the given
   1037   /// block.
   1038   BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB);
   1039 
   1040   /// Return true if elements that makes up the given SCEV dominate the
   1041   /// specified basic block.
   1042   bool dominates(const SCEV *S, const BasicBlock *BB);
   1043 
   1044   /// Return true if elements that makes up the given SCEV properly dominate
   1045   /// the specified basic block.
   1046   bool properlyDominates(const SCEV *S, const BasicBlock *BB);
   1047 
   1048   /// Test whether the given SCEV has Op as a direct or indirect operand.
   1049   bool hasOperand(const SCEV *S, const SCEV *Op) const;
   1050 
   1051   /// Return the size of an element read or written by Inst.
   1052   const SCEV *getElementSize(Instruction *Inst);
   1053 
   1054   /// Compute the array dimensions Sizes from the set of Terms extracted from
   1055   /// the memory access function of this SCEVAddRecExpr (second step of
   1056   /// delinearization).
   1057   void findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
   1058                            SmallVectorImpl<const SCEV *> &Sizes,
   1059                            const SCEV *ElementSize);
   1060 
   1061   void print(raw_ostream &OS) const;
   1062   void verify() const;
   1063   bool invalidate(Function &F, const PreservedAnalyses &PA,
   1064                   FunctionAnalysisManager::Invalidator &Inv);
   1065 
   1066   /// Collect parametric terms occurring in step expressions (first step of
   1067   /// delinearization).
   1068   void collectParametricTerms(const SCEV *Expr,
   1069                               SmallVectorImpl<const SCEV *> &Terms);
   1070 
   1071   /// Return in Subscripts the access functions for each dimension in Sizes
   1072   /// (third step of delinearization).
   1073   void computeAccessFunctions(const SCEV *Expr,
   1074                               SmallVectorImpl<const SCEV *> &Subscripts,
   1075                               SmallVectorImpl<const SCEV *> &Sizes);
   1076 
   1077   /// Gathers the individual index expressions from a GEP instruction.
   1078   ///
   1079   /// This function optimistically assumes the GEP references into a fixed size
   1080   /// array. If this is actually true, this function returns a list of array
   1081   /// subscript expressions in \p Subscripts and a list of integers describing
   1082   /// the size of the individual array dimensions in \p Sizes. Both lists have
   1083   /// either equal length or the size list is one element shorter in case there
   1084   /// is no known size available for the outermost array dimension. Returns true
   1085   /// if successful and false otherwise.
   1086   bool getIndexExpressionsFromGEP(const GetElementPtrInst *GEP,
   1087                                   SmallVectorImpl<const SCEV *> &Subscripts,
   1088                                   SmallVectorImpl<int> &Sizes);
   1089 
   1090   /// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
   1091   /// subscripts and sizes of an array access.
   1092   ///
   1093   /// The delinearization is a 3 step process: the first two steps compute the
   1094   /// sizes of each subscript and the third step computes the access functions
   1095   /// for the delinearized array:
   1096   ///
   1097   /// 1. Find the terms in the step functions
   1098   /// 2. Compute the array size
   1099   /// 3. Compute the access function: divide the SCEV by the array size
   1100   ///    starting with the innermost dimensions found in step 2. The Quotient
   1101   ///    is the SCEV to be divided in the next step of the recursion. The
   1102   ///    Remainder is the subscript of the innermost dimension. Loop over all
   1103   ///    array dimensions computed in step 2.
   1104   ///
   1105   /// To compute a uniform array size for several memory accesses to the same
   1106   /// object, one can collect in step 1 all the step terms for all the memory
   1107   /// accesses, and compute in step 2 a unique array shape. This guarantees
   1108   /// that the array shape will be the same across all memory accesses.
   1109   ///
   1110   /// FIXME: We could derive the result of steps 1 and 2 from a description of
   1111   /// the array shape given in metadata.
   1112   ///
   1113   /// Example:
   1114   ///
   1115   /// A[][n][m]
   1116   ///
   1117   /// for i
   1118   ///   for j
   1119   ///     for k
   1120   ///       A[j+k][2i][5i] =
   1121   ///
   1122   /// The initial SCEV:
   1123   ///
   1124   /// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k]
   1125   ///
   1126   /// 1. Find the different terms in the step functions:
   1127   /// -> [2*m, 5, n*m, n*m]
   1128   ///
   1129   /// 2. Compute the array size: sort and unique them
   1130   /// -> [n*m, 2*m, 5]
   1131   /// find the GCD of all the terms = 1
   1132   /// divide by the GCD and erase constant terms
   1133   /// -> [n*m, 2*m]
   1134   /// GCD = m
   1135   /// divide by GCD -> [n, 2]
   1136   /// remove constant terms
   1137   /// -> [n]
   1138   /// size of the array is A[unknown][n][m]
   1139   ///
   1140   /// 3. Compute the access function
   1141   /// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m
   1142   /// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k
   1143   /// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k
   1144   /// The remainder is the subscript of the innermost array dimension: [5i].
   1145   ///
   1146   /// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n
   1147   /// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k
   1148   /// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k
   1149   /// The Remainder is the subscript of the next array dimension: [2i].
   1150   ///
   1151   /// The subscript of the outermost dimension is the Quotient: [j+k].
   1152   ///
   1153   /// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
   1154   void delinearize(const SCEV *Expr, SmallVectorImpl<const SCEV *> &Subscripts,
   1155                    SmallVectorImpl<const SCEV *> &Sizes,
   1156                    const SCEV *ElementSize);
   1157 
   1158   /// Return the DataLayout associated with the module this SCEV instance is
   1159   /// operating on.
   1160   const DataLayout &getDataLayout() const {
   1161     return F.getParent()->getDataLayout();
   1162   }
   1163 
   1164   const SCEVPredicate *getEqualPredicate(const SCEV *LHS, const SCEV *RHS);
   1165 
   1166   const SCEVPredicate *
   1167   getWrapPredicate(const SCEVAddRecExpr *AR,
   1168                    SCEVWrapPredicate::IncrementWrapFlags AddedFlags);
   1169 
   1170   /// Re-writes the SCEV according to the Predicates in \p A.
   1171   const SCEV *rewriteUsingPredicate(const SCEV *S, const Loop *L,
   1172                                     SCEVUnionPredicate &A);
   1173   /// Tries to convert the \p S expression to an AddRec expression,
   1174   /// adding additional predicates to \p Preds as required.
   1175   const SCEVAddRecExpr *convertSCEVToAddRecWithPredicates(
   1176       const SCEV *S, const Loop *L,
   1177       SmallPtrSetImpl<const SCEVPredicate *> &Preds);
   1178 
   1179   /// Compute \p LHS - \p RHS and returns the result as an APInt if it is a
   1180   /// constant, and None if it isn't.
   1181   ///
   1182   /// This is intended to be a cheaper version of getMinusSCEV.  We can be
   1183   /// frugal here since we just bail out of actually constructing and
   1184   /// canonicalizing an expression in the cases where the result isn't going
   1185   /// to be a constant.
   1186   Optional<APInt> computeConstantDifference(const SCEV *LHS, const SCEV *RHS);
   1187 
   1188   /// Update no-wrap flags of an AddRec. This may drop the cached info about
   1189   /// this AddRec (such as range info) in case if new flags may potentially
   1190   /// sharpen it.
   1191   void setNoWrapFlags(SCEVAddRecExpr *AddRec, SCEV::NoWrapFlags Flags);
   1192 
   1193   /// Try to apply information from loop guards for \p L to \p Expr.
   1194   const SCEV *applyLoopGuards(const SCEV *Expr, const Loop *L);
   1195 
   1196 private:
   1197   /// A CallbackVH to arrange for ScalarEvolution to be notified whenever a
   1198   /// Value is deleted.
   1199   class SCEVCallbackVH final : public CallbackVH {
   1200     ScalarEvolution *SE;
   1201 
   1202     void deleted() override;
   1203     void allUsesReplacedWith(Value *New) override;
   1204 
   1205   public:
   1206     SCEVCallbackVH(Value *V, ScalarEvolution *SE = nullptr);
   1207   };
   1208 
   1209   friend class SCEVCallbackVH;
   1210   friend class SCEVExpander;
   1211   friend class SCEVUnknown;
   1212 
   1213   /// The function we are analyzing.
   1214   Function &F;
   1215 
   1216   /// Does the module have any calls to the llvm.experimental.guard intrinsic
   1217   /// at all?  If this is false, we avoid doing work that will only help if
   1218   /// thare are guards present in the IR.
   1219   bool HasGuards;
   1220 
   1221   /// The target library information for the target we are targeting.
   1222   TargetLibraryInfo &TLI;
   1223 
   1224   /// The tracker for \@llvm.assume intrinsics in this function.
   1225   AssumptionCache &AC;
   1226 
   1227   /// The dominator tree.
   1228   DominatorTree &DT;
   1229 
   1230   /// The loop information for the function we are currently analyzing.
   1231   LoopInfo &LI;
   1232 
   1233   /// This SCEV is used to represent unknown trip counts and things.
   1234   std::unique_ptr<SCEVCouldNotCompute> CouldNotCompute;
   1235 
   1236   /// The type for HasRecMap.
   1237   using HasRecMapType = DenseMap<const SCEV *, bool>;
   1238 
   1239   /// This is a cache to record whether a SCEV contains any scAddRecExpr.
   1240   HasRecMapType HasRecMap;
   1241 
   1242   /// The type for ExprValueMap.
   1243   using ValueOffsetPair = std::pair<Value *, ConstantInt *>;
   1244   using ExprValueMapType = DenseMap<const SCEV *, SetVector<ValueOffsetPair>>;
   1245 
   1246   /// ExprValueMap -- This map records the original values from which
   1247   /// the SCEV expr is generated from.
   1248   ///
   1249   /// We want to represent the mapping as SCEV -> ValueOffsetPair instead
   1250   /// of SCEV -> Value:
   1251   /// Suppose we know S1 expands to V1, and
   1252   ///  S1 = S2 + C_a
   1253   ///  S3 = S2 + C_b
   1254   /// where C_a and C_b are different SCEVConstants. Then we'd like to
   1255   /// expand S3 as V1 - C_a + C_b instead of expanding S2 literally.
   1256   /// It is helpful when S2 is a complex SCEV expr.
   1257   ///
   1258   /// In order to do that, we represent ExprValueMap as a mapping from
   1259   /// SCEV to ValueOffsetPair. We will save both S1->{V1, 0} and
   1260   /// S2->{V1, C_a} into the map when we create SCEV for V1. When S3
   1261   /// is expanded, it will first expand S2 to V1 - C_a because of
   1262   /// S2->{V1, C_a} in the map, then expand S3 to V1 - C_a + C_b.
   1263   ///
   1264   /// Note: S->{V, Offset} in the ExprValueMap means S can be expanded
   1265   /// to V - Offset.
   1266   ExprValueMapType ExprValueMap;
   1267 
   1268   /// The type for ValueExprMap.
   1269   using ValueExprMapType =
   1270       DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *>>;
   1271 
   1272   /// This is a cache of the values we have analyzed so far.
   1273   ValueExprMapType ValueExprMap;
   1274 
   1275   /// Mark predicate values currently being processed by isImpliedCond.
   1276   SmallPtrSet<const Value *, 6> PendingLoopPredicates;
   1277 
   1278   /// Mark SCEVUnknown Phis currently being processed by getRangeRef.
   1279   SmallPtrSet<const PHINode *, 6> PendingPhiRanges;
   1280 
   1281   // Mark SCEVUnknown Phis currently being processed by isImpliedViaMerge.
   1282   SmallPtrSet<const PHINode *, 6> PendingMerges;
   1283 
   1284   /// Set to true by isLoopBackedgeGuardedByCond when we're walking the set of
   1285   /// conditions dominating the backedge of a loop.
   1286   bool WalkingBEDominatingConds = false;
   1287 
   1288   /// Set to true by isKnownPredicateViaSplitting when we're trying to prove a
   1289   /// predicate by splitting it into a set of independent predicates.
   1290   bool ProvingSplitPredicate = false;
   1291 
   1292   /// Memoized values for the GetMinTrailingZeros
   1293   DenseMap<const SCEV *, uint32_t> MinTrailingZerosCache;
   1294 
   1295   /// Return the Value set from which the SCEV expr is generated.
   1296   SetVector<ValueOffsetPair> *getSCEVValues(const SCEV *S);
   1297 
   1298   /// Private helper method for the GetMinTrailingZeros method
   1299   uint32_t GetMinTrailingZerosImpl(const SCEV *S);
   1300 
   1301   /// Information about the number of loop iterations for which a loop exit's
   1302   /// branch condition evaluates to the not-taken path.  This is a temporary
   1303   /// pair of exact and max expressions that are eventually summarized in
   1304   /// ExitNotTakenInfo and BackedgeTakenInfo.
   1305   struct ExitLimit {
   1306     const SCEV *ExactNotTaken; // The exit is not taken exactly this many times
   1307     const SCEV *MaxNotTaken; // The exit is not taken at most this many times
   1308 
   1309     // Not taken either exactly MaxNotTaken or zero times
   1310     bool MaxOrZero = false;
   1311 
   1312     /// A set of predicate guards for this ExitLimit. The result is only valid
   1313     /// if all of the predicates in \c Predicates evaluate to 'true' at
   1314     /// run-time.
   1315     SmallPtrSet<const SCEVPredicate *, 4> Predicates;
   1316 
   1317     void addPredicate(const SCEVPredicate *P) {
   1318       assert(!isa<SCEVUnionPredicate>(P) && "Only add leaf predicates here!");
   1319       Predicates.insert(P);
   1320     }
   1321 
   1322     /// Construct either an exact exit limit from a constant, or an unknown
   1323     /// one from a SCEVCouldNotCompute.  No other types of SCEVs are allowed
   1324     /// as arguments and asserts enforce that internally.
   1325     /*implicit*/ ExitLimit(const SCEV *E);
   1326 
   1327     ExitLimit(
   1328         const SCEV *E, const SCEV *M, bool MaxOrZero,
   1329         ArrayRef<const SmallPtrSetImpl<const SCEVPredicate *> *> PredSetList);
   1330 
   1331     ExitLimit(const SCEV *E, const SCEV *M, bool MaxOrZero,
   1332               const SmallPtrSetImpl<const SCEVPredicate *> &PredSet);
   1333 
   1334     ExitLimit(const SCEV *E, const SCEV *M, bool MaxOrZero);
   1335 
   1336     /// Test whether this ExitLimit contains any computed information, or
   1337     /// whether it's all SCEVCouldNotCompute values.
   1338     bool hasAnyInfo() const {
   1339       return !isa<SCEVCouldNotCompute>(ExactNotTaken) ||
   1340              !isa<SCEVCouldNotCompute>(MaxNotTaken);
   1341     }
   1342 
   1343     /// Test whether this ExitLimit contains all information.
   1344     bool hasFullInfo() const {
   1345       return !isa<SCEVCouldNotCompute>(ExactNotTaken);
   1346     }
   1347   };
   1348 
   1349   /// Information about the number of times a particular loop exit may be
   1350   /// reached before exiting the loop.
   1351   struct ExitNotTakenInfo {
   1352     PoisoningVH<BasicBlock> ExitingBlock;
   1353     const SCEV *ExactNotTaken;
   1354     const SCEV *MaxNotTaken;
   1355     std::unique_ptr<SCEVUnionPredicate> Predicate;
   1356 
   1357     explicit ExitNotTakenInfo(PoisoningVH<BasicBlock> ExitingBlock,
   1358                               const SCEV *ExactNotTaken,
   1359                               const SCEV *MaxNotTaken,
   1360                               std::unique_ptr<SCEVUnionPredicate> Predicate)
   1361       : ExitingBlock(ExitingBlock), ExactNotTaken(ExactNotTaken),
   1362         MaxNotTaken(ExactNotTaken), Predicate(std::move(Predicate)) {}
   1363 
   1364     bool hasAlwaysTruePredicate() const {
   1365       return !Predicate || Predicate->isAlwaysTrue();
   1366     }
   1367   };
   1368 
   1369   /// Information about the backedge-taken count of a loop. This currently
   1370   /// includes an exact count and a maximum count.
   1371   ///
   1372   class BackedgeTakenInfo {
   1373     /// A list of computable exits and their not-taken counts.  Loops almost
   1374     /// never have more than one computable exit.
   1375     SmallVector<ExitNotTakenInfo, 1> ExitNotTaken;
   1376 
   1377     /// Expression indicating the least constant maximum backedge-taken count of
   1378     /// the loop that is known, or a SCEVCouldNotCompute. This expression is
   1379     /// only valid if the redicates associated with all loop exits are true.
   1380     const SCEV *ConstantMax;
   1381 
   1382     /// Indicating if \c ExitNotTaken has an element for every exiting block in
   1383     /// the loop.
   1384     bool IsComplete;
   1385 
   1386     /// Expression indicating the least maximum backedge-taken count of the loop
   1387     /// that is known, or a SCEVCouldNotCompute. Lazily computed on first query.
   1388     const SCEV *SymbolicMax = nullptr;
   1389 
   1390     /// True iff the backedge is taken either exactly Max or zero times.
   1391     bool MaxOrZero = false;
   1392 
   1393     bool isComplete() const { return IsComplete; }
   1394     const SCEV *getConstantMax() const { return ConstantMax; }
   1395 
   1396   public:
   1397     BackedgeTakenInfo() : ConstantMax(nullptr), IsComplete(false) {}
   1398     BackedgeTakenInfo(BackedgeTakenInfo &&) = default;
   1399     BackedgeTakenInfo &operator=(BackedgeTakenInfo &&) = default;
   1400 
   1401     using EdgeExitInfo = std::pair<BasicBlock *, ExitLimit>;
   1402 
   1403     /// Initialize BackedgeTakenInfo from a list of exact exit counts.
   1404     BackedgeTakenInfo(ArrayRef<EdgeExitInfo> ExitCounts, bool IsComplete,
   1405                       const SCEV *ConstantMax, bool MaxOrZero);
   1406 
   1407     /// Test whether this BackedgeTakenInfo contains any computed information,
   1408     /// or whether it's all SCEVCouldNotCompute values.
   1409     bool hasAnyInfo() const {
   1410       return !ExitNotTaken.empty() ||
   1411              !isa<SCEVCouldNotCompute>(getConstantMax());
   1412     }
   1413 
   1414     /// Test whether this BackedgeTakenInfo contains complete information.
   1415     bool hasFullInfo() const { return isComplete(); }
   1416 
   1417     /// Return an expression indicating the exact *backedge-taken*
   1418     /// count of the loop if it is known or SCEVCouldNotCompute
   1419     /// otherwise.  If execution makes it to the backedge on every
   1420     /// iteration (i.e. there are no abnormal exists like exception
   1421     /// throws and thread exits) then this is the number of times the
   1422     /// loop header will execute minus one.
   1423     ///
   1424     /// If the SCEV predicate associated with the answer can be different
   1425     /// from AlwaysTrue, we must add a (non null) Predicates argument.
   1426     /// The SCEV predicate associated with the answer will be added to
   1427     /// Predicates. A run-time check needs to be emitted for the SCEV
   1428     /// predicate in order for the answer to be valid.
   1429     ///
   1430     /// Note that we should always know if we need to pass a predicate
   1431     /// argument or not from the way the ExitCounts vector was computed.
   1432     /// If we allowed SCEV predicates to be generated when populating this
   1433     /// vector, this information can contain them and therefore a
   1434     /// SCEVPredicate argument should be added to getExact.
   1435     const SCEV *getExact(const Loop *L, ScalarEvolution *SE,
   1436                          SCEVUnionPredicate *Predicates = nullptr) const;
   1437 
   1438     /// Return the number of times this loop exit may fall through to the back
   1439     /// edge, or SCEVCouldNotCompute. The loop is guaranteed not to exit via
   1440     /// this block before this number of iterations, but may exit via another
   1441     /// block.
   1442     const SCEV *getExact(const BasicBlock *ExitingBlock,
   1443                          ScalarEvolution *SE) const;
   1444 
   1445     /// Get the constant max backedge taken count for the loop.
   1446     const SCEV *getConstantMax(ScalarEvolution *SE) const;
   1447 
   1448     /// Get the constant max backedge taken count for the particular loop exit.
   1449     const SCEV *getConstantMax(const BasicBlock *ExitingBlock,
   1450                                ScalarEvolution *SE) const;
   1451 
   1452     /// Get the symbolic max backedge taken count for the loop.
   1453     const SCEV *getSymbolicMax(const Loop *L, ScalarEvolution *SE);
   1454 
   1455     /// Return true if the number of times this backedge is taken is either the
   1456     /// value returned by getConstantMax or zero.
   1457     bool isConstantMaxOrZero(ScalarEvolution *SE) const;
   1458 
   1459     /// Return true if any backedge taken count expressions refer to the given
   1460     /// subexpression.
   1461     bool hasOperand(const SCEV *S, ScalarEvolution *SE) const;
   1462   };
   1463 
   1464   /// Cache the backedge-taken count of the loops for this function as they
   1465   /// are computed.
   1466   DenseMap<const Loop *, BackedgeTakenInfo> BackedgeTakenCounts;
   1467 
   1468   /// Cache the predicated backedge-taken count of the loops for this
   1469   /// function as they are computed.
   1470   DenseMap<const Loop *, BackedgeTakenInfo> PredicatedBackedgeTakenCounts;
   1471 
   1472   /// This map contains entries for all of the PHI instructions that we
   1473   /// attempt to compute constant evolutions for.  This allows us to avoid
   1474   /// potentially expensive recomputation of these properties.  An instruction
   1475   /// maps to null if we are unable to compute its exit value.
   1476   DenseMap<PHINode *, Constant *> ConstantEvolutionLoopExitValue;
   1477 
   1478   /// This map contains entries for all the expressions that we attempt to
   1479   /// compute getSCEVAtScope information for, which can be expensive in
   1480   /// extreme cases.
   1481   DenseMap<const SCEV *, SmallVector<std::pair<const Loop *, const SCEV *>, 2>>
   1482       ValuesAtScopes;
   1483 
   1484   /// Memoized computeLoopDisposition results.
   1485   DenseMap<const SCEV *,
   1486            SmallVector<PointerIntPair<const Loop *, 2, LoopDisposition>, 2>>
   1487       LoopDispositions;
   1488 
   1489   struct LoopProperties {
   1490     /// Set to true if the loop contains no instruction that can have side
   1491     /// effects (i.e. via throwing an exception, volatile or atomic access).
   1492     bool HasNoAbnormalExits;
   1493 
   1494     /// Set to true if the loop contains no instruction that can abnormally exit
   1495     /// the loop (i.e. via throwing an exception, by terminating the thread
   1496     /// cleanly or by infinite looping in a called function).  Strictly
   1497     /// speaking, the last one is not leaving the loop, but is identical to
   1498     /// leaving the loop for reasoning about undefined behavior.
   1499     bool HasNoSideEffects;
   1500   };
   1501 
   1502   /// Cache for \c getLoopProperties.
   1503   DenseMap<const Loop *, LoopProperties> LoopPropertiesCache;
   1504 
   1505   /// Return a \c LoopProperties instance for \p L, creating one if necessary.
   1506   LoopProperties getLoopProperties(const Loop *L);
   1507 
   1508   bool loopHasNoSideEffects(const Loop *L) {
   1509     return getLoopProperties(L).HasNoSideEffects;
   1510   }
   1511 
   1512   bool loopHasNoAbnormalExits(const Loop *L) {
   1513     return getLoopProperties(L).HasNoAbnormalExits;
   1514   }
   1515 
   1516   /// Compute a LoopDisposition value.
   1517   LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L);
   1518 
   1519   /// Memoized computeBlockDisposition results.
   1520   DenseMap<
   1521       const SCEV *,
   1522       SmallVector<PointerIntPair<const BasicBlock *, 2, BlockDisposition>, 2>>
   1523       BlockDispositions;
   1524 
   1525   /// Compute a BlockDisposition value.
   1526   BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB);
   1527 
   1528   /// Memoized results from getRange
   1529   DenseMap<const SCEV *, ConstantRange> UnsignedRanges;
   1530 
   1531   /// Memoized results from getRange
   1532   DenseMap<const SCEV *, ConstantRange> SignedRanges;
   1533 
   1534   /// Used to parameterize getRange
   1535   enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED };
   1536 
   1537   /// Set the memoized range for the given SCEV.
   1538   const ConstantRange &setRange(const SCEV *S, RangeSignHint Hint,
   1539                                 ConstantRange CR) {
   1540     DenseMap<const SCEV *, ConstantRange> &Cache =
   1541         Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges;
   1542 
   1543     auto Pair = Cache.try_emplace(S, std::move(CR));
   1544     if (!Pair.second)
   1545       Pair.first->second = std::move(CR);
   1546     return Pair.first->second;
   1547   }
   1548 
   1549   /// Determine the range for a particular SCEV.
   1550   /// NOTE: This returns a reference to an entry in a cache. It must be
   1551   /// copied if its needed for longer.
   1552   const ConstantRange &getRangeRef(const SCEV *S, RangeSignHint Hint);
   1553 
   1554   /// Determines the range for the affine SCEVAddRecExpr {\p Start,+,\p Stop}.
   1555   /// Helper for \c getRange.
   1556   ConstantRange getRangeForAffineAR(const SCEV *Start, const SCEV *Stop,
   1557                                     const SCEV *MaxBECount, unsigned BitWidth);
   1558 
   1559   /// Determines the range for the affine non-self-wrapping SCEVAddRecExpr {\p
   1560   /// Start,+,\p Stop}<nw>.
   1561   ConstantRange getRangeForAffineNoSelfWrappingAR(const SCEVAddRecExpr *AddRec,
   1562                                                   const SCEV *MaxBECount,
   1563                                                   unsigned BitWidth,
   1564                                                   RangeSignHint SignHint);
   1565 
   1566   /// Try to compute a range for the affine SCEVAddRecExpr {\p Start,+,\p
   1567   /// Stop} by "factoring out" a ternary expression from the add recurrence.
   1568   /// Helper called by \c getRange.
   1569   ConstantRange getRangeViaFactoring(const SCEV *Start, const SCEV *Stop,
   1570                                      const SCEV *MaxBECount, unsigned BitWidth);
   1571 
   1572   /// If the unknown expression U corresponds to a simple recurrence, return
   1573   /// a constant range which represents the entire recurrence.  Note that
   1574   /// *add* recurrences with loop invariant steps aren't represented by
   1575   /// SCEVUnknowns and thus don't use this mechanism.
   1576   ConstantRange getRangeForUnknownRecurrence(const SCEVUnknown *U);
   1577 
   1578   /// We know that there is no SCEV for the specified value.  Analyze the
   1579   /// expression.
   1580   const SCEV *createSCEV(Value *V);
   1581 
   1582   /// Provide the special handling we need to analyze PHI SCEVs.
   1583   const SCEV *createNodeForPHI(PHINode *PN);
   1584 
   1585   /// Helper function called from createNodeForPHI.
   1586   const SCEV *createAddRecFromPHI(PHINode *PN);
   1587 
   1588   /// A helper function for createAddRecFromPHI to handle simple cases.
   1589   const SCEV *createSimpleAffineAddRec(PHINode *PN, Value *BEValueV,
   1590                                             Value *StartValueV);
   1591 
   1592   /// Helper function called from createNodeForPHI.
   1593   const SCEV *createNodeFromSelectLikePHI(PHINode *PN);
   1594 
   1595   /// Provide special handling for a select-like instruction (currently this
   1596   /// is either a select instruction or a phi node).  \p I is the instruction
   1597   /// being processed, and it is assumed equivalent to "Cond ? TrueVal :
   1598   /// FalseVal".
   1599   const SCEV *createNodeForSelectOrPHI(Instruction *I, Value *Cond,
   1600                                        Value *TrueVal, Value *FalseVal);
   1601 
   1602   /// Provide the special handling we need to analyze GEP SCEVs.
   1603   const SCEV *createNodeForGEP(GEPOperator *GEP);
   1604 
   1605   /// Implementation code for getSCEVAtScope; called at most once for each
   1606   /// SCEV+Loop pair.
   1607   const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
   1608 
   1609   /// This looks up computed SCEV values for all instructions that depend on
   1610   /// the given instruction and removes them from the ValueExprMap map if they
   1611   /// reference SymName. This is used during PHI resolution.
   1612   void forgetSymbolicName(Instruction *I, const SCEV *SymName);
   1613 
   1614   /// Return the BackedgeTakenInfo for the given loop, lazily computing new
   1615   /// values if the loop hasn't been analyzed yet. The returned result is
   1616   /// guaranteed not to be predicated.
   1617   BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
   1618 
   1619   /// Similar to getBackedgeTakenInfo, but will add predicates as required
   1620   /// with the purpose of returning complete information.
   1621   const BackedgeTakenInfo &getPredicatedBackedgeTakenInfo(const Loop *L);
   1622 
   1623   /// Compute the number of times the specified loop will iterate.
   1624   /// If AllowPredicates is set, we will create new SCEV predicates as
   1625   /// necessary in order to return an exact answer.
   1626   BackedgeTakenInfo computeBackedgeTakenCount(const Loop *L,
   1627                                               bool AllowPredicates = false);
   1628 
   1629   /// Compute the number of times the backedge of the specified loop will
   1630   /// execute if it exits via the specified block. If AllowPredicates is set,
   1631   /// this call will try to use a minimal set of SCEV predicates in order to
   1632   /// return an exact answer.
   1633   ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
   1634                              bool AllowPredicates = false);
   1635 
   1636   /// Compute the number of times the backedge of the specified loop will
   1637   /// execute if its exit condition were a conditional branch of ExitCond.
   1638   ///
   1639   /// \p ControlsExit is true if ExitCond directly controls the exit
   1640   /// branch. In this case, we can assume that the loop exits only if the
   1641   /// condition is true and can infer that failing to meet the condition prior
   1642   /// to integer wraparound results in undefined behavior.
   1643   ///
   1644   /// If \p AllowPredicates is set, this call will try to use a minimal set of
   1645   /// SCEV predicates in order to return an exact answer.
   1646   ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond,
   1647                                      bool ExitIfTrue, bool ControlsExit,
   1648                                      bool AllowPredicates = false);
   1649 
   1650   /// Return a symbolic upper bound for the backedge taken count of the loop.
   1651   /// This is more general than getConstantMaxBackedgeTakenCount as it returns
   1652   /// an arbitrary expression as opposed to only constants.
   1653   const SCEV *computeSymbolicMaxBackedgeTakenCount(const Loop *L);
   1654 
   1655   // Helper functions for computeExitLimitFromCond to avoid exponential time
   1656   // complexity.
   1657 
   1658   class ExitLimitCache {
   1659     // It may look like we need key on the whole (L, ExitIfTrue, ControlsExit,
   1660     // AllowPredicates) tuple, but recursive calls to
   1661     // computeExitLimitFromCondCached from computeExitLimitFromCondImpl only
   1662     // vary the in \c ExitCond and \c ControlsExit parameters.  We remember the
   1663     // initial values of the other values to assert our assumption.
   1664     SmallDenseMap<PointerIntPair<Value *, 1>, ExitLimit> TripCountMap;
   1665 
   1666     const Loop *L;
   1667     bool ExitIfTrue;
   1668     bool AllowPredicates;
   1669 
   1670   public:
   1671     ExitLimitCache(const Loop *L, bool ExitIfTrue, bool AllowPredicates)
   1672         : L(L), ExitIfTrue(ExitIfTrue), AllowPredicates(AllowPredicates) {}
   1673 
   1674     Optional<ExitLimit> find(const Loop *L, Value *ExitCond, bool ExitIfTrue,
   1675                              bool ControlsExit, bool AllowPredicates);
   1676 
   1677     void insert(const Loop *L, Value *ExitCond, bool ExitIfTrue,
   1678                 bool ControlsExit, bool AllowPredicates, const ExitLimit &EL);
   1679   };
   1680 
   1681   using ExitLimitCacheTy = ExitLimitCache;
   1682 
   1683   ExitLimit computeExitLimitFromCondCached(ExitLimitCacheTy &Cache,
   1684                                            const Loop *L, Value *ExitCond,
   1685                                            bool ExitIfTrue,
   1686                                            bool ControlsExit,
   1687                                            bool AllowPredicates);
   1688   ExitLimit computeExitLimitFromCondImpl(ExitLimitCacheTy &Cache, const Loop *L,
   1689                                          Value *ExitCond, bool ExitIfTrue,
   1690                                          bool ControlsExit,
   1691                                          bool AllowPredicates);
   1692   Optional<ScalarEvolution::ExitLimit>
   1693   computeExitLimitFromCondFromBinOp(ExitLimitCacheTy &Cache, const Loop *L,
   1694                                     Value *ExitCond, bool ExitIfTrue,
   1695                                     bool ControlsExit, bool AllowPredicates);
   1696 
   1697   /// Compute the number of times the backedge of the specified loop will
   1698   /// execute if its exit condition were a conditional branch of the ICmpInst
   1699   /// ExitCond and ExitIfTrue. If AllowPredicates is set, this call will try
   1700   /// to use a minimal set of SCEV predicates in order to return an exact
   1701   /// answer.
   1702   ExitLimit computeExitLimitFromICmp(const Loop *L, ICmpInst *ExitCond,
   1703                                      bool ExitIfTrue,
   1704                                      bool IsSubExpr,
   1705                                      bool AllowPredicates = false);
   1706 
   1707   /// Compute the number of times the backedge of the specified loop will
   1708   /// execute if its exit condition were a switch with a single exiting case
   1709   /// to ExitingBB.
   1710   ExitLimit computeExitLimitFromSingleExitSwitch(const Loop *L,
   1711                                                  SwitchInst *Switch,
   1712                                                  BasicBlock *ExitingBB,
   1713                                                  bool IsSubExpr);
   1714 
   1715   /// Given an exit condition of 'icmp op load X, cst', try to see if we can
   1716   /// compute the backedge-taken count.
   1717   ExitLimit computeLoadConstantCompareExitLimit(LoadInst *LI, Constant *RHS,
   1718                                                 const Loop *L,
   1719                                                 ICmpInst::Predicate p);
   1720 
   1721   /// Compute the exit limit of a loop that is controlled by a
   1722   /// "(IV >> 1) != 0" type comparison.  We cannot compute the exact trip
   1723   /// count in these cases (since SCEV has no way of expressing them), but we
   1724   /// can still sometimes compute an upper bound.
   1725   ///
   1726   /// Return an ExitLimit for a loop whose backedge is guarded by `LHS Pred
   1727   /// RHS`.
   1728   ExitLimit computeShiftCompareExitLimit(Value *LHS, Value *RHS, const Loop *L,
   1729                                          ICmpInst::Predicate Pred);
   1730 
   1731   /// If the loop is known to execute a constant number of times (the
   1732   /// condition evolves only from constants), try to evaluate a few iterations
   1733   /// of the loop until we get the exit condition gets a value of ExitWhen
   1734   /// (true or false).  If we cannot evaluate the exit count of the loop,
   1735   /// return CouldNotCompute.
   1736   const SCEV *computeExitCountExhaustively(const Loop *L, Value *Cond,
   1737                                            bool ExitWhen);
   1738 
   1739   /// Return the number of times an exit condition comparing the specified
   1740   /// value to zero will execute.  If not computable, return CouldNotCompute.
   1741   /// If AllowPredicates is set, this call will try to use a minimal set of
   1742   /// SCEV predicates in order to return an exact answer.
   1743   ExitLimit howFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr,
   1744                          bool AllowPredicates = false);
   1745 
   1746   /// Return the number of times an exit condition checking the specified
   1747   /// value for nonzero will execute.  If not computable, return
   1748   /// CouldNotCompute.
   1749   ExitLimit howFarToNonZero(const SCEV *V, const Loop *L);
   1750 
   1751   /// Return the number of times an exit condition containing the specified
   1752   /// less-than comparison will execute.  If not computable, return
   1753   /// CouldNotCompute.
   1754   ///
   1755   /// \p isSigned specifies whether the less-than is signed.
   1756   ///
   1757   /// \p ControlsExit is true when the LHS < RHS condition directly controls
   1758   /// the branch (loops exits only if condition is true). In this case, we can
   1759   /// use NoWrapFlags to skip overflow checks.
   1760   ///
   1761   /// If \p AllowPredicates is set, this call will try to use a minimal set of
   1762   /// SCEV predicates in order to return an exact answer.
   1763   ExitLimit howManyLessThans(const SCEV *LHS, const SCEV *RHS, const Loop *L,
   1764                              bool isSigned, bool ControlsExit,
   1765                              bool AllowPredicates = false);
   1766 
   1767   ExitLimit howManyGreaterThans(const SCEV *LHS, const SCEV *RHS, const Loop *L,
   1768                                 bool isSigned, bool IsSubExpr,
   1769                                 bool AllowPredicates = false);
   1770 
   1771   /// Return a predecessor of BB (which may not be an immediate predecessor)
   1772   /// which has exactly one successor from which BB is reachable, or null if
   1773   /// no such block is found.
   1774   std::pair<const BasicBlock *, const BasicBlock *>
   1775   getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB) const;
   1776 
   1777   /// Test whether the condition described by Pred, LHS, and RHS is true
   1778   /// whenever the given FoundCondValue value evaluates to true in given
   1779   /// Context. If Context is nullptr, then the found predicate is true
   1780   /// everywhere. LHS and FoundLHS may have different type width.
   1781   bool isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
   1782                      const Value *FoundCondValue, bool Inverse,
   1783                      const Instruction *Context = nullptr);
   1784 
   1785   /// Test whether the condition described by Pred, LHS, and RHS is true
   1786   /// whenever the given FoundCondValue value evaluates to true in given
   1787   /// Context. If Context is nullptr, then the found predicate is true
   1788   /// everywhere. LHS and FoundLHS must have same type width.
   1789   bool isImpliedCondBalancedTypes(ICmpInst::Predicate Pred, const SCEV *LHS,
   1790                                   const SCEV *RHS,
   1791                                   ICmpInst::Predicate FoundPred,
   1792                                   const SCEV *FoundLHS, const SCEV *FoundRHS,
   1793                                   const Instruction *Context);
   1794 
   1795   /// Test whether the condition described by Pred, LHS, and RHS is true
   1796   /// whenever the condition described by FoundPred, FoundLHS, FoundRHS is
   1797   /// true in given Context. If Context is nullptr, then the found predicate is
   1798   /// true everywhere.
   1799   bool isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
   1800                      ICmpInst::Predicate FoundPred, const SCEV *FoundLHS,
   1801                      const SCEV *FoundRHS,
   1802                      const Instruction *Context = nullptr);
   1803 
   1804   /// Test whether the condition described by Pred, LHS, and RHS is true
   1805   /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
   1806   /// true in given Context. If Context is nullptr, then the found predicate is
   1807   /// true everywhere.
   1808   bool isImpliedCondOperands(ICmpInst::Predicate Pred, const SCEV *LHS,
   1809                              const SCEV *RHS, const SCEV *FoundLHS,
   1810                              const SCEV *FoundRHS,
   1811                              const Instruction *Context = nullptr);
   1812 
   1813   /// Test whether the condition described by Pred, LHS, and RHS is true
   1814   /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
   1815   /// true. Here LHS is an operation that includes FoundLHS as one of its
   1816   /// arguments.
   1817   bool isImpliedViaOperations(ICmpInst::Predicate Pred,
   1818                               const SCEV *LHS, const SCEV *RHS,
   1819                               const SCEV *FoundLHS, const SCEV *FoundRHS,
   1820                               unsigned Depth = 0);
   1821 
   1822   /// Test whether the condition described by Pred, LHS, and RHS is true.
   1823   /// Use only simple non-recursive types of checks, such as range analysis etc.
   1824   bool isKnownViaNonRecursiveReasoning(ICmpInst::Predicate Pred,
   1825                                        const SCEV *LHS, const SCEV *RHS);
   1826 
   1827   /// Test whether the condition described by Pred, LHS, and RHS is true
   1828   /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
   1829   /// true.
   1830   bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, const SCEV *LHS,
   1831                                    const SCEV *RHS, const SCEV *FoundLHS,
   1832                                    const SCEV *FoundRHS);
   1833 
   1834   /// Test whether the condition described by Pred, LHS, and RHS is true
   1835   /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
   1836   /// true.  Utility function used by isImpliedCondOperands.  Tries to get
   1837   /// cases like "X `sgt` 0 => X - 1 `sgt` -1".
   1838   bool isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred, const SCEV *LHS,
   1839                                       const SCEV *RHS, const SCEV *FoundLHS,
   1840                                       const SCEV *FoundRHS);
   1841 
   1842   /// Return true if the condition denoted by \p LHS \p Pred \p RHS is implied
   1843   /// by a call to @llvm.experimental.guard in \p BB.
   1844   bool isImpliedViaGuard(const BasicBlock *BB, ICmpInst::Predicate Pred,
   1845                          const SCEV *LHS, const SCEV *RHS);
   1846 
   1847   /// Test whether the condition described by Pred, LHS, and RHS is true
   1848   /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
   1849   /// true.
   1850   ///
   1851   /// This routine tries to rule out certain kinds of integer overflow, and
   1852   /// then tries to reason about arithmetic properties of the predicates.
   1853   bool isImpliedCondOperandsViaNoOverflow(ICmpInst::Predicate Pred,
   1854                                           const SCEV *LHS, const SCEV *RHS,
   1855                                           const SCEV *FoundLHS,
   1856                                           const SCEV *FoundRHS);
   1857 
   1858   /// Test whether the condition described by Pred, LHS, and RHS is true
   1859   /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
   1860   /// true.
   1861   ///
   1862   /// This routine tries to weaken the known condition basing on fact that
   1863   /// FoundLHS is an AddRec.
   1864   bool isImpliedCondOperandsViaAddRecStart(ICmpInst::Predicate Pred,
   1865                                            const SCEV *LHS, const SCEV *RHS,
   1866                                            const SCEV *FoundLHS,
   1867                                            const SCEV *FoundRHS,
   1868                                            const Instruction *Context);
   1869 
   1870   /// Test whether the condition described by Pred, LHS, and RHS is true
   1871   /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
   1872   /// true.
   1873   ///
   1874   /// This routine tries to figure out predicate for Phis which are SCEVUnknown
   1875   /// if it is true for every possible incoming value from their respective
   1876   /// basic blocks.
   1877   bool isImpliedViaMerge(ICmpInst::Predicate Pred,
   1878                          const SCEV *LHS, const SCEV *RHS,
   1879                          const SCEV *FoundLHS, const SCEV *FoundRHS,
   1880                          unsigned Depth);
   1881 
   1882   /// If we know that the specified Phi is in the header of its containing
   1883   /// loop, we know the loop executes a constant number of times, and the PHI
   1884   /// node is just a recurrence involving constants, fold it.
   1885   Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt &BEs,
   1886                                               const Loop *L);
   1887 
   1888   /// Test if the given expression is known to satisfy the condition described
   1889   /// by Pred and the known constant ranges of LHS and RHS.
   1890   bool isKnownPredicateViaConstantRanges(ICmpInst::Predicate Pred,
   1891                                          const SCEV *LHS, const SCEV *RHS);
   1892 
   1893   /// Try to prove the condition described by "LHS Pred RHS" by ruling out
   1894   /// integer overflow.
   1895   ///
   1896   /// For instance, this will return true for "A s< (A + C)<nsw>" if C is
   1897   /// positive.
   1898   bool isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred, const SCEV *LHS,
   1899                                      const SCEV *RHS);
   1900 
   1901   /// Try to split Pred LHS RHS into logical conjunctions (and's) and try to
   1902   /// prove them individually.
   1903   bool isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, const SCEV *LHS,
   1904                                     const SCEV *RHS);
   1905 
   1906   /// Try to match the Expr as "(L + R)<Flags>".
   1907   bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R,
   1908                       SCEV::NoWrapFlags &Flags);
   1909 
   1910   /// Drop memoized information computed for S.
   1911   void forgetMemoizedResults(const SCEV *S);
   1912 
   1913   /// Return an existing SCEV for V if there is one, otherwise return nullptr.
   1914   const SCEV *getExistingSCEV(Value *V);
   1915 
   1916   /// Return false iff given SCEV contains a SCEVUnknown with NULL value-
   1917   /// pointer.
   1918   bool checkValidity(const SCEV *S) const;
   1919 
   1920   /// Return true if `ExtendOpTy`({`Start`,+,`Step`}) can be proved to be
   1921   /// equal to {`ExtendOpTy`(`Start`),+,`ExtendOpTy`(`Step`)}.  This is
   1922   /// equivalent to proving no signed (resp. unsigned) wrap in
   1923   /// {`Start`,+,`Step`} if `ExtendOpTy` is `SCEVSignExtendExpr`
   1924   /// (resp. `SCEVZeroExtendExpr`).
   1925   template <typename ExtendOpTy>
   1926   bool proveNoWrapByVaryingStart(const SCEV *Start, const SCEV *Step,
   1927                                  const Loop *L);
   1928 
   1929   /// Try to prove NSW or NUW on \p AR relying on ConstantRange manipulation.
   1930   SCEV::NoWrapFlags proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR);
   1931 
   1932   /// Try to prove NSW on \p AR by proving facts about conditions known  on
   1933   /// entry and backedge.
   1934   SCEV::NoWrapFlags proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR);
   1935 
   1936   /// Try to prove NUW on \p AR by proving facts about conditions known on
   1937   /// entry and backedge.
   1938   SCEV::NoWrapFlags proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR);
   1939 
   1940   Optional<MonotonicPredicateType>
   1941   getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS,
   1942                                 ICmpInst::Predicate Pred);
   1943 
   1944   /// Return SCEV no-wrap flags that can be proven based on reasoning about
   1945   /// how poison produced from no-wrap flags on this value (e.g. a nuw add)
   1946   /// would trigger undefined behavior on overflow.
   1947   SCEV::NoWrapFlags getNoWrapFlagsFromUB(const Value *V);
   1948 
   1949   /// Return true if the SCEV corresponding to \p I is never poison.  Proving
   1950   /// this is more complex than proving that just \p I is never poison, since
   1951   /// SCEV commons expressions across control flow, and you can have cases
   1952   /// like:
   1953   ///
   1954   ///   idx0 = a + b;
   1955   ///   ptr[idx0] = 100;
   1956   ///   if (<condition>) {
   1957   ///     idx1 = a +nsw b;
   1958   ///     ptr[idx1] = 200;
   1959   ///   }
   1960   ///
   1961   /// where the SCEV expression (+ a b) is guaranteed to not be poison (and
   1962   /// hence not sign-overflow) only if "<condition>" is true.  Since both
   1963   /// `idx0` and `idx1` will be mapped to the same SCEV expression, (+ a b),
   1964   /// it is not okay to annotate (+ a b) with <nsw> in the above example.
   1965   bool isSCEVExprNeverPoison(const Instruction *I);
   1966 
   1967   /// This is like \c isSCEVExprNeverPoison but it specifically works for
   1968   /// instructions that will get mapped to SCEV add recurrences.  Return true
   1969   /// if \p I will never generate poison under the assumption that \p I is an
   1970   /// add recurrence on the loop \p L.
   1971   bool isAddRecNeverPoison(const Instruction *I, const Loop *L);
   1972 
   1973   /// Similar to createAddRecFromPHI, but with the additional flexibility of
   1974   /// suggesting runtime overflow checks in case casts are encountered.
   1975   /// If successful, the analysis records that for this loop, \p SymbolicPHI,
   1976   /// which is the UnknownSCEV currently representing the PHI, can be rewritten
   1977   /// into an AddRec, assuming some predicates; The function then returns the
   1978   /// AddRec and the predicates as a pair, and caches this pair in
   1979   /// PredicatedSCEVRewrites.
   1980   /// If the analysis is not successful, a mapping from the \p SymbolicPHI to
   1981   /// itself (with no predicates) is recorded, and a nullptr with an empty
   1982   /// predicates vector is returned as a pair.
   1983   Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
   1984   createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI);
   1985 
   1986   /// Compute the backedge taken count knowing the interval difference, the
   1987   /// stride and presence of the equality in the comparison.
   1988   const SCEV *computeBECount(const SCEV *Delta, const SCEV *Stride,
   1989                              bool Equality);
   1990 
   1991   /// Compute the maximum backedge count based on the range of values
   1992   /// permitted by Start, End, and Stride. This is for loops of the form
   1993   /// {Start, +, Stride} LT End.
   1994   ///
   1995   /// Precondition: the induction variable is known to be positive.  We *don't*
   1996   /// assert these preconditions so please be careful.
   1997   const SCEV *computeMaxBECountForLT(const SCEV *Start, const SCEV *Stride,
   1998                                      const SCEV *End, unsigned BitWidth,
   1999                                      bool IsSigned);
   2000 
   2001   /// Verify if an linear IV with positive stride can overflow when in a
   2002   /// less-than comparison, knowing the invariant term of the comparison,
   2003   /// the stride and the knowledge of NSW/NUW flags on the recurrence.
   2004   bool doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, bool IsSigned,
   2005                           bool NoWrap);
   2006 
   2007   /// Verify if an linear IV with negative stride can overflow when in a
   2008   /// greater-than comparison, knowing the invariant term of the comparison,
   2009   /// the stride and the knowledge of NSW/NUW flags on the recurrence.
   2010   bool doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, bool IsSigned,
   2011                           bool NoWrap);
   2012 
   2013   /// Get add expr already created or create a new one.
   2014   const SCEV *getOrCreateAddExpr(ArrayRef<const SCEV *> Ops,
   2015                                  SCEV::NoWrapFlags Flags);
   2016 
   2017   /// Get mul expr already created or create a new one.
   2018   const SCEV *getOrCreateMulExpr(ArrayRef<const SCEV *> Ops,
   2019                                  SCEV::NoWrapFlags Flags);
   2020 
   2021   // Get addrec expr already created or create a new one.
   2022   const SCEV *getOrCreateAddRecExpr(ArrayRef<const SCEV *> Ops,
   2023                                     const Loop *L, SCEV::NoWrapFlags Flags);
   2024 
   2025   /// Return x if \p Val is f(x) where f is a 1-1 function.
   2026   const SCEV *stripInjectiveFunctions(const SCEV *Val) const;
   2027 
   2028   /// Find all of the loops transitively used in \p S, and fill \p LoopsUsed.
   2029   /// A loop is considered "used" by an expression if it contains
   2030   /// an add rec on said loop.
   2031   void getUsedLoops(const SCEV *S, SmallPtrSetImpl<const Loop *> &LoopsUsed);
   2032 
   2033   /// Find all of the loops transitively used in \p S, and update \c LoopUsers
   2034   /// accordingly.
   2035   void addToLoopUseLists(const SCEV *S);
   2036 
   2037   /// Try to match the pattern generated by getURemExpr(A, B). If successful,
   2038   /// Assign A and B to LHS and RHS, respectively.
   2039   bool matchURem(const SCEV *Expr, const SCEV *&LHS, const SCEV *&RHS);
   2040 
   2041   /// Look for a SCEV expression with type `SCEVType` and operands `Ops` in
   2042   /// `UniqueSCEVs`.
   2043   ///
   2044   /// The first component of the returned tuple is the SCEV if found and null
   2045   /// otherwise.  The second component is the `FoldingSetNodeID` that was
   2046   /// constructed to look up the SCEV and the third component is the insertion
   2047   /// point.
   2048   std::tuple<SCEV *, FoldingSetNodeID, void *>
   2049   findExistingSCEVInCache(SCEVTypes SCEVType, ArrayRef<const SCEV *> Ops);
   2050 
   2051   FoldingSet<SCEV> UniqueSCEVs;
   2052   FoldingSet<SCEVPredicate> UniquePreds;
   2053   BumpPtrAllocator SCEVAllocator;
   2054 
   2055   /// This maps loops to a list of SCEV expressions that (transitively) use said
   2056   /// loop.
   2057   DenseMap<const Loop *, SmallVector<const SCEV *, 4>> LoopUsers;
   2058 
   2059   /// Cache tentative mappings from UnknownSCEVs in a Loop, to a SCEV expression
   2060   /// they can be rewritten into under certain predicates.
   2061   DenseMap<std::pair<const SCEVUnknown *, const Loop *>,
   2062            std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
   2063       PredicatedSCEVRewrites;
   2064 
   2065   /// The head of a linked list of all SCEVUnknown values that have been
   2066   /// allocated. This is used by releaseMemory to locate them all and call
   2067   /// their destructors.
   2068   SCEVUnknown *FirstUnknown = nullptr;
   2069 };
   2070 
   2071 /// Analysis pass that exposes the \c ScalarEvolution for a function.
   2072 class ScalarEvolutionAnalysis
   2073     : public AnalysisInfoMixin<ScalarEvolutionAnalysis> {
   2074   friend AnalysisInfoMixin<ScalarEvolutionAnalysis>;
   2075 
   2076   static AnalysisKey Key;
   2077 
   2078 public:
   2079   using Result = ScalarEvolution;
   2080 
   2081   ScalarEvolution run(Function &F, FunctionAnalysisManager &AM);
   2082 };
   2083 
   2084 /// Verifier pass for the \c ScalarEvolutionAnalysis results.
   2085 class ScalarEvolutionVerifierPass
   2086     : public PassInfoMixin<ScalarEvolutionVerifierPass> {
   2087 public:
   2088   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
   2089 };
   2090 
   2091 /// Printer pass for the \c ScalarEvolutionAnalysis results.
   2092 class ScalarEvolutionPrinterPass
   2093     : public PassInfoMixin<ScalarEvolutionPrinterPass> {
   2094   raw_ostream &OS;
   2095 
   2096 public:
   2097   explicit ScalarEvolutionPrinterPass(raw_ostream &OS) : OS(OS) {}
   2098 
   2099   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
   2100 };
   2101 
   2102 class ScalarEvolutionWrapperPass : public FunctionPass {
   2103   std::unique_ptr<ScalarEvolution> SE;
   2104 
   2105 public:
   2106   static char ID;
   2107 
   2108   ScalarEvolutionWrapperPass();
   2109 
   2110   ScalarEvolution &getSE() { return *SE; }
   2111   const ScalarEvolution &getSE() const { return *SE; }
   2112 
   2113   bool runOnFunction(Function &F) override;
   2114   void releaseMemory() override;
   2115   void getAnalysisUsage(AnalysisUsage &AU) const override;
   2116   void print(raw_ostream &OS, const Module * = nullptr) const override;
   2117   void verifyAnalysis() const override;
   2118 };
   2119 
   2120 /// An interface layer with SCEV used to manage how we see SCEV expressions
   2121 /// for values in the context of existing predicates. We can add new
   2122 /// predicates, but we cannot remove them.
   2123 ///
   2124 /// This layer has multiple purposes:
   2125 ///   - provides a simple interface for SCEV versioning.
   2126 ///   - guarantees that the order of transformations applied on a SCEV
   2127 ///     expression for a single Value is consistent across two different
   2128 ///     getSCEV calls. This means that, for example, once we've obtained
   2129 ///     an AddRec expression for a certain value through expression
   2130 ///     rewriting, we will continue to get an AddRec expression for that
   2131 ///     Value.
   2132 ///   - lowers the number of expression rewrites.
   2133 class PredicatedScalarEvolution {
   2134 public:
   2135   PredicatedScalarEvolution(ScalarEvolution &SE, Loop &L);
   2136 
   2137   const SCEVUnionPredicate &getUnionPredicate() const;
   2138 
   2139   /// Returns the SCEV expression of V, in the context of the current SCEV
   2140   /// predicate.  The order of transformations applied on the expression of V
   2141   /// returned by ScalarEvolution is guaranteed to be preserved, even when
   2142   /// adding new predicates.
   2143   const SCEV *getSCEV(Value *V);
   2144 
   2145   /// Get the (predicated) backedge count for the analyzed loop.
   2146   const SCEV *getBackedgeTakenCount();
   2147 
   2148   /// Adds a new predicate.
   2149   void addPredicate(const SCEVPredicate &Pred);
   2150 
   2151   /// Attempts to produce an AddRecExpr for V by adding additional SCEV
   2152   /// predicates. If we can't transform the expression into an AddRecExpr we
   2153   /// return nullptr and not add additional SCEV predicates to the current
   2154   /// context.
   2155   const SCEVAddRecExpr *getAsAddRec(Value *V);
   2156 
   2157   /// Proves that V doesn't overflow by adding SCEV predicate.
   2158   void setNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags);
   2159 
   2160   /// Returns true if we've proved that V doesn't wrap by means of a SCEV
   2161   /// predicate.
   2162   bool hasNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags);
   2163 
   2164   /// Returns the ScalarEvolution analysis used.
   2165   ScalarEvolution *getSE() const { return &SE; }
   2166 
   2167   /// We need to explicitly define the copy constructor because of FlagsMap.
   2168   PredicatedScalarEvolution(const PredicatedScalarEvolution &);
   2169 
   2170   /// Print the SCEV mappings done by the Predicated Scalar Evolution.
   2171   /// The printed text is indented by \p Depth.
   2172   void print(raw_ostream &OS, unsigned Depth) const;
   2173 
   2174   /// Check if \p AR1 and \p AR2 are equal, while taking into account
   2175   /// Equal predicates in Preds.
   2176   bool areAddRecsEqualWithPreds(const SCEVAddRecExpr *AR1,
   2177                                 const SCEVAddRecExpr *AR2) const;
   2178 
   2179 private:
   2180   /// Increments the version number of the predicate.  This needs to be called
   2181   /// every time the SCEV predicate changes.
   2182   void updateGeneration();
   2183 
   2184   /// Holds a SCEV and the version number of the SCEV predicate used to
   2185   /// perform the rewrite of the expression.
   2186   using RewriteEntry = std::pair<unsigned, const SCEV *>;
   2187 
   2188   /// Maps a SCEV to the rewrite result of that SCEV at a certain version
   2189   /// number. If this number doesn't match the current Generation, we will
   2190   /// need to do a rewrite. To preserve the transformation order of previous
   2191   /// rewrites, we will rewrite the previous result instead of the original
   2192   /// SCEV.
   2193   DenseMap<const SCEV *, RewriteEntry> RewriteMap;
   2194 
   2195   /// Records what NoWrap flags we've added to a Value *.
   2196   ValueMap<Value *, SCEVWrapPredicate::IncrementWrapFlags> FlagsMap;
   2197 
   2198   /// The ScalarEvolution analysis.
   2199   ScalarEvolution &SE;
   2200 
   2201   /// The analyzed Loop.
   2202   const Loop &L;
   2203 
   2204   /// The SCEVPredicate that forms our context. We will rewrite all
   2205   /// expressions assuming that this predicate true.
   2206   SCEVUnionPredicate Preds;
   2207 
   2208   /// Marks the version of the SCEV predicate used. When rewriting a SCEV
   2209   /// expression we mark it with the version of the predicate. We use this to
   2210   /// figure out if the predicate has changed from the last rewrite of the
   2211   /// SCEV. If so, we need to perform a new rewrite.
   2212   unsigned Generation = 0;
   2213 
   2214   /// The backedge taken count.
   2215   const SCEV *BackedgeCount = nullptr;
   2216 };
   2217 
   2218 } // end namespace llvm
   2219 
   2220 #endif // LLVM_ANALYSIS_SCALAREVOLUTION_H
   2221