|
|
|||
File indexing completed on 2026-05-10 08:43:17
0001 //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // The ScalarEvolution class is an LLVM pass which can be used to analyze and 0010 // categorize scalar expressions in loops. It specializes in recognizing 0011 // general induction variables, representing them with the abstract and opaque 0012 // SCEV class. Given this analysis, trip counts of loops and other important 0013 // properties can be obtained. 0014 // 0015 // This analysis is primarily useful for induction variable substitution and 0016 // strength reduction. 0017 // 0018 //===----------------------------------------------------------------------===// 0019 0020 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H 0021 #define LLVM_ANALYSIS_SCALAREVOLUTION_H 0022 0023 #include "llvm/ADT/APInt.h" 0024 #include "llvm/ADT/ArrayRef.h" 0025 #include "llvm/ADT/DenseMap.h" 0026 #include "llvm/ADT/DenseMapInfo.h" 0027 #include "llvm/ADT/FoldingSet.h" 0028 #include "llvm/ADT/PointerIntPair.h" 0029 #include "llvm/ADT/SetVector.h" 0030 #include "llvm/ADT/SmallPtrSet.h" 0031 #include "llvm/ADT/SmallVector.h" 0032 #include "llvm/IR/ConstantRange.h" 0033 #include "llvm/IR/Instructions.h" 0034 #include "llvm/IR/PassManager.h" 0035 #include "llvm/IR/ValueHandle.h" 0036 #include "llvm/IR/ValueMap.h" 0037 #include "llvm/Pass.h" 0038 #include <cassert> 0039 #include <cstdint> 0040 #include <memory> 0041 #include <optional> 0042 #include <utility> 0043 0044 namespace llvm { 0045 0046 class OverflowingBinaryOperator; 0047 class AssumptionCache; 0048 class BasicBlock; 0049 class Constant; 0050 class ConstantInt; 0051 class DataLayout; 0052 class DominatorTree; 0053 class GEPOperator; 0054 class LLVMContext; 0055 class Loop; 0056 class LoopInfo; 0057 class raw_ostream; 0058 class ScalarEvolution; 0059 class SCEVAddRecExpr; 0060 class SCEVUnknown; 0061 class StructType; 0062 class TargetLibraryInfo; 0063 class Type; 0064 enum SCEVTypes : unsigned short; 0065 0066 extern bool VerifySCEV; 0067 0068 /// This class represents an analyzed expression in the program. These are 0069 /// opaque objects that the client is not allowed to do much with directly. 0070 /// 0071 class SCEV : public FoldingSetNode { 0072 friend struct FoldingSetTrait<SCEV>; 0073 0074 /// A reference to an Interned FoldingSetNodeID for this node. The 0075 /// ScalarEvolution's BumpPtrAllocator holds the data. 0076 FoldingSetNodeIDRef FastID; 0077 0078 // The SCEV baseclass this node corresponds to 0079 const SCEVTypes SCEVType; 0080 0081 protected: 0082 // Estimated complexity of this node's expression tree size. 0083 const unsigned short ExpressionSize; 0084 0085 /// This field is initialized to zero and may be used in subclasses to store 0086 /// miscellaneous information. 0087 unsigned short SubclassData = 0; 0088 0089 public: 0090 /// NoWrapFlags are bitfield indices into SubclassData. 0091 /// 0092 /// Add and Mul expressions may have no-unsigned-wrap <NUW> or 0093 /// no-signed-wrap <NSW> properties, which are derived from the IR 0094 /// operator. NSW is a misnomer that we use to mean no signed overflow or 0095 /// underflow. 0096 /// 0097 /// AddRec expressions may have a no-self-wraparound <NW> property if, in 0098 /// the integer domain, abs(step) * max-iteration(loop) <= 0099 /// unsigned-max(bitwidth). This means that the recurrence will never reach 0100 /// its start value if the step is non-zero. Computing the same value on 0101 /// each iteration is not considered wrapping, and recurrences with step = 0 0102 /// are trivially <NW>. <NW> is independent of the sign of step and the 0103 /// value the add recurrence starts with. 0104 /// 0105 /// Note that NUW and NSW are also valid properties of a recurrence, and 0106 /// either implies NW. For convenience, NW will be set for a recurrence 0107 /// whenever either NUW or NSW are set. 0108 /// 0109 /// We require that the flag on a SCEV apply to the entire scope in which 0110 /// that SCEV is defined. A SCEV's scope is set of locations dominated by 0111 /// a defining location, which is in turn described by the following rules: 0112 /// * A SCEVUnknown is at the point of definition of the Value. 0113 /// * A SCEVConstant is defined at all points. 0114 /// * A SCEVAddRec is defined starting with the header of the associated 0115 /// loop. 0116 /// * All other SCEVs are defined at the earlest point all operands are 0117 /// defined. 0118 /// 0119 /// The above rules describe a maximally hoisted form (without regards to 0120 /// potential control dependence). A SCEV is defined anywhere a 0121 /// corresponding instruction could be defined in said maximally hoisted 0122 /// form. Note that SCEVUDivExpr (currently the only expression type which 0123 /// can trap) can be defined per these rules in regions where it would trap 0124 /// at runtime. A SCEV being defined does not require the existence of any 0125 /// instruction within the defined scope. 0126 enum NoWrapFlags { 0127 FlagAnyWrap = 0, // No guarantee. 0128 FlagNW = (1 << 0), // No self-wrap. 0129 FlagNUW = (1 << 1), // No unsigned wrap. 0130 FlagNSW = (1 << 2), // No signed wrap. 0131 NoWrapMask = (1 << 3) - 1 0132 }; 0133 0134 explicit SCEV(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, 0135 unsigned short ExpressionSize) 0136 : FastID(ID), SCEVType(SCEVTy), ExpressionSize(ExpressionSize) {} 0137 SCEV(const SCEV &) = delete; 0138 SCEV &operator=(const SCEV &) = delete; 0139 0140 SCEVTypes getSCEVType() const { return SCEVType; } 0141 0142 /// Return the LLVM type of this SCEV expression. 0143 Type *getType() const; 0144 0145 /// Return operands of this SCEV expression. 0146 ArrayRef<const SCEV *> operands() const; 0147 0148 /// Return true if the expression is a constant zero. 0149 bool isZero() const; 0150 0151 /// Return true if the expression is a constant one. 0152 bool isOne() const; 0153 0154 /// Return true if the expression is a constant all-ones value. 0155 bool isAllOnesValue() const; 0156 0157 /// Return true if the specified scev is negated, but not a constant. 0158 bool isNonConstantNegative() const; 0159 0160 // Returns estimated size of the mathematical expression represented by this 0161 // SCEV. The rules of its calculation are following: 0162 // 1) Size of a SCEV without operands (like constants and SCEVUnknown) is 1; 0163 // 2) Size SCEV with operands Op1, Op2, ..., OpN is calculated by formula: 0164 // (1 + Size(Op1) + ... + Size(OpN)). 0165 // This value gives us an estimation of time we need to traverse through this 0166 // SCEV and all its operands recursively. We may use it to avoid performing 0167 // heavy transformations on SCEVs of excessive size for sake of saving the 0168 // compilation time. 0169 unsigned short getExpressionSize() const { 0170 return ExpressionSize; 0171 } 0172 0173 /// Print out the internal representation of this scalar to the specified 0174 /// stream. This should really only be used for debugging purposes. 0175 void print(raw_ostream &OS) const; 0176 0177 /// This method is used for debugging. 0178 void dump() const; 0179 }; 0180 0181 // Specialize FoldingSetTrait for SCEV to avoid needing to compute 0182 // temporary FoldingSetNodeID values. 0183 template <> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> { 0184 static void Profile(const SCEV &X, FoldingSetNodeID &ID) { ID = X.FastID; } 0185 0186 static bool Equals(const SCEV &X, const FoldingSetNodeID &ID, unsigned IDHash, 0187 FoldingSetNodeID &TempID) { 0188 return ID == X.FastID; 0189 } 0190 0191 static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) { 0192 return X.FastID.ComputeHash(); 0193 } 0194 }; 0195 0196 inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) { 0197 S.print(OS); 0198 return OS; 0199 } 0200 0201 /// An object of this class is returned by queries that could not be answered. 0202 /// For example, if you ask for the number of iterations of a linked-list 0203 /// traversal loop, you will get one of these. None of the standard SCEV 0204 /// operations are valid on this class, it is just a marker. 0205 struct SCEVCouldNotCompute : public SCEV { 0206 SCEVCouldNotCompute(); 0207 0208 /// Methods for support type inquiry through isa, cast, and dyn_cast: 0209 static bool classof(const SCEV *S); 0210 }; 0211 0212 /// This class represents an assumption made using SCEV expressions which can 0213 /// be checked at run-time. 0214 class SCEVPredicate : public FoldingSetNode { 0215 friend struct FoldingSetTrait<SCEVPredicate>; 0216 0217 /// A reference to an Interned FoldingSetNodeID for this node. The 0218 /// ScalarEvolution's BumpPtrAllocator holds the data. 0219 FoldingSetNodeIDRef FastID; 0220 0221 public: 0222 enum SCEVPredicateKind { P_Union, P_Compare, P_Wrap }; 0223 0224 protected: 0225 SCEVPredicateKind Kind; 0226 ~SCEVPredicate() = default; 0227 SCEVPredicate(const SCEVPredicate &) = default; 0228 SCEVPredicate &operator=(const SCEVPredicate &) = default; 0229 0230 public: 0231 SCEVPredicate(const FoldingSetNodeIDRef ID, SCEVPredicateKind Kind); 0232 0233 SCEVPredicateKind getKind() const { return Kind; } 0234 0235 /// Returns the estimated complexity of this predicate. This is roughly 0236 /// measured in the number of run-time checks required. 0237 virtual unsigned getComplexity() const { return 1; } 0238 0239 /// Returns true if the predicate is always true. This means that no 0240 /// assumptions were made and nothing needs to be checked at run-time. 0241 virtual bool isAlwaysTrue() const = 0; 0242 0243 /// Returns true if this predicate implies \p N. 0244 virtual bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const = 0; 0245 0246 /// Prints a textual representation of this predicate with an indentation of 0247 /// \p Depth. 0248 virtual void print(raw_ostream &OS, unsigned Depth = 0) const = 0; 0249 }; 0250 0251 inline raw_ostream &operator<<(raw_ostream &OS, const SCEVPredicate &P) { 0252 P.print(OS); 0253 return OS; 0254 } 0255 0256 // Specialize FoldingSetTrait for SCEVPredicate to avoid needing to compute 0257 // temporary FoldingSetNodeID values. 0258 template <> 0259 struct FoldingSetTrait<SCEVPredicate> : DefaultFoldingSetTrait<SCEVPredicate> { 0260 static void Profile(const SCEVPredicate &X, FoldingSetNodeID &ID) { 0261 ID = X.FastID; 0262 } 0263 0264 static bool Equals(const SCEVPredicate &X, const FoldingSetNodeID &ID, 0265 unsigned IDHash, FoldingSetNodeID &TempID) { 0266 return ID == X.FastID; 0267 } 0268 0269 static unsigned ComputeHash(const SCEVPredicate &X, 0270 FoldingSetNodeID &TempID) { 0271 return X.FastID.ComputeHash(); 0272 } 0273 }; 0274 0275 /// This class represents an assumption that the expression LHS Pred RHS 0276 /// evaluates to true, and this can be checked at run-time. 0277 class SCEVComparePredicate final : public SCEVPredicate { 0278 /// We assume that LHS Pred RHS is true. 0279 const ICmpInst::Predicate Pred; 0280 const SCEV *LHS; 0281 const SCEV *RHS; 0282 0283 public: 0284 SCEVComparePredicate(const FoldingSetNodeIDRef ID, 0285 const ICmpInst::Predicate Pred, 0286 const SCEV *LHS, const SCEV *RHS); 0287 0288 /// Implementation of the SCEVPredicate interface 0289 bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override; 0290 void print(raw_ostream &OS, unsigned Depth = 0) const override; 0291 bool isAlwaysTrue() const override; 0292 0293 ICmpInst::Predicate getPredicate() const { return Pred; } 0294 0295 /// Returns the left hand side of the predicate. 0296 const SCEV *getLHS() const { return LHS; } 0297 0298 /// Returns the right hand side of the predicate. 0299 const SCEV *getRHS() const { return RHS; } 0300 0301 /// Methods for support type inquiry through isa, cast, and dyn_cast: 0302 static bool classof(const SCEVPredicate *P) { 0303 return P->getKind() == P_Compare; 0304 } 0305 }; 0306 0307 /// This class represents an assumption made on an AddRec expression. Given an 0308 /// affine AddRec expression {a,+,b}, we assume that it has the nssw or nusw 0309 /// flags (defined below) in the first X iterations of the loop, where X is a 0310 /// SCEV expression returned by getPredicatedBackedgeTakenCount). 0311 /// 0312 /// Note that this does not imply that X is equal to the backedge taken 0313 /// count. This means that if we have a nusw predicate for i32 {0,+,1} with a 0314 /// predicated backedge taken count of X, we only guarantee that {0,+,1} has 0315 /// nusw in the first X iterations. {0,+,1} may still wrap in the loop if we 0316 /// have more than X iterations. 0317 class SCEVWrapPredicate final : public SCEVPredicate { 0318 public: 0319 /// Similar to SCEV::NoWrapFlags, but with slightly different semantics 0320 /// for FlagNUSW. The increment is considered to be signed, and a + b 0321 /// (where b is the increment) is considered to wrap if: 0322 /// zext(a + b) != zext(a) + sext(b) 0323 /// 0324 /// If Signed is a function that takes an n-bit tuple and maps to the 0325 /// integer domain as the tuples value interpreted as twos complement, 0326 /// and Unsigned a function that takes an n-bit tuple and maps to the 0327 /// integer domain as the base two value of input tuple, then a + b 0328 /// has IncrementNUSW iff: 0329 /// 0330 /// 0 <= Unsigned(a) + Signed(b) < 2^n 0331 /// 0332 /// The IncrementNSSW flag has identical semantics with SCEV::FlagNSW. 0333 /// 0334 /// Note that the IncrementNUSW flag is not commutative: if base + inc 0335 /// has IncrementNUSW, then inc + base doesn't neccessarily have this 0336 /// property. The reason for this is that this is used for sign/zero 0337 /// extending affine AddRec SCEV expressions when a SCEVWrapPredicate is 0338 /// assumed. A {base,+,inc} expression is already non-commutative with 0339 /// regards to base and inc, since it is interpreted as: 0340 /// (((base + inc) + inc) + inc) ... 0341 enum IncrementWrapFlags { 0342 IncrementAnyWrap = 0, // No guarantee. 0343 IncrementNUSW = (1 << 0), // No unsigned with signed increment wrap. 0344 IncrementNSSW = (1 << 1), // No signed with signed increment wrap 0345 // (equivalent with SCEV::NSW) 0346 IncrementNoWrapMask = (1 << 2) - 1 0347 }; 0348 0349 /// Convenient IncrementWrapFlags manipulation methods. 0350 [[nodiscard]] static SCEVWrapPredicate::IncrementWrapFlags 0351 clearFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, 0352 SCEVWrapPredicate::IncrementWrapFlags OffFlags) { 0353 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!"); 0354 assert((OffFlags & IncrementNoWrapMask) == OffFlags && 0355 "Invalid flags value!"); 0356 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & ~OffFlags); 0357 } 0358 0359 [[nodiscard]] static SCEVWrapPredicate::IncrementWrapFlags 0360 maskFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, int Mask) { 0361 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!"); 0362 assert((Mask & IncrementNoWrapMask) == Mask && "Invalid mask value!"); 0363 0364 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & Mask); 0365 } 0366 0367 [[nodiscard]] static SCEVWrapPredicate::IncrementWrapFlags 0368 setFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, 0369 SCEVWrapPredicate::IncrementWrapFlags OnFlags) { 0370 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!"); 0371 assert((OnFlags & IncrementNoWrapMask) == OnFlags && 0372 "Invalid flags value!"); 0373 0374 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags | OnFlags); 0375 } 0376 0377 /// Returns the set of SCEVWrapPredicate no wrap flags implied by a 0378 /// SCEVAddRecExpr. 0379 [[nodiscard]] static SCEVWrapPredicate::IncrementWrapFlags 0380 getImpliedFlags(const SCEVAddRecExpr *AR, ScalarEvolution &SE); 0381 0382 private: 0383 const SCEVAddRecExpr *AR; 0384 IncrementWrapFlags Flags; 0385 0386 public: 0387 explicit SCEVWrapPredicate(const FoldingSetNodeIDRef ID, 0388 const SCEVAddRecExpr *AR, 0389 IncrementWrapFlags Flags); 0390 0391 /// Returns the set assumed no overflow flags. 0392 IncrementWrapFlags getFlags() const { return Flags; } 0393 0394 /// Implementation of the SCEVPredicate interface 0395 const SCEVAddRecExpr *getExpr() const; 0396 bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override; 0397 void print(raw_ostream &OS, unsigned Depth = 0) const override; 0398 bool isAlwaysTrue() const override; 0399 0400 /// Methods for support type inquiry through isa, cast, and dyn_cast: 0401 static bool classof(const SCEVPredicate *P) { 0402 return P->getKind() == P_Wrap; 0403 } 0404 }; 0405 0406 /// This class represents a composition of other SCEV predicates, and is the 0407 /// class that most clients will interact with. This is equivalent to a 0408 /// logical "AND" of all the predicates in the union. 0409 /// 0410 /// NB! Unlike other SCEVPredicate sub-classes this class does not live in the 0411 /// ScalarEvolution::Preds folding set. This is why the \c add function is sound. 0412 class SCEVUnionPredicate final : public SCEVPredicate { 0413 private: 0414 using PredicateMap = 0415 DenseMap<const SCEV *, SmallVector<const SCEVPredicate *, 4>>; 0416 0417 /// Vector with references to all predicates in this union. 0418 SmallVector<const SCEVPredicate *, 16> Preds; 0419 0420 /// Adds a predicate to this union. 0421 void add(const SCEVPredicate *N, ScalarEvolution &SE); 0422 0423 public: 0424 SCEVUnionPredicate(ArrayRef<const SCEVPredicate *> Preds, 0425 ScalarEvolution &SE); 0426 0427 ArrayRef<const SCEVPredicate *> getPredicates() const { return Preds; } 0428 0429 /// Implementation of the SCEVPredicate interface 0430 bool isAlwaysTrue() const override; 0431 bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override; 0432 void print(raw_ostream &OS, unsigned Depth) const override; 0433 0434 /// We estimate the complexity of a union predicate as the size number of 0435 /// predicates in the union. 0436 unsigned getComplexity() const override { return Preds.size(); } 0437 0438 /// Methods for support type inquiry through isa, cast, and dyn_cast: 0439 static bool classof(const SCEVPredicate *P) { 0440 return P->getKind() == P_Union; 0441 } 0442 }; 0443 0444 /// The main scalar evolution driver. Because client code (intentionally) 0445 /// can't do much with the SCEV objects directly, they must ask this class 0446 /// for services. 0447 class ScalarEvolution { 0448 friend class ScalarEvolutionsTest; 0449 0450 public: 0451 /// An enum describing the relationship between a SCEV and a loop. 0452 enum LoopDisposition { 0453 LoopVariant, ///< The SCEV is loop-variant (unknown). 0454 LoopInvariant, ///< The SCEV is loop-invariant. 0455 LoopComputable ///< The SCEV varies predictably with the loop. 0456 }; 0457 0458 /// An enum describing the relationship between a SCEV and a basic block. 0459 enum BlockDisposition { 0460 DoesNotDominateBlock, ///< The SCEV does not dominate the block. 0461 DominatesBlock, ///< The SCEV dominates the block. 0462 ProperlyDominatesBlock ///< The SCEV properly dominates the block. 0463 }; 0464 0465 /// Convenient NoWrapFlags manipulation that hides enum casts and is 0466 /// visible in the ScalarEvolution name space. 0467 [[nodiscard]] static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags, 0468 int Mask) { 0469 return (SCEV::NoWrapFlags)(Flags & Mask); 0470 } 0471 [[nodiscard]] static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags, 0472 SCEV::NoWrapFlags OnFlags) { 0473 return (SCEV::NoWrapFlags)(Flags | OnFlags); 0474 } 0475 [[nodiscard]] static SCEV::NoWrapFlags 0476 clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags) { 0477 return (SCEV::NoWrapFlags)(Flags & ~OffFlags); 0478 } 0479 [[nodiscard]] static bool hasFlags(SCEV::NoWrapFlags Flags, 0480 SCEV::NoWrapFlags TestFlags) { 0481 return TestFlags == maskFlags(Flags, TestFlags); 0482 }; 0483 0484 ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC, 0485 DominatorTree &DT, LoopInfo &LI); 0486 ScalarEvolution(ScalarEvolution &&Arg); 0487 ~ScalarEvolution(); 0488 0489 LLVMContext &getContext() const { return F.getContext(); } 0490 0491 /// Test if values of the given type are analyzable within the SCEV 0492 /// framework. This primarily includes integer types, and it can optionally 0493 /// include pointer types if the ScalarEvolution class has access to 0494 /// target-specific information. 0495 bool isSCEVable(Type *Ty) const; 0496 0497 /// Return the size in bits of the specified type, for which isSCEVable must 0498 /// return true. 0499 uint64_t getTypeSizeInBits(Type *Ty) const; 0500 0501 /// Return a type with the same bitwidth as the given type and which 0502 /// represents how SCEV will treat the given type, for which isSCEVable must 0503 /// return true. For pointer types, this is the pointer-sized integer type. 0504 Type *getEffectiveSCEVType(Type *Ty) const; 0505 0506 // Returns a wider type among {Ty1, Ty2}. 0507 Type *getWiderType(Type *Ty1, Type *Ty2) const; 0508 0509 /// Return true if there exists a point in the program at which both 0510 /// A and B could be operands to the same instruction. 0511 /// SCEV expressions are generally assumed to correspond to instructions 0512 /// which could exists in IR. In general, this requires that there exists 0513 /// a use point in the program where all operands dominate the use. 0514 /// 0515 /// Example: 0516 /// loop { 0517 /// if 0518 /// loop { v1 = load @global1; } 0519 /// else 0520 /// loop { v2 = load @global2; } 0521 /// } 0522 /// No SCEV with operand V1, and v2 can exist in this program. 0523 bool instructionCouldExistWithOperands(const SCEV *A, const SCEV *B); 0524 0525 /// Return true if the SCEV is a scAddRecExpr or it contains 0526 /// scAddRecExpr. The result will be cached in HasRecMap. 0527 bool containsAddRecurrence(const SCEV *S); 0528 0529 /// Is operation \p BinOp between \p LHS and \p RHS provably does not have 0530 /// a signed/unsigned overflow (\p Signed)? If \p CtxI is specified, the 0531 /// no-overflow fact should be true in the context of this instruction. 0532 bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, 0533 const SCEV *LHS, const SCEV *RHS, 0534 const Instruction *CtxI = nullptr); 0535 0536 /// Parse NSW/NUW flags from add/sub/mul IR binary operation \p Op into 0537 /// SCEV no-wrap flags, and deduce flag[s] that aren't known yet. 0538 /// Does not mutate the original instruction. Returns std::nullopt if it could 0539 /// not deduce more precise flags than the instruction already has, otherwise 0540 /// returns proven flags. 0541 std::optional<SCEV::NoWrapFlags> 0542 getStrengthenedNoWrapFlagsFromBinOp(const OverflowingBinaryOperator *OBO); 0543 0544 /// Notify this ScalarEvolution that \p User directly uses SCEVs in \p Ops. 0545 void registerUser(const SCEV *User, ArrayRef<const SCEV *> Ops); 0546 0547 /// Return true if the SCEV expression contains an undef value. 0548 bool containsUndefs(const SCEV *S) const; 0549 0550 /// Return true if the SCEV expression contains a Value that has been 0551 /// optimised out and is now a nullptr. 0552 bool containsErasedValue(const SCEV *S) const; 0553 0554 /// Return a SCEV expression for the full generality of the specified 0555 /// expression. 0556 const SCEV *getSCEV(Value *V); 0557 0558 /// Return an existing SCEV for V if there is one, otherwise return nullptr. 0559 const SCEV *getExistingSCEV(Value *V); 0560 0561 const SCEV *getConstant(ConstantInt *V); 0562 const SCEV *getConstant(const APInt &Val); 0563 const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false); 0564 const SCEV *getLosslessPtrToIntExpr(const SCEV *Op, unsigned Depth = 0); 0565 const SCEV *getPtrToIntExpr(const SCEV *Op, Type *Ty); 0566 const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0); 0567 const SCEV *getVScale(Type *Ty); 0568 const SCEV *getElementCount(Type *Ty, ElementCount EC); 0569 const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0); 0570 const SCEV *getZeroExtendExprImpl(const SCEV *Op, Type *Ty, 0571 unsigned Depth = 0); 0572 const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0); 0573 const SCEV *getSignExtendExprImpl(const SCEV *Op, Type *Ty, 0574 unsigned Depth = 0); 0575 const SCEV *getCastExpr(SCEVTypes Kind, const SCEV *Op, Type *Ty); 0576 const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty); 0577 const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops, 0578 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap, 0579 unsigned Depth = 0); 0580 const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS, 0581 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap, 0582 unsigned Depth = 0) { 0583 SmallVector<const SCEV *, 2> Ops = {LHS, RHS}; 0584 return getAddExpr(Ops, Flags, Depth); 0585 } 0586 const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2, 0587 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap, 0588 unsigned Depth = 0) { 0589 SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2}; 0590 return getAddExpr(Ops, Flags, Depth); 0591 } 0592 const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops, 0593 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap, 0594 unsigned Depth = 0); 0595 const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS, 0596 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap, 0597 unsigned Depth = 0) { 0598 SmallVector<const SCEV *, 2> Ops = {LHS, RHS}; 0599 return getMulExpr(Ops, Flags, Depth); 0600 } 0601 const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2, 0602 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap, 0603 unsigned Depth = 0) { 0604 SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2}; 0605 return getMulExpr(Ops, Flags, Depth); 0606 } 0607 const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS); 0608 const SCEV *getUDivExactExpr(const SCEV *LHS, const SCEV *RHS); 0609 const SCEV *getURemExpr(const SCEV *LHS, const SCEV *RHS); 0610 const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, 0611 SCEV::NoWrapFlags Flags); 0612 const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, 0613 const Loop *L, SCEV::NoWrapFlags Flags); 0614 const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands, 0615 const Loop *L, SCEV::NoWrapFlags Flags) { 0616 SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end()); 0617 return getAddRecExpr(NewOp, L, Flags); 0618 } 0619 0620 /// Checks if \p SymbolicPHI can be rewritten as an AddRecExpr under some 0621 /// Predicates. If successful return these <AddRecExpr, Predicates>; 0622 /// The function is intended to be called from PSCEV (the caller will decide 0623 /// whether to actually add the predicates and carry out the rewrites). 0624 std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>> 0625 createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI); 0626 0627 /// Returns an expression for a GEP 0628 /// 0629 /// \p GEP The GEP. The indices contained in the GEP itself are ignored, 0630 /// instead we use IndexExprs. 0631 /// \p IndexExprs The expressions for the indices. 0632 const SCEV *getGEPExpr(GEPOperator *GEP, 0633 const SmallVectorImpl<const SCEV *> &IndexExprs); 0634 const SCEV *getAbsExpr(const SCEV *Op, bool IsNSW); 0635 const SCEV *getMinMaxExpr(SCEVTypes Kind, 0636 SmallVectorImpl<const SCEV *> &Operands); 0637 const SCEV *getSequentialMinMaxExpr(SCEVTypes Kind, 0638 SmallVectorImpl<const SCEV *> &Operands); 0639 const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS); 0640 const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands); 0641 const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS); 0642 const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands); 0643 const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS); 0644 const SCEV *getSMinExpr(SmallVectorImpl<const SCEV *> &Operands); 0645 const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS, 0646 bool Sequential = false); 0647 const SCEV *getUMinExpr(SmallVectorImpl<const SCEV *> &Operands, 0648 bool Sequential = false); 0649 const SCEV *getUnknown(Value *V); 0650 const SCEV *getCouldNotCompute(); 0651 0652 /// Return a SCEV for the constant 0 of a specific type. 0653 const SCEV *getZero(Type *Ty) { return getConstant(Ty, 0); } 0654 0655 /// Return a SCEV for the constant 1 of a specific type. 0656 const SCEV *getOne(Type *Ty) { return getConstant(Ty, 1); } 0657 0658 /// Return a SCEV for the constant \p Power of two. 0659 const SCEV *getPowerOfTwo(Type *Ty, unsigned Power) { 0660 assert(Power < getTypeSizeInBits(Ty) && "Power out of range"); 0661 return getConstant(APInt::getOneBitSet(getTypeSizeInBits(Ty), Power)); 0662 } 0663 0664 /// Return a SCEV for the constant -1 of a specific type. 0665 const SCEV *getMinusOne(Type *Ty) { 0666 return getConstant(Ty, -1, /*isSigned=*/true); 0667 } 0668 0669 /// Return an expression for a TypeSize. 0670 const SCEV *getSizeOfExpr(Type *IntTy, TypeSize Size); 0671 0672 /// Return an expression for the alloc size of AllocTy that is type IntTy 0673 const SCEV *getSizeOfExpr(Type *IntTy, Type *AllocTy); 0674 0675 /// Return an expression for the store size of StoreTy that is type IntTy 0676 const SCEV *getStoreSizeOfExpr(Type *IntTy, Type *StoreTy); 0677 0678 /// Return an expression for offsetof on the given field with type IntTy 0679 const SCEV *getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo); 0680 0681 /// Return the SCEV object corresponding to -V. 0682 const SCEV *getNegativeSCEV(const SCEV *V, 0683 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap); 0684 0685 /// Return the SCEV object corresponding to ~V. 0686 const SCEV *getNotSCEV(const SCEV *V); 0687 0688 /// Return LHS-RHS. Minus is represented in SCEV as A+B*-1. 0689 /// 0690 /// If the LHS and RHS are pointers which don't share a common base 0691 /// (according to getPointerBase()), this returns a SCEVCouldNotCompute. 0692 /// To compute the difference between two unrelated pointers, you can 0693 /// explicitly convert the arguments using getPtrToIntExpr(), for pointer 0694 /// types that support it. 0695 const SCEV *getMinusSCEV(const SCEV *LHS, const SCEV *RHS, 0696 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap, 0697 unsigned Depth = 0); 0698 0699 /// Compute ceil(N / D). N and D are treated as unsigned values. 0700 /// 0701 /// Since SCEV doesn't have native ceiling division, this generates a 0702 /// SCEV expression of the following form: 0703 /// 0704 /// umin(N, 1) + floor((N - umin(N, 1)) / D) 0705 /// 0706 /// A denominator of zero or poison is handled the same way as getUDivExpr(). 0707 const SCEV *getUDivCeilSCEV(const SCEV *N, const SCEV *D); 0708 0709 /// Return a SCEV corresponding to a conversion of the input value to the 0710 /// specified type. If the type must be extended, it is zero extended. 0711 const SCEV *getTruncateOrZeroExtend(const SCEV *V, Type *Ty, 0712 unsigned Depth = 0); 0713 0714 /// Return a SCEV corresponding to a conversion of the input value to the 0715 /// specified type. If the type must be extended, it is sign extended. 0716 const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty, 0717 unsigned Depth = 0); 0718 0719 /// Return a SCEV corresponding to a conversion of the input value to the 0720 /// specified type. If the type must be extended, it is zero extended. The 0721 /// conversion must not be narrowing. 0722 const SCEV *getNoopOrZeroExtend(const SCEV *V, Type *Ty); 0723 0724 /// Return a SCEV corresponding to a conversion of the input value to the 0725 /// specified type. If the type must be extended, it is sign extended. The 0726 /// conversion must not be narrowing. 0727 const SCEV *getNoopOrSignExtend(const SCEV *V, Type *Ty); 0728 0729 /// Return a SCEV corresponding to a conversion of the input value to the 0730 /// specified type. If the type must be extended, it is extended with 0731 /// unspecified bits. The conversion must not be narrowing. 0732 const SCEV *getNoopOrAnyExtend(const SCEV *V, Type *Ty); 0733 0734 /// Return a SCEV corresponding to a conversion of the input value to the 0735 /// specified type. The conversion must not be widening. 0736 const SCEV *getTruncateOrNoop(const SCEV *V, Type *Ty); 0737 0738 /// Promote the operands to the wider of the types using zero-extension, and 0739 /// then perform a umax operation with them. 0740 const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS); 0741 0742 /// Promote the operands to the wider of the types using zero-extension, and 0743 /// then perform a umin operation with them. 0744 const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS, 0745 bool Sequential = false); 0746 0747 /// Promote the operands to the wider of the types using zero-extension, and 0748 /// then perform a umin operation with them. N-ary function. 0749 const SCEV *getUMinFromMismatchedTypes(SmallVectorImpl<const SCEV *> &Ops, 0750 bool Sequential = false); 0751 0752 /// Transitively follow the chain of pointer-type operands until reaching a 0753 /// SCEV that does not have a single pointer operand. This returns a 0754 /// SCEVUnknown pointer for well-formed pointer-type expressions, but corner 0755 /// cases do exist. 0756 const SCEV *getPointerBase(const SCEV *V); 0757 0758 /// Compute an expression equivalent to S - getPointerBase(S). 0759 const SCEV *removePointerBase(const SCEV *S); 0760 0761 /// Return a SCEV expression for the specified value at the specified scope 0762 /// in the program. The L value specifies a loop nest to evaluate the 0763 /// expression at, where null is the top-level or a specified loop is 0764 /// immediately inside of the loop. 0765 /// 0766 /// This method can be used to compute the exit value for a variable defined 0767 /// in a loop by querying what the value will hold in the parent loop. 0768 /// 0769 /// In the case that a relevant loop exit value cannot be computed, the 0770 /// original value V is returned. 0771 const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L); 0772 0773 /// This is a convenience function which does getSCEVAtScope(getSCEV(V), L). 0774 const SCEV *getSCEVAtScope(Value *V, const Loop *L); 0775 0776 /// Test whether entry to the loop is protected by a conditional between LHS 0777 /// and RHS. This is used to help avoid max expressions in loop trip 0778 /// counts, and to eliminate casts. 0779 bool isLoopEntryGuardedByCond(const Loop *L, CmpPredicate Pred, 0780 const SCEV *LHS, const SCEV *RHS); 0781 0782 /// Test whether entry to the basic block is protected by a conditional 0783 /// between LHS and RHS. 0784 bool isBasicBlockEntryGuardedByCond(const BasicBlock *BB, CmpPredicate Pred, 0785 const SCEV *LHS, const SCEV *RHS); 0786 0787 /// Test whether the backedge of the loop is protected by a conditional 0788 /// between LHS and RHS. This is used to eliminate casts. 0789 bool isLoopBackedgeGuardedByCond(const Loop *L, CmpPredicate Pred, 0790 const SCEV *LHS, const SCEV *RHS); 0791 0792 /// A version of getTripCountFromExitCount below which always picks an 0793 /// evaluation type which can not result in overflow. 0794 const SCEV *getTripCountFromExitCount(const SCEV *ExitCount); 0795 0796 /// Convert from an "exit count" (i.e. "backedge taken count") to a "trip 0797 /// count". A "trip count" is the number of times the header of the loop 0798 /// will execute if an exit is taken after the specified number of backedges 0799 /// have been taken. (e.g. TripCount = ExitCount + 1). Note that the 0800 /// expression can overflow if ExitCount = UINT_MAX. If EvalTy is not wide 0801 /// enough to hold the result without overflow, result unsigned wraps with 0802 /// 2s-complement semantics. ex: EC = 255 (i8), TC = 0 (i8) 0803 const SCEV *getTripCountFromExitCount(const SCEV *ExitCount, Type *EvalTy, 0804 const Loop *L); 0805 0806 /// Returns the exact trip count of the loop if we can compute it, and 0807 /// the result is a small constant. '0' is used to represent an unknown 0808 /// or non-constant trip count. Note that a trip count is simply one more 0809 /// than the backedge taken count for the loop. 0810 unsigned getSmallConstantTripCount(const Loop *L); 0811 0812 /// Return the exact trip count for this loop if we exit through ExitingBlock. 0813 /// '0' is used to represent an unknown or non-constant trip count. Note 0814 /// that a trip count is simply one more than the backedge taken count for 0815 /// the same exit. 0816 /// This "trip count" assumes that control exits via ExitingBlock. More 0817 /// precisely, it is the number of times that control will reach ExitingBlock 0818 /// before taking the branch. For loops with multiple exits, it may not be 0819 /// the number times that the loop header executes if the loop exits 0820 /// prematurely via another branch. 0821 unsigned getSmallConstantTripCount(const Loop *L, 0822 const BasicBlock *ExitingBlock); 0823 0824 /// Returns the upper bound of the loop trip count as a normal unsigned 0825 /// value. 0826 /// Returns 0 if the trip count is unknown, not constant or requires 0827 /// SCEV predicates and \p Predicates is nullptr. 0828 unsigned getSmallConstantMaxTripCount( 0829 const Loop *L, 0830 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr); 0831 0832 /// Returns the largest constant divisor of the trip count as a normal 0833 /// unsigned value, if possible. This means that the actual trip count is 0834 /// always a multiple of the returned value. Returns 1 if the trip count is 0835 /// unknown or not guaranteed to be the multiple of a constant., Will also 0836 /// return 1 if the trip count is very large (>= 2^32). 0837 /// Note that the argument is an exit count for loop L, NOT a trip count. 0838 unsigned getSmallConstantTripMultiple(const Loop *L, 0839 const SCEV *ExitCount); 0840 0841 /// Returns the largest constant divisor of the trip count of the 0842 /// loop. Will return 1 if no trip count could be computed, or if a 0843 /// divisor could not be found. 0844 unsigned getSmallConstantTripMultiple(const Loop *L); 0845 0846 /// Returns the largest constant divisor of the trip count of this loop as a 0847 /// normal unsigned value, if possible. This means that the actual trip 0848 /// count is always a multiple of the returned value (don't forget the trip 0849 /// count could very well be zero as well!). As explained in the comments 0850 /// for getSmallConstantTripCount, this assumes that control exits the loop 0851 /// via ExitingBlock. 0852 unsigned getSmallConstantTripMultiple(const Loop *L, 0853 const BasicBlock *ExitingBlock); 0854 0855 /// The terms "backedge taken count" and "exit count" are used 0856 /// interchangeably to refer to the number of times the backedge of a loop 0857 /// has executed before the loop is exited. 0858 enum ExitCountKind { 0859 /// An expression exactly describing the number of times the backedge has 0860 /// executed when a loop is exited. 0861 Exact, 0862 /// A constant which provides an upper bound on the exact trip count. 0863 ConstantMaximum, 0864 /// An expression which provides an upper bound on the exact trip count. 0865 SymbolicMaximum, 0866 }; 0867 0868 /// Return the number of times the backedge executes before the given exit 0869 /// would be taken; if not exactly computable, return SCEVCouldNotCompute. 0870 /// For a single exit loop, this value is equivelent to the result of 0871 /// getBackedgeTakenCount. The loop is guaranteed to exit (via *some* exit) 0872 /// before the backedge is executed (ExitCount + 1) times. Note that there 0873 /// is no guarantee about *which* exit is taken on the exiting iteration. 0874 const SCEV *getExitCount(const Loop *L, const BasicBlock *ExitingBlock, 0875 ExitCountKind Kind = Exact); 0876 0877 /// Same as above except this uses the predicated backedge taken info and 0878 /// may require predicates. 0879 const SCEV * 0880 getPredicatedExitCount(const Loop *L, const BasicBlock *ExitingBlock, 0881 SmallVectorImpl<const SCEVPredicate *> *Predicates, 0882 ExitCountKind Kind = Exact); 0883 0884 /// If the specified loop has a predictable backedge-taken count, return it, 0885 /// otherwise return a SCEVCouldNotCompute object. The backedge-taken count is 0886 /// the number of times the loop header will be branched to from within the 0887 /// loop, assuming there are no abnormal exists like exception throws. This is 0888 /// one less than the trip count of the loop, since it doesn't count the first 0889 /// iteration, when the header is branched to from outside the loop. 0890 /// 0891 /// Note that it is not valid to call this method on a loop without a 0892 /// loop-invariant backedge-taken count (see 0893 /// hasLoopInvariantBackedgeTakenCount). 0894 const SCEV *getBackedgeTakenCount(const Loop *L, ExitCountKind Kind = Exact); 0895 0896 /// Similar to getBackedgeTakenCount, except it will add a set of 0897 /// SCEV predicates to Predicates that are required to be true in order for 0898 /// the answer to be correct. Predicates can be checked with run-time 0899 /// checks and can be used to perform loop versioning. 0900 const SCEV *getPredicatedBackedgeTakenCount( 0901 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Predicates); 0902 0903 /// When successful, this returns a SCEVConstant that is greater than or equal 0904 /// to (i.e. a "conservative over-approximation") of the value returend by 0905 /// getBackedgeTakenCount. If such a value cannot be computed, it returns the 0906 /// SCEVCouldNotCompute object. 0907 const SCEV *getConstantMaxBackedgeTakenCount(const Loop *L) { 0908 return getBackedgeTakenCount(L, ConstantMaximum); 0909 } 0910 0911 /// Similar to getConstantMaxBackedgeTakenCount, except it will add a set of 0912 /// SCEV predicates to Predicates that are required to be true in order for 0913 /// the answer to be correct. Predicates can be checked with run-time 0914 /// checks and can be used to perform loop versioning. 0915 const SCEV *getPredicatedConstantMaxBackedgeTakenCount( 0916 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Predicates); 0917 0918 /// When successful, this returns a SCEV that is greater than or equal 0919 /// to (i.e. a "conservative over-approximation") of the value returend by 0920 /// getBackedgeTakenCount. If such a value cannot be computed, it returns the 0921 /// SCEVCouldNotCompute object. 0922 const SCEV *getSymbolicMaxBackedgeTakenCount(const Loop *L) { 0923 return getBackedgeTakenCount(L, SymbolicMaximum); 0924 } 0925 0926 /// Similar to getSymbolicMaxBackedgeTakenCount, except it will add a set of 0927 /// SCEV predicates to Predicates that are required to be true in order for 0928 /// the answer to be correct. Predicates can be checked with run-time 0929 /// checks and can be used to perform loop versioning. 0930 const SCEV *getPredicatedSymbolicMaxBackedgeTakenCount( 0931 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Predicates); 0932 0933 /// Return true if the backedge taken count is either the value returned by 0934 /// getConstantMaxBackedgeTakenCount or zero. 0935 bool isBackedgeTakenCountMaxOrZero(const Loop *L); 0936 0937 /// Return true if the specified loop has an analyzable loop-invariant 0938 /// backedge-taken count. 0939 bool hasLoopInvariantBackedgeTakenCount(const Loop *L); 0940 0941 // This method should be called by the client when it made any change that 0942 // would invalidate SCEV's answers, and the client wants to remove all loop 0943 // information held internally by ScalarEvolution. This is intended to be used 0944 // when the alternative to forget a loop is too expensive (i.e. large loop 0945 // bodies). 0946 void forgetAllLoops(); 0947 0948 /// This method should be called by the client when it has changed a loop in 0949 /// a way that may effect ScalarEvolution's ability to compute a trip count, 0950 /// or if the loop is deleted. This call is potentially expensive for large 0951 /// loop bodies. 0952 void forgetLoop(const Loop *L); 0953 0954 // This method invokes forgetLoop for the outermost loop of the given loop 0955 // \p L, making ScalarEvolution forget about all this subtree. This needs to 0956 // be done whenever we make a transform that may affect the parameters of the 0957 // outer loop, such as exit counts for branches. 0958 void forgetTopmostLoop(const Loop *L); 0959 0960 /// This method should be called by the client when it has changed a value 0961 /// in a way that may effect its value, or which may disconnect it from a 0962 /// def-use chain linking it to a loop. 0963 void forgetValue(Value *V); 0964 0965 /// Forget LCSSA phi node V of loop L to which a new predecessor was added, 0966 /// such that it may no longer be trivial. 0967 void forgetLcssaPhiWithNewPredecessor(Loop *L, PHINode *V); 0968 0969 /// Called when the client has changed the disposition of values in 0970 /// this loop. 0971 /// 0972 /// We don't have a way to invalidate per-loop dispositions. Clear and 0973 /// recompute is simpler. 0974 void forgetLoopDispositions(); 0975 0976 /// Called when the client has changed the disposition of values in 0977 /// a loop or block. 0978 /// 0979 /// We don't have a way to invalidate per-loop/per-block dispositions. Clear 0980 /// and recompute is simpler. 0981 void forgetBlockAndLoopDispositions(Value *V = nullptr); 0982 0983 /// Determine the minimum number of zero bits that S is guaranteed to end in 0984 /// (at every loop iteration). It is, at the same time, the minimum number 0985 /// of times S is divisible by 2. For example, given {4,+,8} it returns 2. 0986 /// If S is guaranteed to be 0, it returns the bitwidth of S. 0987 uint32_t getMinTrailingZeros(const SCEV *S); 0988 0989 /// Returns the max constant multiple of S. 0990 APInt getConstantMultiple(const SCEV *S); 0991 0992 // Returns the max constant multiple of S. If S is exactly 0, return 1. 0993 APInt getNonZeroConstantMultiple(const SCEV *S); 0994 0995 /// Determine the unsigned range for a particular SCEV. 0996 /// NOTE: This returns a copy of the reference returned by getRangeRef. 0997 ConstantRange getUnsignedRange(const SCEV *S) { 0998 return getRangeRef(S, HINT_RANGE_UNSIGNED); 0999 } 1000 1001 /// Determine the min of the unsigned range for a particular SCEV. 1002 APInt getUnsignedRangeMin(const SCEV *S) { 1003 return getRangeRef(S, HINT_RANGE_UNSIGNED).getUnsignedMin(); 1004 } 1005 1006 /// Determine the max of the unsigned range for a particular SCEV. 1007 APInt getUnsignedRangeMax(const SCEV *S) { 1008 return getRangeRef(S, HINT_RANGE_UNSIGNED).getUnsignedMax(); 1009 } 1010 1011 /// Determine the signed range for a particular SCEV. 1012 /// NOTE: This returns a copy of the reference returned by getRangeRef. 1013 ConstantRange getSignedRange(const SCEV *S) { 1014 return getRangeRef(S, HINT_RANGE_SIGNED); 1015 } 1016 1017 /// Determine the min of the signed range for a particular SCEV. 1018 APInt getSignedRangeMin(const SCEV *S) { 1019 return getRangeRef(S, HINT_RANGE_SIGNED).getSignedMin(); 1020 } 1021 1022 /// Determine the max of the signed range for a particular SCEV. 1023 APInt getSignedRangeMax(const SCEV *S) { 1024 return getRangeRef(S, HINT_RANGE_SIGNED).getSignedMax(); 1025 } 1026 1027 /// Test if the given expression is known to be negative. 1028 bool isKnownNegative(const SCEV *S); 1029 1030 /// Test if the given expression is known to be positive. 1031 bool isKnownPositive(const SCEV *S); 1032 1033 /// Test if the given expression is known to be non-negative. 1034 bool isKnownNonNegative(const SCEV *S); 1035 1036 /// Test if the given expression is known to be non-positive. 1037 bool isKnownNonPositive(const SCEV *S); 1038 1039 /// Test if the given expression is known to be non-zero. 1040 bool isKnownNonZero(const SCEV *S); 1041 1042 /// Test if the given expression is known to be a power of 2. OrNegative 1043 /// allows matching negative power of 2s, and OrZero allows matching 0. 1044 bool isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero = false, 1045 bool OrNegative = false); 1046 1047 /// Splits SCEV expression \p S into two SCEVs. One of them is obtained from 1048 /// \p S by substitution of all AddRec sub-expression related to loop \p L 1049 /// with initial value of that SCEV. The second is obtained from \p S by 1050 /// substitution of all AddRec sub-expressions related to loop \p L with post 1051 /// increment of this AddRec in the loop \p L. In both cases all other AddRec 1052 /// sub-expressions (not related to \p L) remain the same. 1053 /// If the \p S contains non-invariant unknown SCEV the function returns 1054 /// CouldNotCompute SCEV in both values of std::pair. 1055 /// For example, for SCEV S={0, +, 1}<L1> + {0, +, 1}<L2> and loop L=L1 1056 /// the function returns pair: 1057 /// first = {0, +, 1}<L2> 1058 /// second = {1, +, 1}<L1> + {0, +, 1}<L2> 1059 /// We can see that for the first AddRec sub-expression it was replaced with 1060 /// 0 (initial value) for the first element and to {1, +, 1}<L1> (post 1061 /// increment value) for the second one. In both cases AddRec expression 1062 /// related to L2 remains the same. 1063 std::pair<const SCEV *, const SCEV *> SplitIntoInitAndPostInc(const Loop *L, 1064 const SCEV *S); 1065 1066 /// We'd like to check the predicate on every iteration of the most dominated 1067 /// loop between loops used in LHS and RHS. 1068 /// To do this we use the following list of steps: 1069 /// 1. Collect set S all loops on which either LHS or RHS depend. 1070 /// 2. If S is non-empty 1071 /// a. Let PD be the element of S which is dominated by all other elements. 1072 /// b. Let E(LHS) be value of LHS on entry of PD. 1073 /// To get E(LHS), we should just take LHS and replace all AddRecs that are 1074 /// attached to PD on with their entry values. 1075 /// Define E(RHS) in the same way. 1076 /// c. Let B(LHS) be value of L on backedge of PD. 1077 /// To get B(LHS), we should just take LHS and replace all AddRecs that are 1078 /// attached to PD on with their backedge values. 1079 /// Define B(RHS) in the same way. 1080 /// d. Note that E(LHS) and E(RHS) are automatically available on entry of PD, 1081 /// so we can assert on that. 1082 /// e. Return true if isLoopEntryGuardedByCond(Pred, E(LHS), E(RHS)) && 1083 /// isLoopBackedgeGuardedByCond(Pred, B(LHS), B(RHS)) 1084 bool isKnownViaInduction(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS); 1085 1086 /// Test if the given expression is known to satisfy the condition described 1087 /// by Pred, LHS, and RHS. 1088 bool isKnownPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS); 1089 1090 /// Check whether the condition described by Pred, LHS, and RHS is true or 1091 /// false. If we know it, return the evaluation of this condition. If neither 1092 /// is proved, return std::nullopt. 1093 std::optional<bool> evaluatePredicate(CmpPredicate Pred, const SCEV *LHS, 1094 const SCEV *RHS); 1095 1096 /// Test if the given expression is known to satisfy the condition described 1097 /// by Pred, LHS, and RHS in the given Context. 1098 bool isKnownPredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, 1099 const Instruction *CtxI); 1100 1101 /// Check whether the condition described by Pred, LHS, and RHS is true or 1102 /// false in the given \p Context. If we know it, return the evaluation of 1103 /// this condition. If neither is proved, return std::nullopt. 1104 std::optional<bool> evaluatePredicateAt(CmpPredicate Pred, const SCEV *LHS, 1105 const SCEV *RHS, 1106 const Instruction *CtxI); 1107 1108 /// Test if the condition described by Pred, LHS, RHS is known to be true on 1109 /// every iteration of the loop of the recurrency LHS. 1110 bool isKnownOnEveryIteration(CmpPredicate Pred, const SCEVAddRecExpr *LHS, 1111 const SCEV *RHS); 1112 1113 /// Information about the number of loop iterations for which a loop exit's 1114 /// branch condition evaluates to the not-taken path. This is a temporary 1115 /// pair of exact and max expressions that are eventually summarized in 1116 /// ExitNotTakenInfo and BackedgeTakenInfo. 1117 struct ExitLimit { 1118 const SCEV *ExactNotTaken; // The exit is not taken exactly this many times 1119 const SCEV *ConstantMaxNotTaken; // The exit is not taken at most this many 1120 // times 1121 const SCEV *SymbolicMaxNotTaken; 1122 1123 // Not taken either exactly ConstantMaxNotTaken or zero times 1124 bool MaxOrZero = false; 1125 1126 /// A vector of predicate guards for this ExitLimit. The result is only 1127 /// valid if all of the predicates in \c Predicates evaluate to 'true' at 1128 /// run-time. 1129 SmallVector<const SCEVPredicate *, 4> Predicates; 1130 1131 /// Construct either an exact exit limit from a constant, or an unknown 1132 /// one from a SCEVCouldNotCompute. No other types of SCEVs are allowed 1133 /// as arguments and asserts enforce that internally. 1134 /*implicit*/ ExitLimit(const SCEV *E); 1135 1136 ExitLimit(const SCEV *E, const SCEV *ConstantMaxNotTaken, 1137 const SCEV *SymbolicMaxNotTaken, bool MaxOrZero, 1138 ArrayRef<ArrayRef<const SCEVPredicate *>> PredLists = {}); 1139 1140 ExitLimit(const SCEV *E, const SCEV *ConstantMaxNotTaken, 1141 const SCEV *SymbolicMaxNotTaken, bool MaxOrZero, 1142 ArrayRef<const SCEVPredicate *> PredList); 1143 1144 /// Test whether this ExitLimit contains any computed information, or 1145 /// whether it's all SCEVCouldNotCompute values. 1146 bool hasAnyInfo() const { 1147 return !isa<SCEVCouldNotCompute>(ExactNotTaken) || 1148 !isa<SCEVCouldNotCompute>(ConstantMaxNotTaken); 1149 } 1150 1151 /// Test whether this ExitLimit contains all information. 1152 bool hasFullInfo() const { 1153 return !isa<SCEVCouldNotCompute>(ExactNotTaken); 1154 } 1155 }; 1156 1157 /// Compute the number of times the backedge of the specified loop will 1158 /// execute if its exit condition were a conditional branch of ExitCond. 1159 /// 1160 /// \p ControlsOnlyExit is true if ExitCond directly controls the only exit 1161 /// branch. In this case, we can assume that the loop exits only if the 1162 /// condition is true and can infer that failing to meet the condition prior 1163 /// to integer wraparound results in undefined behavior. 1164 /// 1165 /// If \p AllowPredicates is set, this call will try to use a minimal set of 1166 /// SCEV predicates in order to return an exact answer. 1167 ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond, 1168 bool ExitIfTrue, bool ControlsOnlyExit, 1169 bool AllowPredicates = false); 1170 1171 /// A predicate is said to be monotonically increasing if may go from being 1172 /// false to being true as the loop iterates, but never the other way 1173 /// around. A predicate is said to be monotonically decreasing if may go 1174 /// from being true to being false as the loop iterates, but never the other 1175 /// way around. 1176 enum MonotonicPredicateType { 1177 MonotonicallyIncreasing, 1178 MonotonicallyDecreasing 1179 }; 1180 1181 /// If, for all loop invariant X, the predicate "LHS `Pred` X" is 1182 /// monotonically increasing or decreasing, returns 1183 /// Some(MonotonicallyIncreasing) and Some(MonotonicallyDecreasing) 1184 /// respectively. If we could not prove either of these facts, returns 1185 /// std::nullopt. 1186 std::optional<MonotonicPredicateType> 1187 getMonotonicPredicateType(const SCEVAddRecExpr *LHS, 1188 ICmpInst::Predicate Pred); 1189 1190 struct LoopInvariantPredicate { 1191 ICmpInst::Predicate Pred; 1192 const SCEV *LHS; 1193 const SCEV *RHS; 1194 1195 LoopInvariantPredicate(ICmpInst::Predicate Pred, const SCEV *LHS, 1196 const SCEV *RHS) 1197 : Pred(Pred), LHS(LHS), RHS(RHS) {} 1198 }; 1199 /// If the result of the predicate LHS `Pred` RHS is loop invariant with 1200 /// respect to L, return a LoopInvariantPredicate with LHS and RHS being 1201 /// invariants, available at L's entry. Otherwise, return std::nullopt. 1202 std::optional<LoopInvariantPredicate> 1203 getLoopInvariantPredicate(ICmpInst::Predicate Pred, const SCEV *LHS, 1204 const SCEV *RHS, const Loop *L, 1205 const Instruction *CtxI = nullptr); 1206 1207 /// If the result of the predicate LHS `Pred` RHS is loop invariant with 1208 /// respect to L at given Context during at least first MaxIter iterations, 1209 /// return a LoopInvariantPredicate with LHS and RHS being invariants, 1210 /// available at L's entry. Otherwise, return std::nullopt. The predicate 1211 /// should be the loop's exit condition. 1212 std::optional<LoopInvariantPredicate> 1213 getLoopInvariantExitCondDuringFirstIterations(CmpPredicate Pred, 1214 const SCEV *LHS, 1215 const SCEV *RHS, const Loop *L, 1216 const Instruction *CtxI, 1217 const SCEV *MaxIter); 1218 1219 std::optional<LoopInvariantPredicate> 1220 getLoopInvariantExitCondDuringFirstIterationsImpl( 1221 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, 1222 const Instruction *CtxI, const SCEV *MaxIter); 1223 1224 /// Simplify LHS and RHS in a comparison with predicate Pred. Return true 1225 /// iff any changes were made. If the operands are provably equal or 1226 /// unequal, LHS and RHS are set to the same value and Pred is set to either 1227 /// ICMP_EQ or ICMP_NE. 1228 bool SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS, 1229 const SCEV *&RHS, unsigned Depth = 0); 1230 1231 /// Return the "disposition" of the given SCEV with respect to the given 1232 /// loop. 1233 LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L); 1234 1235 /// Return true if the value of the given SCEV is unchanging in the 1236 /// specified loop. 1237 bool isLoopInvariant(const SCEV *S, const Loop *L); 1238 1239 /// Determine if the SCEV can be evaluated at loop's entry. It is true if it 1240 /// doesn't depend on a SCEVUnknown of an instruction which is dominated by 1241 /// the header of loop L. 1242 bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L); 1243 1244 /// Return true if the given SCEV changes value in a known way in the 1245 /// specified loop. This property being true implies that the value is 1246 /// variant in the loop AND that we can emit an expression to compute the 1247 /// value of the expression at any particular loop iteration. 1248 bool hasComputableLoopEvolution(const SCEV *S, const Loop *L); 1249 1250 /// Return the "disposition" of the given SCEV with respect to the given 1251 /// block. 1252 BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB); 1253 1254 /// Return true if elements that makes up the given SCEV dominate the 1255 /// specified basic block. 1256 bool dominates(const SCEV *S, const BasicBlock *BB); 1257 1258 /// Return true if elements that makes up the given SCEV properly dominate 1259 /// the specified basic block. 1260 bool properlyDominates(const SCEV *S, const BasicBlock *BB); 1261 1262 /// Test whether the given SCEV has Op as a direct or indirect operand. 1263 bool hasOperand(const SCEV *S, const SCEV *Op) const; 1264 1265 /// Return the size of an element read or written by Inst. 1266 const SCEV *getElementSize(Instruction *Inst); 1267 1268 void print(raw_ostream &OS) const; 1269 void verify() const; 1270 bool invalidate(Function &F, const PreservedAnalyses &PA, 1271 FunctionAnalysisManager::Invalidator &Inv); 1272 1273 /// Return the DataLayout associated with the module this SCEV instance is 1274 /// operating on. 1275 const DataLayout &getDataLayout() const { return DL; } 1276 1277 const SCEVPredicate *getEqualPredicate(const SCEV *LHS, const SCEV *RHS); 1278 const SCEVPredicate *getComparePredicate(ICmpInst::Predicate Pred, 1279 const SCEV *LHS, const SCEV *RHS); 1280 1281 const SCEVPredicate * 1282 getWrapPredicate(const SCEVAddRecExpr *AR, 1283 SCEVWrapPredicate::IncrementWrapFlags AddedFlags); 1284 1285 /// Re-writes the SCEV according to the Predicates in \p A. 1286 const SCEV *rewriteUsingPredicate(const SCEV *S, const Loop *L, 1287 const SCEVPredicate &A); 1288 /// Tries to convert the \p S expression to an AddRec expression, 1289 /// adding additional predicates to \p Preds as required. 1290 const SCEVAddRecExpr *convertSCEVToAddRecWithPredicates( 1291 const SCEV *S, const Loop *L, 1292 SmallVectorImpl<const SCEVPredicate *> &Preds); 1293 1294 /// Compute \p LHS - \p RHS and returns the result as an APInt if it is a 1295 /// constant, and std::nullopt if it isn't. 1296 /// 1297 /// This is intended to be a cheaper version of getMinusSCEV. We can be 1298 /// frugal here since we just bail out of actually constructing and 1299 /// canonicalizing an expression in the cases where the result isn't going 1300 /// to be a constant. 1301 std::optional<APInt> computeConstantDifference(const SCEV *LHS, 1302 const SCEV *RHS); 1303 1304 /// Update no-wrap flags of an AddRec. This may drop the cached info about 1305 /// this AddRec (such as range info) in case if new flags may potentially 1306 /// sharpen it. 1307 void setNoWrapFlags(SCEVAddRecExpr *AddRec, SCEV::NoWrapFlags Flags); 1308 1309 class LoopGuards { 1310 DenseMap<const SCEV *, const SCEV *> RewriteMap; 1311 bool PreserveNUW = false; 1312 bool PreserveNSW = false; 1313 ScalarEvolution &SE; 1314 1315 LoopGuards(ScalarEvolution &SE) : SE(SE) {} 1316 1317 /// Recursively collect loop guards in \p Guards, starting from 1318 /// block \p Block with predecessor \p Pred. The intended starting point 1319 /// is to collect from a loop header and its predecessor. 1320 static void 1321 collectFromBlock(ScalarEvolution &SE, ScalarEvolution::LoopGuards &Guards, 1322 const BasicBlock *Block, const BasicBlock *Pred, 1323 SmallPtrSetImpl<const BasicBlock *> &VisitedBlocks, 1324 unsigned Depth = 0); 1325 1326 /// Collect loop guards in \p Guards, starting from PHINode \p 1327 /// Phi, by calling \p collectFromBlock on the incoming blocks of 1328 /// \Phi and trying to merge the found constraints into a single 1329 /// combined one for \p Phi. 1330 static void collectFromPHI( 1331 ScalarEvolution &SE, ScalarEvolution::LoopGuards &Guards, 1332 const PHINode &Phi, SmallPtrSetImpl<const BasicBlock *> &VisitedBlocks, 1333 SmallDenseMap<const BasicBlock *, LoopGuards> &IncomingGuards, 1334 unsigned Depth); 1335 1336 public: 1337 /// Collect rewrite map for loop guards for loop \p L, together with flags 1338 /// indicating if NUW and NSW can be preserved during rewriting. 1339 static LoopGuards collect(const Loop *L, ScalarEvolution &SE); 1340 1341 /// Try to apply the collected loop guards to \p Expr. 1342 const SCEV *rewrite(const SCEV *Expr) const; 1343 }; 1344 1345 /// Try to apply information from loop guards for \p L to \p Expr. 1346 const SCEV *applyLoopGuards(const SCEV *Expr, const Loop *L); 1347 const SCEV *applyLoopGuards(const SCEV *Expr, const LoopGuards &Guards); 1348 1349 /// Return true if the loop has no abnormal exits. That is, if the loop 1350 /// is not infinite, it must exit through an explicit edge in the CFG. 1351 /// (As opposed to either a) throwing out of the function or b) entering a 1352 /// well defined infinite loop in some callee.) 1353 bool loopHasNoAbnormalExits(const Loop *L) { 1354 return getLoopProperties(L).HasNoAbnormalExits; 1355 } 1356 1357 /// Return true if this loop is finite by assumption. That is, 1358 /// to be infinite, it must also be undefined. 1359 bool loopIsFiniteByAssumption(const Loop *L); 1360 1361 /// Return the set of Values that, if poison, will definitively result in S 1362 /// being poison as well. The returned set may be incomplete, i.e. there can 1363 /// be additional Values that also result in S being poison. 1364 void getPoisonGeneratingValues(SmallPtrSetImpl<const Value *> &Result, 1365 const SCEV *S); 1366 1367 /// Check whether it is poison-safe to represent the expression S using the 1368 /// instruction I. If such a replacement is performed, the poison flags of 1369 /// instructions in DropPoisonGeneratingInsts must be dropped. 1370 bool canReuseInstruction( 1371 const SCEV *S, Instruction *I, 1372 SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts); 1373 1374 class FoldID { 1375 const SCEV *Op = nullptr; 1376 const Type *Ty = nullptr; 1377 unsigned short C; 1378 1379 public: 1380 FoldID(SCEVTypes C, const SCEV *Op, const Type *Ty) : Op(Op), Ty(Ty), C(C) { 1381 assert(Op); 1382 assert(Ty); 1383 } 1384 1385 FoldID(unsigned short C) : C(C) {} 1386 1387 unsigned computeHash() const { 1388 return detail::combineHashValue( 1389 C, detail::combineHashValue(reinterpret_cast<uintptr_t>(Op), 1390 reinterpret_cast<uintptr_t>(Ty))); 1391 } 1392 1393 bool operator==(const FoldID &RHS) const { 1394 return std::tie(Op, Ty, C) == std::tie(RHS.Op, RHS.Ty, RHS.C); 1395 } 1396 }; 1397 1398 private: 1399 /// A CallbackVH to arrange for ScalarEvolution to be notified whenever a 1400 /// Value is deleted. 1401 class SCEVCallbackVH final : public CallbackVH { 1402 ScalarEvolution *SE; 1403 1404 void deleted() override; 1405 void allUsesReplacedWith(Value *New) override; 1406 1407 public: 1408 SCEVCallbackVH(Value *V, ScalarEvolution *SE = nullptr); 1409 }; 1410 1411 friend class SCEVCallbackVH; 1412 friend class SCEVExpander; 1413 friend class SCEVUnknown; 1414 1415 /// The function we are analyzing. 1416 Function &F; 1417 1418 /// Data layout of the module. 1419 const DataLayout &DL; 1420 1421 /// Does the module have any calls to the llvm.experimental.guard intrinsic 1422 /// at all? If this is false, we avoid doing work that will only help if 1423 /// thare are guards present in the IR. 1424 bool HasGuards; 1425 1426 /// The target library information for the target we are targeting. 1427 TargetLibraryInfo &TLI; 1428 1429 /// The tracker for \@llvm.assume intrinsics in this function. 1430 AssumptionCache &AC; 1431 1432 /// The dominator tree. 1433 DominatorTree &DT; 1434 1435 /// The loop information for the function we are currently analyzing. 1436 LoopInfo &LI; 1437 1438 /// This SCEV is used to represent unknown trip counts and things. 1439 std::unique_ptr<SCEVCouldNotCompute> CouldNotCompute; 1440 1441 /// The type for HasRecMap. 1442 using HasRecMapType = DenseMap<const SCEV *, bool>; 1443 1444 /// This is a cache to record whether a SCEV contains any scAddRecExpr. 1445 HasRecMapType HasRecMap; 1446 1447 /// The type for ExprValueMap. 1448 using ValueSetVector = SmallSetVector<Value *, 4>; 1449 using ExprValueMapType = DenseMap<const SCEV *, ValueSetVector>; 1450 1451 /// ExprValueMap -- This map records the original values from which 1452 /// the SCEV expr is generated from. 1453 ExprValueMapType ExprValueMap; 1454 1455 /// The type for ValueExprMap. 1456 using ValueExprMapType = 1457 DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *>>; 1458 1459 /// This is a cache of the values we have analyzed so far. 1460 ValueExprMapType ValueExprMap; 1461 1462 /// This is a cache for expressions that got folded to a different existing 1463 /// SCEV. 1464 DenseMap<FoldID, const SCEV *> FoldCache; 1465 DenseMap<const SCEV *, SmallVector<FoldID, 2>> FoldCacheUser; 1466 1467 /// Mark predicate values currently being processed by isImpliedCond. 1468 SmallPtrSet<const Value *, 6> PendingLoopPredicates; 1469 1470 /// Mark SCEVUnknown Phis currently being processed by getRangeRef. 1471 SmallPtrSet<const PHINode *, 6> PendingPhiRanges; 1472 1473 /// Mark SCEVUnknown Phis currently being processed by getRangeRefIter. 1474 SmallPtrSet<const PHINode *, 6> PendingPhiRangesIter; 1475 1476 // Mark SCEVUnknown Phis currently being processed by isImpliedViaMerge. 1477 SmallPtrSet<const PHINode *, 6> PendingMerges; 1478 1479 /// Set to true by isLoopBackedgeGuardedByCond when we're walking the set of 1480 /// conditions dominating the backedge of a loop. 1481 bool WalkingBEDominatingConds = false; 1482 1483 /// Set to true by isKnownPredicateViaSplitting when we're trying to prove a 1484 /// predicate by splitting it into a set of independent predicates. 1485 bool ProvingSplitPredicate = false; 1486 1487 /// Memoized values for the getConstantMultiple 1488 DenseMap<const SCEV *, APInt> ConstantMultipleCache; 1489 1490 /// Return the Value set from which the SCEV expr is generated. 1491 ArrayRef<Value *> getSCEVValues(const SCEV *S); 1492 1493 /// Private helper method for the getConstantMultiple method. 1494 APInt getConstantMultipleImpl(const SCEV *S); 1495 1496 /// Information about the number of times a particular loop exit may be 1497 /// reached before exiting the loop. 1498 struct ExitNotTakenInfo { 1499 PoisoningVH<BasicBlock> ExitingBlock; 1500 const SCEV *ExactNotTaken; 1501 const SCEV *ConstantMaxNotTaken; 1502 const SCEV *SymbolicMaxNotTaken; 1503 SmallVector<const SCEVPredicate *, 4> Predicates; 1504 1505 explicit ExitNotTakenInfo(PoisoningVH<BasicBlock> ExitingBlock, 1506 const SCEV *ExactNotTaken, 1507 const SCEV *ConstantMaxNotTaken, 1508 const SCEV *SymbolicMaxNotTaken, 1509 ArrayRef<const SCEVPredicate *> Predicates) 1510 : ExitingBlock(ExitingBlock), ExactNotTaken(ExactNotTaken), 1511 ConstantMaxNotTaken(ConstantMaxNotTaken), 1512 SymbolicMaxNotTaken(SymbolicMaxNotTaken), Predicates(Predicates) {} 1513 1514 bool hasAlwaysTruePredicate() const { 1515 return Predicates.empty(); 1516 } 1517 }; 1518 1519 /// Information about the backedge-taken count of a loop. This currently 1520 /// includes an exact count and a maximum count. 1521 /// 1522 class BackedgeTakenInfo { 1523 friend class ScalarEvolution; 1524 1525 /// A list of computable exits and their not-taken counts. Loops almost 1526 /// never have more than one computable exit. 1527 SmallVector<ExitNotTakenInfo, 1> ExitNotTaken; 1528 1529 /// Expression indicating the least constant maximum backedge-taken count of 1530 /// the loop that is known, or a SCEVCouldNotCompute. This expression is 1531 /// only valid if the predicates associated with all loop exits are true. 1532 const SCEV *ConstantMax = nullptr; 1533 1534 /// Indicating if \c ExitNotTaken has an element for every exiting block in 1535 /// the loop. 1536 bool IsComplete = false; 1537 1538 /// Expression indicating the least maximum backedge-taken count of the loop 1539 /// that is known, or a SCEVCouldNotCompute. Lazily computed on first query. 1540 const SCEV *SymbolicMax = nullptr; 1541 1542 /// True iff the backedge is taken either exactly Max or zero times. 1543 bool MaxOrZero = false; 1544 1545 bool isComplete() const { return IsComplete; } 1546 const SCEV *getConstantMax() const { return ConstantMax; } 1547 1548 const ExitNotTakenInfo *getExitNotTaken( 1549 const BasicBlock *ExitingBlock, 1550 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const; 1551 1552 public: 1553 BackedgeTakenInfo() = default; 1554 BackedgeTakenInfo(BackedgeTakenInfo &&) = default; 1555 BackedgeTakenInfo &operator=(BackedgeTakenInfo &&) = default; 1556 1557 using EdgeExitInfo = std::pair<BasicBlock *, ExitLimit>; 1558 1559 /// Initialize BackedgeTakenInfo from a list of exact exit counts. 1560 BackedgeTakenInfo(ArrayRef<EdgeExitInfo> ExitCounts, bool IsComplete, 1561 const SCEV *ConstantMax, bool MaxOrZero); 1562 1563 /// Test whether this BackedgeTakenInfo contains any computed information, 1564 /// or whether it's all SCEVCouldNotCompute values. 1565 bool hasAnyInfo() const { 1566 return !ExitNotTaken.empty() || 1567 !isa<SCEVCouldNotCompute>(getConstantMax()); 1568 } 1569 1570 /// Test whether this BackedgeTakenInfo contains complete information. 1571 bool hasFullInfo() const { return isComplete(); } 1572 1573 /// Return an expression indicating the exact *backedge-taken* 1574 /// count of the loop if it is known or SCEVCouldNotCompute 1575 /// otherwise. If execution makes it to the backedge on every 1576 /// iteration (i.e. there are no abnormal exists like exception 1577 /// throws and thread exits) then this is the number of times the 1578 /// loop header will execute minus one. 1579 /// 1580 /// If the SCEV predicate associated with the answer can be different 1581 /// from AlwaysTrue, we must add a (non null) Predicates argument. 1582 /// The SCEV predicate associated with the answer will be added to 1583 /// Predicates. A run-time check needs to be emitted for the SCEV 1584 /// predicate in order for the answer to be valid. 1585 /// 1586 /// Note that we should always know if we need to pass a predicate 1587 /// argument or not from the way the ExitCounts vector was computed. 1588 /// If we allowed SCEV predicates to be generated when populating this 1589 /// vector, this information can contain them and therefore a 1590 /// SCEVPredicate argument should be added to getExact. 1591 const SCEV *getExact( 1592 const Loop *L, ScalarEvolution *SE, 1593 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const; 1594 1595 /// Return the number of times this loop exit may fall through to the back 1596 /// edge, or SCEVCouldNotCompute. The loop is guaranteed not to exit via 1597 /// this block before this number of iterations, but may exit via another 1598 /// block. If \p Predicates is null the function returns CouldNotCompute if 1599 /// predicates are required, otherwise it fills in the required predicates. 1600 const SCEV *getExact( 1601 const BasicBlock *ExitingBlock, ScalarEvolution *SE, 1602 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const { 1603 if (auto *ENT = getExitNotTaken(ExitingBlock, Predicates)) 1604 return ENT->ExactNotTaken; 1605 else 1606 return SE->getCouldNotCompute(); 1607 } 1608 1609 /// Get the constant max backedge taken count for the loop. 1610 const SCEV *getConstantMax( 1611 ScalarEvolution *SE, 1612 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const; 1613 1614 /// Get the constant max backedge taken count for the particular loop exit. 1615 const SCEV *getConstantMax( 1616 const BasicBlock *ExitingBlock, ScalarEvolution *SE, 1617 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const { 1618 if (auto *ENT = getExitNotTaken(ExitingBlock, Predicates)) 1619 return ENT->ConstantMaxNotTaken; 1620 else 1621 return SE->getCouldNotCompute(); 1622 } 1623 1624 /// Get the symbolic max backedge taken count for the loop. 1625 const SCEV *getSymbolicMax( 1626 const Loop *L, ScalarEvolution *SE, 1627 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr); 1628 1629 /// Get the symbolic max backedge taken count for the particular loop exit. 1630 const SCEV *getSymbolicMax( 1631 const BasicBlock *ExitingBlock, ScalarEvolution *SE, 1632 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const { 1633 if (auto *ENT = getExitNotTaken(ExitingBlock, Predicates)) 1634 return ENT->SymbolicMaxNotTaken; 1635 else 1636 return SE->getCouldNotCompute(); 1637 } 1638 1639 /// Return true if the number of times this backedge is taken is either the 1640 /// value returned by getConstantMax or zero. 1641 bool isConstantMaxOrZero(ScalarEvolution *SE) const; 1642 }; 1643 1644 /// Cache the backedge-taken count of the loops for this function as they 1645 /// are computed. 1646 DenseMap<const Loop *, BackedgeTakenInfo> BackedgeTakenCounts; 1647 1648 /// Cache the predicated backedge-taken count of the loops for this 1649 /// function as they are computed. 1650 DenseMap<const Loop *, BackedgeTakenInfo> PredicatedBackedgeTakenCounts; 1651 1652 /// Loops whose backedge taken counts directly use this non-constant SCEV. 1653 DenseMap<const SCEV *, SmallPtrSet<PointerIntPair<const Loop *, 1, bool>, 4>> 1654 BECountUsers; 1655 1656 /// This map contains entries for all of the PHI instructions that we 1657 /// attempt to compute constant evolutions for. This allows us to avoid 1658 /// potentially expensive recomputation of these properties. An instruction 1659 /// maps to null if we are unable to compute its exit value. 1660 DenseMap<PHINode *, Constant *> ConstantEvolutionLoopExitValue; 1661 1662 /// This map contains entries for all the expressions that we attempt to 1663 /// compute getSCEVAtScope information for, which can be expensive in 1664 /// extreme cases. 1665 DenseMap<const SCEV *, SmallVector<std::pair<const Loop *, const SCEV *>, 2>> 1666 ValuesAtScopes; 1667 1668 /// Reverse map for invalidation purposes: Stores of which SCEV and which 1669 /// loop this is the value-at-scope of. 1670 DenseMap<const SCEV *, SmallVector<std::pair<const Loop *, const SCEV *>, 2>> 1671 ValuesAtScopesUsers; 1672 1673 /// Memoized computeLoopDisposition results. 1674 DenseMap<const SCEV *, 1675 SmallVector<PointerIntPair<const Loop *, 2, LoopDisposition>, 2>> 1676 LoopDispositions; 1677 1678 struct LoopProperties { 1679 /// Set to true if the loop contains no instruction that can abnormally exit 1680 /// the loop (i.e. via throwing an exception, by terminating the thread 1681 /// cleanly or by infinite looping in a called function). Strictly 1682 /// speaking, the last one is not leaving the loop, but is identical to 1683 /// leaving the loop for reasoning about undefined behavior. 1684 bool HasNoAbnormalExits; 1685 1686 /// Set to true if the loop contains no instruction that can have side 1687 /// effects (i.e. via throwing an exception, volatile or atomic access). 1688 bool HasNoSideEffects; 1689 }; 1690 1691 /// Cache for \c getLoopProperties. 1692 DenseMap<const Loop *, LoopProperties> LoopPropertiesCache; 1693 1694 /// Return a \c LoopProperties instance for \p L, creating one if necessary. 1695 LoopProperties getLoopProperties(const Loop *L); 1696 1697 bool loopHasNoSideEffects(const Loop *L) { 1698 return getLoopProperties(L).HasNoSideEffects; 1699 } 1700 1701 /// Compute a LoopDisposition value. 1702 LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L); 1703 1704 /// Memoized computeBlockDisposition results. 1705 DenseMap< 1706 const SCEV *, 1707 SmallVector<PointerIntPair<const BasicBlock *, 2, BlockDisposition>, 2>> 1708 BlockDispositions; 1709 1710 /// Compute a BlockDisposition value. 1711 BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB); 1712 1713 /// Stores all SCEV that use a given SCEV as its direct operand. 1714 DenseMap<const SCEV *, SmallPtrSet<const SCEV *, 8> > SCEVUsers; 1715 1716 /// Memoized results from getRange 1717 DenseMap<const SCEV *, ConstantRange> UnsignedRanges; 1718 1719 /// Memoized results from getRange 1720 DenseMap<const SCEV *, ConstantRange> SignedRanges; 1721 1722 /// Used to parameterize getRange 1723 enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED }; 1724 1725 /// Set the memoized range for the given SCEV. 1726 const ConstantRange &setRange(const SCEV *S, RangeSignHint Hint, 1727 ConstantRange CR) { 1728 DenseMap<const SCEV *, ConstantRange> &Cache = 1729 Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges; 1730 1731 auto Pair = Cache.insert_or_assign(S, std::move(CR)); 1732 return Pair.first->second; 1733 } 1734 1735 /// Determine the range for a particular SCEV. 1736 /// NOTE: This returns a reference to an entry in a cache. It must be 1737 /// copied if its needed for longer. 1738 const ConstantRange &getRangeRef(const SCEV *S, RangeSignHint Hint, 1739 unsigned Depth = 0); 1740 1741 /// Determine the range for a particular SCEV, but evaluates ranges for 1742 /// operands iteratively first. 1743 const ConstantRange &getRangeRefIter(const SCEV *S, RangeSignHint Hint); 1744 1745 /// Determines the range for the affine SCEVAddRecExpr {\p Start,+,\p Step}. 1746 /// Helper for \c getRange. 1747 ConstantRange getRangeForAffineAR(const SCEV *Start, const SCEV *Step, 1748 const APInt &MaxBECount); 1749 1750 /// Determines the range for the affine non-self-wrapping SCEVAddRecExpr {\p 1751 /// Start,+,\p Step}<nw>. 1752 ConstantRange getRangeForAffineNoSelfWrappingAR(const SCEVAddRecExpr *AddRec, 1753 const SCEV *MaxBECount, 1754 unsigned BitWidth, 1755 RangeSignHint SignHint); 1756 1757 /// Try to compute a range for the affine SCEVAddRecExpr {\p Start,+,\p 1758 /// Step} by "factoring out" a ternary expression from the add recurrence. 1759 /// Helper called by \c getRange. 1760 ConstantRange getRangeViaFactoring(const SCEV *Start, const SCEV *Step, 1761 const APInt &MaxBECount); 1762 1763 /// If the unknown expression U corresponds to a simple recurrence, return 1764 /// a constant range which represents the entire recurrence. Note that 1765 /// *add* recurrences with loop invariant steps aren't represented by 1766 /// SCEVUnknowns and thus don't use this mechanism. 1767 ConstantRange getRangeForUnknownRecurrence(const SCEVUnknown *U); 1768 1769 /// We know that there is no SCEV for the specified value. Analyze the 1770 /// expression recursively. 1771 const SCEV *createSCEV(Value *V); 1772 1773 /// We know that there is no SCEV for the specified value. Create a new SCEV 1774 /// for \p V iteratively. 1775 const SCEV *createSCEVIter(Value *V); 1776 /// Collect operands of \p V for which SCEV expressions should be constructed 1777 /// first. Returns a SCEV directly if it can be constructed trivially for \p 1778 /// V. 1779 const SCEV *getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops); 1780 1781 /// Returns SCEV for the first operand of a phi if all phi operands have 1782 /// identical opcodes and operands. 1783 const SCEV *createNodeForPHIWithIdenticalOperands(PHINode *PN); 1784 1785 /// Provide the special handling we need to analyze PHI SCEVs. 1786 const SCEV *createNodeForPHI(PHINode *PN); 1787 1788 /// Helper function called from createNodeForPHI. 1789 const SCEV *createAddRecFromPHI(PHINode *PN); 1790 1791 /// A helper function for createAddRecFromPHI to handle simple cases. 1792 const SCEV *createSimpleAffineAddRec(PHINode *PN, Value *BEValueV, 1793 Value *StartValueV); 1794 1795 /// Helper function called from createNodeForPHI. 1796 const SCEV *createNodeFromSelectLikePHI(PHINode *PN); 1797 1798 /// Provide special handling for a select-like instruction (currently this 1799 /// is either a select instruction or a phi node). \p Ty is the type of the 1800 /// instruction being processed, that is assumed equivalent to 1801 /// "Cond ? TrueVal : FalseVal". 1802 std::optional<const SCEV *> 1803 createNodeForSelectOrPHIInstWithICmpInstCond(Type *Ty, ICmpInst *Cond, 1804 Value *TrueVal, Value *FalseVal); 1805 1806 /// See if we can model this select-like instruction via umin_seq expression. 1807 const SCEV *createNodeForSelectOrPHIViaUMinSeq(Value *I, Value *Cond, 1808 Value *TrueVal, 1809 Value *FalseVal); 1810 1811 /// Given a value \p V, which is a select-like instruction (currently this is 1812 /// either a select instruction or a phi node), which is assumed equivalent to 1813 /// Cond ? TrueVal : FalseVal 1814 /// see if we can model it as a SCEV expression. 1815 const SCEV *createNodeForSelectOrPHI(Value *V, Value *Cond, Value *TrueVal, 1816 Value *FalseVal); 1817 1818 /// Provide the special handling we need to analyze GEP SCEVs. 1819 const SCEV *createNodeForGEP(GEPOperator *GEP); 1820 1821 /// Implementation code for getSCEVAtScope; called at most once for each 1822 /// SCEV+Loop pair. 1823 const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L); 1824 1825 /// Return the BackedgeTakenInfo for the given loop, lazily computing new 1826 /// values if the loop hasn't been analyzed yet. The returned result is 1827 /// guaranteed not to be predicated. 1828 BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L); 1829 1830 /// Similar to getBackedgeTakenInfo, but will add predicates as required 1831 /// with the purpose of returning complete information. 1832 BackedgeTakenInfo &getPredicatedBackedgeTakenInfo(const Loop *L); 1833 1834 /// Compute the number of times the specified loop will iterate. 1835 /// If AllowPredicates is set, we will create new SCEV predicates as 1836 /// necessary in order to return an exact answer. 1837 BackedgeTakenInfo computeBackedgeTakenCount(const Loop *L, 1838 bool AllowPredicates = false); 1839 1840 /// Compute the number of times the backedge of the specified loop will 1841 /// execute if it exits via the specified block. If AllowPredicates is set, 1842 /// this call will try to use a minimal set of SCEV predicates in order to 1843 /// return an exact answer. 1844 ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock, 1845 bool IsOnlyExit, bool AllowPredicates = false); 1846 1847 // Helper functions for computeExitLimitFromCond to avoid exponential time 1848 // complexity. 1849 1850 class ExitLimitCache { 1851 // It may look like we need key on the whole (L, ExitIfTrue, 1852 // ControlsOnlyExit, AllowPredicates) tuple, but recursive calls to 1853 // computeExitLimitFromCondCached from computeExitLimitFromCondImpl only 1854 // vary the in \c ExitCond and \c ControlsOnlyExit parameters. We remember 1855 // the initial values of the other values to assert our assumption. 1856 SmallDenseMap<PointerIntPair<Value *, 1>, ExitLimit> TripCountMap; 1857 1858 const Loop *L; 1859 bool ExitIfTrue; 1860 bool AllowPredicates; 1861 1862 public: 1863 ExitLimitCache(const Loop *L, bool ExitIfTrue, bool AllowPredicates) 1864 : L(L), ExitIfTrue(ExitIfTrue), AllowPredicates(AllowPredicates) {} 1865 1866 std::optional<ExitLimit> find(const Loop *L, Value *ExitCond, 1867 bool ExitIfTrue, bool ControlsOnlyExit, 1868 bool AllowPredicates); 1869 1870 void insert(const Loop *L, Value *ExitCond, bool ExitIfTrue, 1871 bool ControlsOnlyExit, bool AllowPredicates, 1872 const ExitLimit &EL); 1873 }; 1874 1875 using ExitLimitCacheTy = ExitLimitCache; 1876 1877 ExitLimit computeExitLimitFromCondCached(ExitLimitCacheTy &Cache, 1878 const Loop *L, Value *ExitCond, 1879 bool ExitIfTrue, 1880 bool ControlsOnlyExit, 1881 bool AllowPredicates); 1882 ExitLimit computeExitLimitFromCondImpl(ExitLimitCacheTy &Cache, const Loop *L, 1883 Value *ExitCond, bool ExitIfTrue, 1884 bool ControlsOnlyExit, 1885 bool AllowPredicates); 1886 std::optional<ScalarEvolution::ExitLimit> computeExitLimitFromCondFromBinOp( 1887 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, 1888 bool ControlsOnlyExit, bool AllowPredicates); 1889 1890 /// Compute the number of times the backedge of the specified loop will 1891 /// execute if its exit condition were a conditional branch of the ICmpInst 1892 /// ExitCond and ExitIfTrue. If AllowPredicates is set, this call will try 1893 /// to use a minimal set of SCEV predicates in order to return an exact 1894 /// answer. 1895 ExitLimit computeExitLimitFromICmp(const Loop *L, ICmpInst *ExitCond, 1896 bool ExitIfTrue, 1897 bool IsSubExpr, 1898 bool AllowPredicates = false); 1899 1900 /// Variant of previous which takes the components representing an ICmp 1901 /// as opposed to the ICmpInst itself. Note that the prior version can 1902 /// return more precise results in some cases and is preferred when caller 1903 /// has a materialized ICmp. 1904 ExitLimit computeExitLimitFromICmp(const Loop *L, CmpPredicate Pred, 1905 const SCEV *LHS, const SCEV *RHS, 1906 bool IsSubExpr, 1907 bool AllowPredicates = false); 1908 1909 /// Compute the number of times the backedge of the specified loop will 1910 /// execute if its exit condition were a switch with a single exiting case 1911 /// to ExitingBB. 1912 ExitLimit computeExitLimitFromSingleExitSwitch(const Loop *L, 1913 SwitchInst *Switch, 1914 BasicBlock *ExitingBB, 1915 bool IsSubExpr); 1916 1917 /// Compute the exit limit of a loop that is controlled by a 1918 /// "(IV >> 1) != 0" type comparison. We cannot compute the exact trip 1919 /// count in these cases (since SCEV has no way of expressing them), but we 1920 /// can still sometimes compute an upper bound. 1921 /// 1922 /// Return an ExitLimit for a loop whose backedge is guarded by `LHS Pred 1923 /// RHS`. 1924 ExitLimit computeShiftCompareExitLimit(Value *LHS, Value *RHS, const Loop *L, 1925 ICmpInst::Predicate Pred); 1926 1927 /// If the loop is known to execute a constant number of times (the 1928 /// condition evolves only from constants), try to evaluate a few iterations 1929 /// of the loop until we get the exit condition gets a value of ExitWhen 1930 /// (true or false). If we cannot evaluate the exit count of the loop, 1931 /// return CouldNotCompute. 1932 const SCEV *computeExitCountExhaustively(const Loop *L, Value *Cond, 1933 bool ExitWhen); 1934 1935 /// Return the number of times an exit condition comparing the specified 1936 /// value to zero will execute. If not computable, return CouldNotCompute. 1937 /// If AllowPredicates is set, this call will try to use a minimal set of 1938 /// SCEV predicates in order to return an exact answer. 1939 ExitLimit howFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr, 1940 bool AllowPredicates = false); 1941 1942 /// Return the number of times an exit condition checking the specified 1943 /// value for nonzero will execute. If not computable, return 1944 /// CouldNotCompute. 1945 ExitLimit howFarToNonZero(const SCEV *V, const Loop *L); 1946 1947 /// Return the number of times an exit condition containing the specified 1948 /// less-than comparison will execute. If not computable, return 1949 /// CouldNotCompute. 1950 /// 1951 /// \p isSigned specifies whether the less-than is signed. 1952 /// 1953 /// \p ControlsOnlyExit is true when the LHS < RHS condition directly controls 1954 /// the branch (loops exits only if condition is true). In this case, we can 1955 /// use NoWrapFlags to skip overflow checks. 1956 /// 1957 /// If \p AllowPredicates is set, this call will try to use a minimal set of 1958 /// SCEV predicates in order to return an exact answer. 1959 ExitLimit howManyLessThans(const SCEV *LHS, const SCEV *RHS, const Loop *L, 1960 bool isSigned, bool ControlsOnlyExit, 1961 bool AllowPredicates = false); 1962 1963 ExitLimit howManyGreaterThans(const SCEV *LHS, const SCEV *RHS, const Loop *L, 1964 bool isSigned, bool IsSubExpr, 1965 bool AllowPredicates = false); 1966 1967 /// Return a predecessor of BB (which may not be an immediate predecessor) 1968 /// which has exactly one successor from which BB is reachable, or null if 1969 /// no such block is found. 1970 std::pair<const BasicBlock *, const BasicBlock *> 1971 getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB) const; 1972 1973 /// Test whether the condition described by Pred, LHS, and RHS is true 1974 /// whenever the given FoundCondValue value evaluates to true in given 1975 /// Context. If Context is nullptr, then the found predicate is true 1976 /// everywhere. LHS and FoundLHS may have different type width. 1977 bool isImpliedCond(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, 1978 const Value *FoundCondValue, bool Inverse, 1979 const Instruction *Context = nullptr); 1980 1981 /// Test whether the condition described by Pred, LHS, and RHS is true 1982 /// whenever the given FoundCondValue value evaluates to true in given 1983 /// Context. If Context is nullptr, then the found predicate is true 1984 /// everywhere. LHS and FoundLHS must have same type width. 1985 bool isImpliedCondBalancedTypes(CmpPredicate Pred, const SCEV *LHS, 1986 const SCEV *RHS, CmpPredicate FoundPred, 1987 const SCEV *FoundLHS, const SCEV *FoundRHS, 1988 const Instruction *CtxI); 1989 1990 /// Test whether the condition described by Pred, LHS, and RHS is true 1991 /// whenever the condition described by FoundPred, FoundLHS, FoundRHS is 1992 /// true in given Context. If Context is nullptr, then the found predicate is 1993 /// true everywhere. 1994 bool isImpliedCond(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, 1995 CmpPredicate FoundPred, const SCEV *FoundLHS, 1996 const SCEV *FoundRHS, 1997 const Instruction *Context = nullptr); 1998 1999 /// Test whether the condition described by Pred, LHS, and RHS is true 2000 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is 2001 /// true in given Context. If Context is nullptr, then the found predicate is 2002 /// true everywhere. 2003 bool isImpliedCondOperands(CmpPredicate Pred, const SCEV *LHS, 2004 const SCEV *RHS, const SCEV *FoundLHS, 2005 const SCEV *FoundRHS, 2006 const Instruction *Context = nullptr); 2007 2008 /// Test whether the condition described by Pred, LHS, and RHS is true 2009 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is 2010 /// true. Here LHS is an operation that includes FoundLHS as one of its 2011 /// arguments. 2012 bool isImpliedViaOperations(CmpPredicate Pred, const SCEV *LHS, 2013 const SCEV *RHS, const SCEV *FoundLHS, 2014 const SCEV *FoundRHS, unsigned Depth = 0); 2015 2016 /// Test whether the condition described by Pred, LHS, and RHS is true. 2017 /// Use only simple non-recursive types of checks, such as range analysis etc. 2018 bool isKnownViaNonRecursiveReasoning(CmpPredicate Pred, const SCEV *LHS, 2019 const SCEV *RHS); 2020 2021 /// Test whether the condition described by Pred, LHS, and RHS is true 2022 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is 2023 /// true. 2024 bool isImpliedCondOperandsHelper(CmpPredicate Pred, const SCEV *LHS, 2025 const SCEV *RHS, const SCEV *FoundLHS, 2026 const SCEV *FoundRHS); 2027 2028 /// Test whether the condition described by Pred, LHS, and RHS is true 2029 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is 2030 /// true. Utility function used by isImpliedCondOperands. Tries to get 2031 /// cases like "X `sgt` 0 => X - 1 `sgt` -1". 2032 bool isImpliedCondOperandsViaRanges(CmpPredicate Pred, const SCEV *LHS, 2033 const SCEV *RHS, CmpPredicate FoundPred, 2034 const SCEV *FoundLHS, 2035 const SCEV *FoundRHS); 2036 2037 /// Return true if the condition denoted by \p LHS \p Pred \p RHS is implied 2038 /// by a call to @llvm.experimental.guard in \p BB. 2039 bool isImpliedViaGuard(const BasicBlock *BB, CmpPredicate Pred, 2040 const SCEV *LHS, const SCEV *RHS); 2041 2042 /// Test whether the condition described by Pred, LHS, and RHS is true 2043 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is 2044 /// true. 2045 /// 2046 /// This routine tries to rule out certain kinds of integer overflow, and 2047 /// then tries to reason about arithmetic properties of the predicates. 2048 bool isImpliedCondOperandsViaNoOverflow(CmpPredicate Pred, const SCEV *LHS, 2049 const SCEV *RHS, const SCEV *FoundLHS, 2050 const SCEV *FoundRHS); 2051 2052 /// Test whether the condition described by Pred, LHS, and RHS is true 2053 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is 2054 /// true. 2055 /// 2056 /// This routine tries to weaken the known condition basing on fact that 2057 /// FoundLHS is an AddRec. 2058 bool isImpliedCondOperandsViaAddRecStart(CmpPredicate Pred, const SCEV *LHS, 2059 const SCEV *RHS, 2060 const SCEV *FoundLHS, 2061 const SCEV *FoundRHS, 2062 const Instruction *CtxI); 2063 2064 /// Test whether the condition described by Pred, LHS, and RHS is true 2065 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is 2066 /// true. 2067 /// 2068 /// This routine tries to figure out predicate for Phis which are SCEVUnknown 2069 /// if it is true for every possible incoming value from their respective 2070 /// basic blocks. 2071 bool isImpliedViaMerge(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, 2072 const SCEV *FoundLHS, const SCEV *FoundRHS, 2073 unsigned Depth); 2074 2075 /// Test whether the condition described by Pred, LHS, and RHS is true 2076 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is 2077 /// true. 2078 /// 2079 /// This routine tries to reason about shifts. 2080 bool isImpliedCondOperandsViaShift(CmpPredicate Pred, const SCEV *LHS, 2081 const SCEV *RHS, const SCEV *FoundLHS, 2082 const SCEV *FoundRHS); 2083 2084 /// If we know that the specified Phi is in the header of its containing 2085 /// loop, we know the loop executes a constant number of times, and the PHI 2086 /// node is just a recurrence involving constants, fold it. 2087 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt &BEs, 2088 const Loop *L); 2089 2090 /// Test if the given expression is known to satisfy the condition described 2091 /// by Pred and the known constant ranges of LHS and RHS. 2092 bool isKnownPredicateViaConstantRanges(CmpPredicate Pred, const SCEV *LHS, 2093 const SCEV *RHS); 2094 2095 /// Try to prove the condition described by "LHS Pred RHS" by ruling out 2096 /// integer overflow. 2097 /// 2098 /// For instance, this will return true for "A s< (A + C)<nsw>" if C is 2099 /// positive. 2100 bool isKnownPredicateViaNoOverflow(CmpPredicate Pred, const SCEV *LHS, 2101 const SCEV *RHS); 2102 2103 /// Try to split Pred LHS RHS into logical conjunctions (and's) and try to 2104 /// prove them individually. 2105 bool isKnownPredicateViaSplitting(CmpPredicate Pred, const SCEV *LHS, 2106 const SCEV *RHS); 2107 2108 /// Try to match the Expr as "(L + R)<Flags>". 2109 bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R, 2110 SCEV::NoWrapFlags &Flags); 2111 2112 /// Forget predicated/non-predicated backedge taken counts for the given loop. 2113 void forgetBackedgeTakenCounts(const Loop *L, bool Predicated); 2114 2115 /// Drop memoized information for all \p SCEVs. 2116 void forgetMemoizedResults(ArrayRef<const SCEV *> SCEVs); 2117 2118 /// Helper for forgetMemoizedResults. 2119 void forgetMemoizedResultsImpl(const SCEV *S); 2120 2121 /// Iterate over instructions in \p Worklist and their users. Erase entries 2122 /// from ValueExprMap and collect SCEV expressions in \p ToForget 2123 void visitAndClearUsers(SmallVectorImpl<Instruction *> &Worklist, 2124 SmallPtrSetImpl<Instruction *> &Visited, 2125 SmallVectorImpl<const SCEV *> &ToForget); 2126 2127 /// Erase Value from ValueExprMap and ExprValueMap. 2128 void eraseValueFromMap(Value *V); 2129 2130 /// Insert V to S mapping into ValueExprMap and ExprValueMap. 2131 void insertValueToMap(Value *V, const SCEV *S); 2132 2133 /// Return false iff given SCEV contains a SCEVUnknown with NULL value- 2134 /// pointer. 2135 bool checkValidity(const SCEV *S) const; 2136 2137 /// Return true if `ExtendOpTy`({`Start`,+,`Step`}) can be proved to be 2138 /// equal to {`ExtendOpTy`(`Start`),+,`ExtendOpTy`(`Step`)}. This is 2139 /// equivalent to proving no signed (resp. unsigned) wrap in 2140 /// {`Start`,+,`Step`} if `ExtendOpTy` is `SCEVSignExtendExpr` 2141 /// (resp. `SCEVZeroExtendExpr`). 2142 template <typename ExtendOpTy> 2143 bool proveNoWrapByVaryingStart(const SCEV *Start, const SCEV *Step, 2144 const Loop *L); 2145 2146 /// Try to prove NSW or NUW on \p AR relying on ConstantRange manipulation. 2147 SCEV::NoWrapFlags proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR); 2148 2149 /// Try to prove NSW on \p AR by proving facts about conditions known on 2150 /// entry and backedge. 2151 SCEV::NoWrapFlags proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR); 2152 2153 /// Try to prove NUW on \p AR by proving facts about conditions known on 2154 /// entry and backedge. 2155 SCEV::NoWrapFlags proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR); 2156 2157 std::optional<MonotonicPredicateType> 2158 getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS, 2159 ICmpInst::Predicate Pred); 2160 2161 /// Return SCEV no-wrap flags that can be proven based on reasoning about 2162 /// how poison produced from no-wrap flags on this value (e.g. a nuw add) 2163 /// would trigger undefined behavior on overflow. 2164 SCEV::NoWrapFlags getNoWrapFlagsFromUB(const Value *V); 2165 2166 /// Return a scope which provides an upper bound on the defining scope of 2167 /// 'S'. Specifically, return the first instruction in said bounding scope. 2168 /// Return nullptr if the scope is trivial (function entry). 2169 /// (See scope definition rules associated with flag discussion above) 2170 const Instruction *getNonTrivialDefiningScopeBound(const SCEV *S); 2171 2172 /// Return a scope which provides an upper bound on the defining scope for 2173 /// a SCEV with the operands in Ops. The outparam Precise is set if the 2174 /// bound found is a precise bound (i.e. must be the defining scope.) 2175 const Instruction *getDefiningScopeBound(ArrayRef<const SCEV *> Ops, 2176 bool &Precise); 2177 2178 /// Wrapper around the above for cases which don't care if the bound 2179 /// is precise. 2180 const Instruction *getDefiningScopeBound(ArrayRef<const SCEV *> Ops); 2181 2182 /// Given two instructions in the same function, return true if we can 2183 /// prove B must execute given A executes. 2184 bool isGuaranteedToTransferExecutionTo(const Instruction *A, 2185 const Instruction *B); 2186 2187 /// Returns true if \p Op is guaranteed not to cause immediate UB. 2188 bool isGuaranteedNotToCauseUB(const SCEV *Op); 2189 2190 /// Returns true if \p Op is guaranteed to not be poison. 2191 static bool isGuaranteedNotToBePoison(const SCEV *Op); 2192 2193 /// Return true if the SCEV corresponding to \p I is never poison. Proving 2194 /// this is more complex than proving that just \p I is never poison, since 2195 /// SCEV commons expressions across control flow, and you can have cases 2196 /// like: 2197 /// 2198 /// idx0 = a + b; 2199 /// ptr[idx0] = 100; 2200 /// if (<condition>) { 2201 /// idx1 = a +nsw b; 2202 /// ptr[idx1] = 200; 2203 /// } 2204 /// 2205 /// where the SCEV expression (+ a b) is guaranteed to not be poison (and 2206 /// hence not sign-overflow) only if "<condition>" is true. Since both 2207 /// `idx0` and `idx1` will be mapped to the same SCEV expression, (+ a b), 2208 /// it is not okay to annotate (+ a b) with <nsw> in the above example. 2209 bool isSCEVExprNeverPoison(const Instruction *I); 2210 2211 /// This is like \c isSCEVExprNeverPoison but it specifically works for 2212 /// instructions that will get mapped to SCEV add recurrences. Return true 2213 /// if \p I will never generate poison under the assumption that \p I is an 2214 /// add recurrence on the loop \p L. 2215 bool isAddRecNeverPoison(const Instruction *I, const Loop *L); 2216 2217 /// Similar to createAddRecFromPHI, but with the additional flexibility of 2218 /// suggesting runtime overflow checks in case casts are encountered. 2219 /// If successful, the analysis records that for this loop, \p SymbolicPHI, 2220 /// which is the UnknownSCEV currently representing the PHI, can be rewritten 2221 /// into an AddRec, assuming some predicates; The function then returns the 2222 /// AddRec and the predicates as a pair, and caches this pair in 2223 /// PredicatedSCEVRewrites. 2224 /// If the analysis is not successful, a mapping from the \p SymbolicPHI to 2225 /// itself (with no predicates) is recorded, and a nullptr with an empty 2226 /// predicates vector is returned as a pair. 2227 std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>> 2228 createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI); 2229 2230 /// Compute the maximum backedge count based on the range of values 2231 /// permitted by Start, End, and Stride. This is for loops of the form 2232 /// {Start, +, Stride} LT End. 2233 /// 2234 /// Preconditions: 2235 /// * the induction variable is known to be positive. 2236 /// * the induction variable is assumed not to overflow (i.e. either it 2237 /// actually doesn't, or we'd have to immediately execute UB) 2238 /// We *don't* assert these preconditions so please be careful. 2239 const SCEV *computeMaxBECountForLT(const SCEV *Start, const SCEV *Stride, 2240 const SCEV *End, unsigned BitWidth, 2241 bool IsSigned); 2242 2243 /// Verify if an linear IV with positive stride can overflow when in a 2244 /// less-than comparison, knowing the invariant term of the comparison, 2245 /// the stride. 2246 bool canIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, bool IsSigned); 2247 2248 /// Verify if an linear IV with negative stride can overflow when in a 2249 /// greater-than comparison, knowing the invariant term of the comparison, 2250 /// the stride. 2251 bool canIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, bool IsSigned); 2252 2253 /// Get add expr already created or create a new one. 2254 const SCEV *getOrCreateAddExpr(ArrayRef<const SCEV *> Ops, 2255 SCEV::NoWrapFlags Flags); 2256 2257 /// Get mul expr already created or create a new one. 2258 const SCEV *getOrCreateMulExpr(ArrayRef<const SCEV *> Ops, 2259 SCEV::NoWrapFlags Flags); 2260 2261 // Get addrec expr already created or create a new one. 2262 const SCEV *getOrCreateAddRecExpr(ArrayRef<const SCEV *> Ops, 2263 const Loop *L, SCEV::NoWrapFlags Flags); 2264 2265 /// Return x if \p Val is f(x) where f is a 1-1 function. 2266 const SCEV *stripInjectiveFunctions(const SCEV *Val) const; 2267 2268 /// Find all of the loops transitively used in \p S, and fill \p LoopsUsed. 2269 /// A loop is considered "used" by an expression if it contains 2270 /// an add rec on said loop. 2271 void getUsedLoops(const SCEV *S, SmallPtrSetImpl<const Loop *> &LoopsUsed); 2272 2273 /// Try to match the pattern generated by getURemExpr(A, B). If successful, 2274 /// Assign A and B to LHS and RHS, respectively. 2275 bool matchURem(const SCEV *Expr, const SCEV *&LHS, const SCEV *&RHS); 2276 2277 /// Look for a SCEV expression with type `SCEVType` and operands `Ops` in 2278 /// `UniqueSCEVs`. Return if found, else nullptr. 2279 SCEV *findExistingSCEVInCache(SCEVTypes SCEVType, ArrayRef<const SCEV *> Ops); 2280 2281 /// Get reachable blocks in this function, making limited use of SCEV 2282 /// reasoning about conditions. 2283 void getReachableBlocks(SmallPtrSetImpl<BasicBlock *> &Reachable, 2284 Function &F); 2285 2286 /// Return the given SCEV expression with a new set of operands. 2287 /// This preserves the origial nowrap flags. 2288 const SCEV *getWithOperands(const SCEV *S, 2289 SmallVectorImpl<const SCEV *> &NewOps); 2290 2291 FoldingSet<SCEV> UniqueSCEVs; 2292 FoldingSet<SCEVPredicate> UniquePreds; 2293 BumpPtrAllocator SCEVAllocator; 2294 2295 /// This maps loops to a list of addrecs that directly use said loop. 2296 DenseMap<const Loop *, SmallVector<const SCEVAddRecExpr *, 4>> LoopUsers; 2297 2298 /// Cache tentative mappings from UnknownSCEVs in a Loop, to a SCEV expression 2299 /// they can be rewritten into under certain predicates. 2300 DenseMap<std::pair<const SCEVUnknown *, const Loop *>, 2301 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>> 2302 PredicatedSCEVRewrites; 2303 2304 /// Set of AddRecs for which proving NUW via an induction has already been 2305 /// tried. 2306 SmallPtrSet<const SCEVAddRecExpr *, 16> UnsignedWrapViaInductionTried; 2307 2308 /// Set of AddRecs for which proving NSW via an induction has already been 2309 /// tried. 2310 SmallPtrSet<const SCEVAddRecExpr *, 16> SignedWrapViaInductionTried; 2311 2312 /// The head of a linked list of all SCEVUnknown values that have been 2313 /// allocated. This is used by releaseMemory to locate them all and call 2314 /// their destructors. 2315 SCEVUnknown *FirstUnknown = nullptr; 2316 }; 2317 2318 /// Analysis pass that exposes the \c ScalarEvolution for a function. 2319 class ScalarEvolutionAnalysis 2320 : public AnalysisInfoMixin<ScalarEvolutionAnalysis> { 2321 friend AnalysisInfoMixin<ScalarEvolutionAnalysis>; 2322 2323 static AnalysisKey Key; 2324 2325 public: 2326 using Result = ScalarEvolution; 2327 2328 ScalarEvolution run(Function &F, FunctionAnalysisManager &AM); 2329 }; 2330 2331 /// Verifier pass for the \c ScalarEvolutionAnalysis results. 2332 class ScalarEvolutionVerifierPass 2333 : public PassInfoMixin<ScalarEvolutionVerifierPass> { 2334 public: 2335 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 2336 static bool isRequired() { return true; } 2337 }; 2338 2339 /// Printer pass for the \c ScalarEvolutionAnalysis results. 2340 class ScalarEvolutionPrinterPass 2341 : public PassInfoMixin<ScalarEvolutionPrinterPass> { 2342 raw_ostream &OS; 2343 2344 public: 2345 explicit ScalarEvolutionPrinterPass(raw_ostream &OS) : OS(OS) {} 2346 2347 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); 2348 2349 static bool isRequired() { return true; } 2350 }; 2351 2352 class ScalarEvolutionWrapperPass : public FunctionPass { 2353 std::unique_ptr<ScalarEvolution> SE; 2354 2355 public: 2356 static char ID; 2357 2358 ScalarEvolutionWrapperPass(); 2359 2360 ScalarEvolution &getSE() { return *SE; } 2361 const ScalarEvolution &getSE() const { return *SE; } 2362 2363 bool runOnFunction(Function &F) override; 2364 void releaseMemory() override; 2365 void getAnalysisUsage(AnalysisUsage &AU) const override; 2366 void print(raw_ostream &OS, const Module * = nullptr) const override; 2367 void verifyAnalysis() const override; 2368 }; 2369 2370 /// An interface layer with SCEV used to manage how we see SCEV expressions 2371 /// for values in the context of existing predicates. We can add new 2372 /// predicates, but we cannot remove them. 2373 /// 2374 /// This layer has multiple purposes: 2375 /// - provides a simple interface for SCEV versioning. 2376 /// - guarantees that the order of transformations applied on a SCEV 2377 /// expression for a single Value is consistent across two different 2378 /// getSCEV calls. This means that, for example, once we've obtained 2379 /// an AddRec expression for a certain value through expression 2380 /// rewriting, we will continue to get an AddRec expression for that 2381 /// Value. 2382 /// - lowers the number of expression rewrites. 2383 class PredicatedScalarEvolution { 2384 public: 2385 PredicatedScalarEvolution(ScalarEvolution &SE, Loop &L); 2386 2387 const SCEVPredicate &getPredicate() const; 2388 2389 /// Returns the SCEV expression of V, in the context of the current SCEV 2390 /// predicate. The order of transformations applied on the expression of V 2391 /// returned by ScalarEvolution is guaranteed to be preserved, even when 2392 /// adding new predicates. 2393 const SCEV *getSCEV(Value *V); 2394 2395 /// Get the (predicated) backedge count for the analyzed loop. 2396 const SCEV *getBackedgeTakenCount(); 2397 2398 /// Get the (predicated) symbolic max backedge count for the analyzed loop. 2399 const SCEV *getSymbolicMaxBackedgeTakenCount(); 2400 2401 /// Returns the upper bound of the loop trip count as a normal unsigned 2402 /// value, or 0 if the trip count is unknown. 2403 unsigned getSmallConstantMaxTripCount(); 2404 2405 /// Adds a new predicate. 2406 void addPredicate(const SCEVPredicate &Pred); 2407 2408 /// Attempts to produce an AddRecExpr for V by adding additional SCEV 2409 /// predicates. If we can't transform the expression into an AddRecExpr we 2410 /// return nullptr and not add additional SCEV predicates to the current 2411 /// context. 2412 const SCEVAddRecExpr *getAsAddRec(Value *V); 2413 2414 /// Proves that V doesn't overflow by adding SCEV predicate. 2415 void setNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags); 2416 2417 /// Returns true if we've proved that V doesn't wrap by means of a SCEV 2418 /// predicate. 2419 bool hasNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags); 2420 2421 /// Returns the ScalarEvolution analysis used. 2422 ScalarEvolution *getSE() const { return &SE; } 2423 2424 /// We need to explicitly define the copy constructor because of FlagsMap. 2425 PredicatedScalarEvolution(const PredicatedScalarEvolution &); 2426 2427 /// Print the SCEV mappings done by the Predicated Scalar Evolution. 2428 /// The printed text is indented by \p Depth. 2429 void print(raw_ostream &OS, unsigned Depth) const; 2430 2431 /// Check if \p AR1 and \p AR2 are equal, while taking into account 2432 /// Equal predicates in Preds. 2433 bool areAddRecsEqualWithPreds(const SCEVAddRecExpr *AR1, 2434 const SCEVAddRecExpr *AR2) const; 2435 2436 private: 2437 /// Increments the version number of the predicate. This needs to be called 2438 /// every time the SCEV predicate changes. 2439 void updateGeneration(); 2440 2441 /// Holds a SCEV and the version number of the SCEV predicate used to 2442 /// perform the rewrite of the expression. 2443 using RewriteEntry = std::pair<unsigned, const SCEV *>; 2444 2445 /// Maps a SCEV to the rewrite result of that SCEV at a certain version 2446 /// number. If this number doesn't match the current Generation, we will 2447 /// need to do a rewrite. To preserve the transformation order of previous 2448 /// rewrites, we will rewrite the previous result instead of the original 2449 /// SCEV. 2450 DenseMap<const SCEV *, RewriteEntry> RewriteMap; 2451 2452 /// Records what NoWrap flags we've added to a Value *. 2453 ValueMap<Value *, SCEVWrapPredicate::IncrementWrapFlags> FlagsMap; 2454 2455 /// The ScalarEvolution analysis. 2456 ScalarEvolution &SE; 2457 2458 /// The analyzed Loop. 2459 const Loop &L; 2460 2461 /// The SCEVPredicate that forms our context. We will rewrite all 2462 /// expressions assuming that this predicate true. 2463 std::unique_ptr<SCEVUnionPredicate> Preds; 2464 2465 /// Marks the version of the SCEV predicate used. When rewriting a SCEV 2466 /// expression we mark it with the version of the predicate. We use this to 2467 /// figure out if the predicate has changed from the last rewrite of the 2468 /// SCEV. If so, we need to perform a new rewrite. 2469 unsigned Generation = 0; 2470 2471 /// The backedge taken count. 2472 const SCEV *BackedgeCount = nullptr; 2473 2474 /// The symbolic backedge taken count. 2475 const SCEV *SymbolicMaxBackedgeCount = nullptr; 2476 2477 /// The constant max trip count for the loop. 2478 std::optional<unsigned> SmallConstantMaxTripCount; 2479 }; 2480 2481 template <> struct DenseMapInfo<ScalarEvolution::FoldID> { 2482 static inline ScalarEvolution::FoldID getEmptyKey() { 2483 ScalarEvolution::FoldID ID(0); 2484 return ID; 2485 } 2486 static inline ScalarEvolution::FoldID getTombstoneKey() { 2487 ScalarEvolution::FoldID ID(1); 2488 return ID; 2489 } 2490 2491 static unsigned getHashValue(const ScalarEvolution::FoldID &Val) { 2492 return Val.computeHash(); 2493 } 2494 2495 static bool isEqual(const ScalarEvolution::FoldID &LHS, 2496 const ScalarEvolution::FoldID &RHS) { 2497 return LHS == RHS; 2498 } 2499 }; 2500 2501 } // end namespace llvm 2502 2503 #endif // LLVM_ANALYSIS_SCALAREVOLUTION_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|