LLVM 23.0.0git
ScalarEvolution.h
Go to the documentation of this file.
1//===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The ScalarEvolution class is an LLVM pass which can be used to analyze and
10// categorize scalar expressions in loops. It specializes in recognizing
11// general induction variables, representing them with the abstract and opaque
12// SCEV class. Given this analysis, trip counts of loops and other important
13// properties can be obtained.
14//
15// This analysis is primarily useful for induction variable substitution and
16// strength reduction.
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
21#define LLVM_ANALYSIS_SCALAREVOLUTION_H
22
23#include "llvm/ADT/APInt.h"
24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/DenseMap.h"
27#include "llvm/ADT/FoldingSet.h"
29#include "llvm/ADT/SetVector.h"
34#include "llvm/IR/PassManager.h"
35#include "llvm/IR/ValueHandle.h"
36#include "llvm/IR/ValueMap.h"
37#include "llvm/Pass.h"
39#include <cassert>
40#include <cstdint>
41#include <memory>
42#include <optional>
43#include <utility>
44
45namespace llvm {
46
48class AssumptionCache;
49class BasicBlock;
50class Constant;
51class ConstantInt;
52class DataLayout;
53class DominatorTree;
54class GEPOperator;
55class LLVMContext;
56class Loop;
57class LoopInfo;
58class raw_ostream;
59class ScalarEvolution;
60class SCEVAddRecExpr;
61class SCEVUnknown;
62class StructType;
64class Type;
65enum SCEVTypes : unsigned short;
66
67LLVM_ABI extern bool VerifySCEV;
68
69class SCEV;
70
71template <typename SCEVPtrT = const SCEV *>
72struct SCEVUseT : PointerIntPair<SCEVPtrT, 2> {
74
76 SCEVUseT(SCEVPtrT S) : SCEVUseT(S, 0) {}
77 SCEVUseT(SCEVPtrT S, unsigned Flags) : Base(S, Flags) {}
78 template <typename OtherPtrT, typename = std::enable_if_t<
79 std::is_convertible_v<OtherPtrT, SCEVPtrT>>>
82
83 operator SCEVPtrT() const { return Base::getPointer(); }
84 SCEVPtrT operator->() const { return Base::getPointer(); }
85
86 void *getRawPointer() const { return Base::getOpaqueValue(); }
87
88 /// Returns true of the SCEVUse is canonical, i.e. no SCEVUse flags set in any
89 /// operands.
90 bool isCanonical() const { return getCanonical() == getRawPointer(); }
91
92 /// Return the canonical SCEV for this SCEVUse.
93 const SCEV *getCanonical() const;
94
95 unsigned getFlags() const { return Base::getInt(); }
96
97 bool operator==(const SCEVUseT &RHS) const {
98 return getRawPointer() == RHS.getRawPointer();
99 }
100
101 bool operator==(const SCEV *RHS) const { return getRawPointer() == RHS; }
102
103 /// Print out the internal representation of this scalar to the specified
104 /// stream. This should really only be used for debugging purposes.
105 void print(raw_ostream &OS) const;
106
107 /// This method is used for debugging.
108 void dump() const;
109};
110
111/// Deduction guide for various SCEV subclass pointers.
112template <typename SCEVPtrT> SCEVUseT(SCEVPtrT) -> SCEVUseT<SCEVPtrT>;
113
115
116/// Provide PointerLikeTypeTraits for SCEVUse, so it can be used with
117/// SmallPtrSet, among others.
118template <> struct PointerLikeTypeTraits<SCEVUse> {
119 static inline void *getAsVoidPointer(SCEVUse U) { return U.getOpaqueValue(); }
120 static inline SCEVUse getFromVoidPointer(void *P) {
121 SCEVUse U;
122 U.setFromOpaqueValue(P);
123 return U;
124 }
125
126 /// The Low bits are used by the PointerIntPair.
127 static constexpr int NumLowBitsAvailable = 0;
128};
129
130template <> struct DenseMapInfo<SCEVUse> {
131 static inline SCEVUse getEmptyKey() {
132 uintptr_t Val = static_cast<uintptr_t>(-1);
134 }
135
136 static inline SCEVUse getTombstoneKey() {
137 uintptr_t Val = static_cast<uintptr_t>(-2);
139 }
140
141 static unsigned getHashValue(SCEVUse U) {
142 return hash_value(U.getRawPointer());
143 }
144
145 static bool isEqual(const SCEVUse LHS, const SCEVUse RHS) {
146 return LHS.getRawPointer() == RHS.getRawPointer();
147 }
148};
149
150template <> struct simplify_type<SCEVUse> {
151 using SimpleType = const SCEV *;
152
154 return Val.getPointer();
155 }
156};
157
158/// Provide CastInfo for SCEVUseT so that cast<SCEVUseT<const To *>>(use)
159/// returns SCEVUseT<const To *> with flags preserved.
160template <typename ToSCEVPtrT>
161struct CastInfo<SCEVUseT<ToSCEVPtrT>, SCEVUse,
162 std::enable_if_t<!is_simple_type<SCEVUse>::value>> {
163 using To = std::remove_cv_t<std::remove_pointer_t<ToSCEVPtrT>>;
165
166 static bool isPossible(const SCEVUse &U) { return isa<To>(U.getPointer()); }
167 static CastReturnType doCast(const SCEVUse &U) {
168 return {cast<To>(U.getPointer()), U.getFlags()};
169 }
170 static CastReturnType castFailed() { return CastReturnType(nullptr); }
172 if (!isPossible(U))
173 return castFailed();
174 return doCast(U);
175 }
176};
177
178template <typename ToSCEVPtrT>
179struct CastInfo<SCEVUseT<ToSCEVPtrT>, const SCEVUse,
180 std::enable_if_t<!is_simple_type<const SCEVUse>::value>>
181 : CastInfo<SCEVUseT<ToSCEVPtrT>, SCEVUse> {};
182
183/// This class represents an analyzed expression in the program. These are
184/// opaque objects that the client is not allowed to do much with directly.
185///
186class SCEV : public FoldingSetNode {
187 friend struct FoldingSetTrait<SCEV>;
188
189 /// A reference to an Interned FoldingSetNodeID for this node. The
190 /// ScalarEvolution's BumpPtrAllocator holds the data.
191 FoldingSetNodeIDRef FastID;
192
193 // The SCEV baseclass this node corresponds to
194 const SCEVTypes SCEVType;
195
196protected:
197 // Estimated complexity of this node's expression tree size.
198 const unsigned short ExpressionSize;
199
200 /// This field is initialized to zero and may be used in subclasses to store
201 /// miscellaneous information.
202 unsigned short SubclassData = 0;
203
204 /// Pointer to the canonical version of the SCEV, i.e. one where all operands
205 /// have no SCEVUse flags.
206 const SCEV *CanonicalSCEV = nullptr;
207
208public:
209 /// NoWrapFlags are bitfield indices into SubclassData.
210 ///
211 /// Add and Mul expressions may have no-unsigned-wrap <NUW> or
212 /// no-signed-wrap <NSW> properties, which are derived from the IR
213 /// operator. NSW is a misnomer that we use to mean no signed overflow or
214 /// underflow.
215 ///
216 /// AddRec expressions may have a no-self-wraparound <NW> property if, in
217 /// the integer domain, abs(step) * max-iteration(loop) <=
218 /// unsigned-max(bitwidth). This means that the recurrence will never reach
219 /// its start value if the step is non-zero. Computing the same value on
220 /// each iteration is not considered wrapping, and recurrences with step = 0
221 /// are trivially <NW>. <NW> is independent of the sign of step and the
222 /// value the add recurrence starts with.
223 ///
224 /// Note that NUW and NSW are also valid properties of a recurrence, and
225 /// either implies NW. For convenience, NW will be set for a recurrence
226 /// whenever either NUW or NSW are set.
227 ///
228 /// We require that the flag on a SCEV apply to the entire scope in which
229 /// that SCEV is defined. A SCEV's scope is set of locations dominated by
230 /// a defining location, which is in turn described by the following rules:
231 /// * A SCEVUnknown is at the point of definition of the Value.
232 /// * A SCEVConstant is defined at all points.
233 /// * A SCEVAddRec is defined starting with the header of the associated
234 /// loop.
235 /// * All other SCEVs are defined at the earlest point all operands are
236 /// defined.
237 ///
238 /// The above rules describe a maximally hoisted form (without regards to
239 /// potential control dependence). A SCEV is defined anywhere a
240 /// corresponding instruction could be defined in said maximally hoisted
241 /// form. Note that SCEVUDivExpr (currently the only expression type which
242 /// can trap) can be defined per these rules in regions where it would trap
243 /// at runtime. A SCEV being defined does not require the existence of any
244 /// instruction within the defined scope.
246 FlagAnyWrap = 0, // No guarantee.
247 FlagNW = (1 << 0), // No self-wrap.
248 FlagNUW = (1 << 1), // No unsigned wrap.
249 FlagNSW = (1 << 2), // No signed wrap.
250 NoWrapMask = (1 << 3) - 1
251 };
252
253 explicit SCEV(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy,
254 unsigned short ExpressionSize)
255 : FastID(ID), SCEVType(SCEVTy), ExpressionSize(ExpressionSize) {}
256 SCEV(const SCEV &) = delete;
257 SCEV &operator=(const SCEV &) = delete;
258
259 SCEVTypes getSCEVType() const { return SCEVType; }
260
261 /// Return the LLVM type of this SCEV expression.
262 LLVM_ABI Type *getType() const;
263
264 /// Return operands of this SCEV expression.
266
267 /// Return true if the expression is a constant zero.
268 LLVM_ABI bool isZero() const;
269
270 /// Return true if the expression is a constant one.
271 LLVM_ABI bool isOne() const;
272
273 /// Return true if the expression is a constant all-ones value.
274 LLVM_ABI bool isAllOnesValue() const;
275
276 /// Return true if the specified scev is negated, but not a constant.
277 LLVM_ABI bool isNonConstantNegative() const;
278
279 // Returns estimated size of the mathematical expression represented by this
280 // SCEV. The rules of its calculation are following:
281 // 1) Size of a SCEV without operands (like constants and SCEVUnknown) is 1;
282 // 2) Size SCEV with operands Op1, Op2, ..., OpN is calculated by formula:
283 // (1 + Size(Op1) + ... + Size(OpN)).
284 // This value gives us an estimation of time we need to traverse through this
285 // SCEV and all its operands recursively. We may use it to avoid performing
286 // heavy transformations on SCEVs of excessive size for sake of saving the
287 // compilation time.
288 unsigned short getExpressionSize() const {
289 return ExpressionSize;
290 }
291
292 /// Print out the internal representation of this scalar to the specified
293 /// stream. This should really only be used for debugging purposes.
294 LLVM_ABI void print(raw_ostream &OS) const;
295
296 /// This method is used for debugging.
297 LLVM_ABI void dump() const;
298
299 /// Compute and set the canonical SCEV, by constructing a SCEV with the same
300 /// operands, but all SCEVUse flags dropped.
302
303 /// Return the canonical SCEV.
304 LLVM_ABI const SCEV *getCanonical() const {
305 assert(CanonicalSCEV && "canonical SCEV not yet computed");
306 return CanonicalSCEV;
307 }
308};
309
310// Specialize FoldingSetTrait for SCEV to avoid needing to compute
311// temporary FoldingSetNodeID values.
312template <> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> {
313 static void Profile(const SCEV &X, FoldingSetNodeID &ID) { ID = X.FastID; }
314
315 static bool Equals(const SCEV &X, const FoldingSetNodeID &ID, unsigned IDHash,
316 FoldingSetNodeID &TempID) {
317 return ID == X.FastID;
318 }
319
320 static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) {
321 return X.FastID.ComputeHash();
322 }
323};
324
325inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
326 S.print(OS);
327 return OS;
328}
329
331 U.print(OS);
332 return OS;
333}
334
335/// An object of this class is returned by queries that could not be answered.
336/// For example, if you ask for the number of iterations of a linked-list
337/// traversal loop, you will get one of these. None of the standard SCEV
338/// operations are valid on this class, it is just a marker.
339struct SCEVCouldNotCompute : public SCEV {
341
342 /// Methods for support type inquiry through isa, cast, and dyn_cast:
343 LLVM_ABI static bool classof(const SCEV *S);
344};
345
346/// This class represents an assumption made using SCEV expressions which can
347/// be checked at run-time.
349 friend struct FoldingSetTrait<SCEVPredicate>;
350
351 /// A reference to an Interned FoldingSetNodeID for this node. The
352 /// ScalarEvolution's BumpPtrAllocator holds the data.
353 FoldingSetNodeIDRef FastID;
354
355public:
357
358protected:
360 ~SCEVPredicate() = default;
361 SCEVPredicate(const SCEVPredicate &) = default;
363
364public:
366
367 SCEVPredicateKind getKind() const { return Kind; }
368
369 /// Returns the estimated complexity of this predicate. This is roughly
370 /// measured in the number of run-time checks required.
371 virtual unsigned getComplexity() const { return 1; }
372
373 /// Returns true if the predicate is always true. This means that no
374 /// assumptions were made and nothing needs to be checked at run-time.
375 virtual bool isAlwaysTrue() const = 0;
376
377 /// Returns true if this predicate implies \p N.
378 virtual bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const = 0;
379
380 /// Prints a textual representation of this predicate with an indentation of
381 /// \p Depth.
382 virtual void print(raw_ostream &OS, unsigned Depth = 0) const = 0;
383};
384
386 P.print(OS);
387 return OS;
388}
389
390// Specialize FoldingSetTrait for SCEVPredicate to avoid needing to compute
391// temporary FoldingSetNodeID values.
392template <>
394 static void Profile(const SCEVPredicate &X, FoldingSetNodeID &ID) {
395 ID = X.FastID;
396 }
397
398 static bool Equals(const SCEVPredicate &X, const FoldingSetNodeID &ID,
399 unsigned IDHash, FoldingSetNodeID &TempID) {
400 return ID == X.FastID;
401 }
402
403 static unsigned ComputeHash(const SCEVPredicate &X,
404 FoldingSetNodeID &TempID) {
405 return X.FastID.ComputeHash();
406 }
407};
408
409/// This class represents an assumption that the expression LHS Pred RHS
410/// evaluates to true, and this can be checked at run-time.
412 /// We assume that LHS Pred RHS is true.
413 const ICmpInst::Predicate Pred;
414 const SCEV *LHS;
415 const SCEV *RHS;
416
417public:
419 const ICmpInst::Predicate Pred,
420 const SCEV *LHS, const SCEV *RHS);
421
422 /// Implementation of the SCEVPredicate interface
423 bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override;
424 void print(raw_ostream &OS, unsigned Depth = 0) const override;
425 bool isAlwaysTrue() const override;
426
427 ICmpInst::Predicate getPredicate() const { return Pred; }
428
429 /// Returns the left hand side of the predicate.
430 const SCEV *getLHS() const { return LHS; }
431
432 /// Returns the right hand side of the predicate.
433 const SCEV *getRHS() const { return RHS; }
434
435 /// Methods for support type inquiry through isa, cast, and dyn_cast:
436 static bool classof(const SCEVPredicate *P) {
437 return P->getKind() == P_Compare;
438 }
439};
440
441/// This class represents an assumption made on an AddRec expression. Given an
442/// affine AddRec expression {a,+,b}, we assume that it has the nssw or nusw
443/// flags (defined below) in the first X iterations of the loop, where X is a
444/// SCEV expression returned by getPredicatedBackedgeTakenCount).
445///
446/// Note that this does not imply that X is equal to the backedge taken
447/// count. This means that if we have a nusw predicate for i32 {0,+,1} with a
448/// predicated backedge taken count of X, we only guarantee that {0,+,1} has
449/// nusw in the first X iterations. {0,+,1} may still wrap in the loop if we
450/// have more than X iterations.
452public:
453 /// Similar to SCEV::NoWrapFlags, but with slightly different semantics
454 /// for FlagNUSW. The increment is considered to be signed, and a + b
455 /// (where b is the increment) is considered to wrap if:
456 /// zext(a + b) != zext(a) + sext(b)
457 ///
458 /// If Signed is a function that takes an n-bit tuple and maps to the
459 /// integer domain as the tuples value interpreted as twos complement,
460 /// and Unsigned a function that takes an n-bit tuple and maps to the
461 /// integer domain as the base two value of input tuple, then a + b
462 /// has IncrementNUSW iff:
463 ///
464 /// 0 <= Unsigned(a) + Signed(b) < 2^n
465 ///
466 /// The IncrementNSSW flag has identical semantics with SCEV::FlagNSW.
467 ///
468 /// Note that the IncrementNUSW flag is not commutative: if base + inc
469 /// has IncrementNUSW, then inc + base doesn't neccessarily have this
470 /// property. The reason for this is that this is used for sign/zero
471 /// extending affine AddRec SCEV expressions when a SCEVWrapPredicate is
472 /// assumed. A {base,+,inc} expression is already non-commutative with
473 /// regards to base and inc, since it is interpreted as:
474 /// (((base + inc) + inc) + inc) ...
476 IncrementAnyWrap = 0, // No guarantee.
477 IncrementNUSW = (1 << 0), // No unsigned with signed increment wrap.
478 IncrementNSSW = (1 << 1), // No signed with signed increment wrap
479 // (equivalent with SCEV::NSW)
480 IncrementNoWrapMask = (1 << 2) - 1
481 };
482
483 /// Convenient IncrementWrapFlags manipulation methods.
484 [[nodiscard]] static SCEVWrapPredicate::IncrementWrapFlags
487 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
488 assert((OffFlags & IncrementNoWrapMask) == OffFlags &&
489 "Invalid flags value!");
490 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & ~OffFlags);
491 }
492
493 [[nodiscard]] static SCEVWrapPredicate::IncrementWrapFlags
495 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
496 assert((Mask & IncrementNoWrapMask) == Mask && "Invalid mask value!");
497
498 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & Mask);
499 }
500
501 [[nodiscard]] static SCEVWrapPredicate::IncrementWrapFlags
504 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
505 assert((OnFlags & IncrementNoWrapMask) == OnFlags &&
506 "Invalid flags value!");
507
508 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags | OnFlags);
509 }
510
511 /// Returns the set of SCEVWrapPredicate no wrap flags implied by a
512 /// SCEVAddRecExpr.
513 [[nodiscard]] static SCEVWrapPredicate::IncrementWrapFlags
514 getImpliedFlags(const SCEVAddRecExpr *AR, ScalarEvolution &SE);
515
516private:
517 const SCEVAddRecExpr *AR;
518 IncrementWrapFlags Flags;
519
520public:
522 const SCEVAddRecExpr *AR,
523 IncrementWrapFlags Flags);
524
525 /// Returns the set assumed no overflow flags.
526 IncrementWrapFlags getFlags() const { return Flags; }
527
528 /// Implementation of the SCEVPredicate interface
529 const SCEVAddRecExpr *getExpr() const;
530 bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override;
531 void print(raw_ostream &OS, unsigned Depth = 0) const override;
532 bool isAlwaysTrue() const override;
533
534 /// Methods for support type inquiry through isa, cast, and dyn_cast:
535 static bool classof(const SCEVPredicate *P) {
536 return P->getKind() == P_Wrap;
537 }
538};
539
540/// This class represents a composition of other SCEV predicates, and is the
541/// class that most clients will interact with. This is equivalent to a
542/// logical "AND" of all the predicates in the union.
543///
544/// NB! Unlike other SCEVPredicate sub-classes this class does not live in the
545/// ScalarEvolution::Preds folding set. This is why the \c add function is sound.
547private:
548 using PredicateMap =
550
551 /// Vector with references to all predicates in this union.
553
554 /// Adds a predicate to this union.
555 void add(const SCEVPredicate *N, ScalarEvolution &SE);
556
557public:
559 ScalarEvolution &SE);
560
562
563 /// Returns a new SCEVUnionPredicate that is the union of this predicate
564 /// and the given predicate \p N.
566 ScalarEvolution &SE) const {
567 SCEVUnionPredicate Result(Preds, SE);
568 Result.add(N, SE);
569 return Result;
570 }
571
572 /// Implementation of the SCEVPredicate interface
573 bool isAlwaysTrue() const override;
574 bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override;
575 void print(raw_ostream &OS, unsigned Depth) const override;
576
577 /// We estimate the complexity of a union predicate as the size number of
578 /// predicates in the union.
579 unsigned getComplexity() const override { return Preds.size(); }
580
581 /// Methods for support type inquiry through isa, cast, and dyn_cast:
582 static bool classof(const SCEVPredicate *P) {
583 return P->getKind() == P_Union;
584 }
585};
586
587/// The main scalar evolution driver. Because client code (intentionally)
588/// can't do much with the SCEV objects directly, they must ask this class
589/// for services.
592
593public:
594 /// An enum describing the relationship between a SCEV and a loop.
596 LoopVariant, ///< The SCEV is loop-variant (unknown).
597 LoopInvariant, ///< The SCEV is loop-invariant.
598 LoopComputable ///< The SCEV varies predictably with the loop.
599 };
600
601 /// An enum describing the relationship between a SCEV and a basic block.
603 DoesNotDominateBlock, ///< The SCEV does not dominate the block.
604 DominatesBlock, ///< The SCEV dominates the block.
605 ProperlyDominatesBlock ///< The SCEV properly dominates the block.
606 };
607
608 /// Convenient NoWrapFlags manipulation that hides enum casts and is
609 /// visible in the ScalarEvolution name space.
611 int Mask) {
612 return (SCEV::NoWrapFlags)(Flags & Mask);
613 }
614 [[nodiscard]] static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags,
615 SCEV::NoWrapFlags OnFlags) {
616 return (SCEV::NoWrapFlags)(Flags | OnFlags);
617 }
618 [[nodiscard]] static SCEV::NoWrapFlags
620 return (SCEV::NoWrapFlags)(Flags & ~OffFlags);
621 }
622 [[nodiscard]] static bool hasFlags(SCEV::NoWrapFlags Flags,
623 SCEV::NoWrapFlags TestFlags) {
624 return TestFlags == maskFlags(Flags, TestFlags);
625 };
626
629 LoopInfo &LI);
632
633 LLVMContext &getContext() const { return F.getContext(); }
634
635 /// Test if values of the given type are analyzable within the SCEV
636 /// framework. This primarily includes integer types, and it can optionally
637 /// include pointer types if the ScalarEvolution class has access to
638 /// target-specific information.
639 LLVM_ABI bool isSCEVable(Type *Ty) const;
640
641 /// Return the size in bits of the specified type, for which isSCEVable must
642 /// return true.
644
645 /// Return a type with the same bitwidth as the given type and which
646 /// represents how SCEV will treat the given type, for which isSCEVable must
647 /// return true. For pointer types, this is the pointer-sized integer type.
649
650 // Returns a wider type among {Ty1, Ty2}.
651 LLVM_ABI Type *getWiderType(Type *Ty1, Type *Ty2) const;
652
653 /// Return true if there exists a point in the program at which both
654 /// A and B could be operands to the same instruction.
655 /// SCEV expressions are generally assumed to correspond to instructions
656 /// which could exists in IR. In general, this requires that there exists
657 /// a use point in the program where all operands dominate the use.
658 ///
659 /// Example:
660 /// loop {
661 /// if
662 /// loop { v1 = load @global1; }
663 /// else
664 /// loop { v2 = load @global2; }
665 /// }
666 /// No SCEV with operand V1, and v2 can exist in this program.
668
669 /// Return true if the SCEV is a scAddRecExpr or it contains
670 /// scAddRecExpr. The result will be cached in HasRecMap.
671 LLVM_ABI bool containsAddRecurrence(const SCEV *S);
672
673 /// Is operation \p BinOp between \p LHS and \p RHS provably does not have
674 /// a signed/unsigned overflow (\p Signed)? If \p CtxI is specified, the
675 /// no-overflow fact should be true in the context of this instruction.
677 const SCEV *LHS, const SCEV *RHS,
678 const Instruction *CtxI = nullptr);
679
680 /// Parse NSW/NUW flags from add/sub/mul IR binary operation \p Op into
681 /// SCEV no-wrap flags, and deduce flag[s] that aren't known yet.
682 /// Does not mutate the original instruction. Returns std::nullopt if it could
683 /// not deduce more precise flags than the instruction already has, otherwise
684 /// returns proven flags.
685 LLVM_ABI std::optional<SCEV::NoWrapFlags>
687
688 /// Notify this ScalarEvolution that \p User directly uses SCEVs in \p Ops.
691
692 /// Return true if the SCEV expression contains an undef value.
693 LLVM_ABI bool containsUndefs(const SCEV *S) const;
694
695 /// Return true if the SCEV expression contains a Value that has been
696 /// optimised out and is now a nullptr.
697 LLVM_ABI bool containsErasedValue(const SCEV *S) const;
698
699 /// Return a SCEV expression for the full generality of the specified
700 /// expression.
701 LLVM_ABI const SCEV *getSCEV(Value *V);
702
703 /// Return an existing SCEV for V if there is one, otherwise return nullptr.
705
707 LLVM_ABI const SCEV *getConstant(const APInt &Val);
708 LLVM_ABI const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false);
710
711 LLVM_ABI const SCEV *getPtrToAddrExpr(const SCEV *Op);
712 LLVM_ABI const SCEV *getPtrToIntExpr(const SCEV *Op, Type *Ty);
713 LLVM_ABI const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty,
714 unsigned Depth = 0);
715 LLVM_ABI const SCEV *getVScale(Type *Ty);
716 LLVM_ABI const SCEV *
719 LLVM_ABI const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty,
720 unsigned Depth = 0);
721 LLVM_ABI const SCEV *getZeroExtendExprImpl(const SCEV *Op, Type *Ty,
722 unsigned Depth = 0);
723 LLVM_ABI const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty,
724 unsigned Depth = 0);
725 LLVM_ABI const SCEV *getSignExtendExprImpl(const SCEV *Op, Type *Ty,
726 unsigned Depth = 0);
727 LLVM_ABI const SCEV *getCastExpr(SCEVTypes Kind, const SCEV *Op, Type *Ty);
728 LLVM_ABI const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty);
729
732 unsigned Depth = 0);
735 unsigned Depth = 0) {
737 return getAddExpr(Ops, Flags, Depth);
738 }
739 const SCEV *getAddExpr(SCEVUse Op0, SCEVUse Op1, SCEVUse Op2,
741 unsigned Depth = 0) {
742 SmallVector<SCEVUse, 3> Ops = {Op0, Op1, Op2};
743 return getAddExpr(Ops, Flags, Depth);
744 }
747 unsigned Depth = 0);
750 unsigned Depth = 0) {
752 return getMulExpr(Ops, Flags, Depth);
753 }
754 const SCEV *getMulExpr(SCEVUse Op0, SCEVUse Op1, SCEVUse Op2,
756 unsigned Depth = 0) {
757 SmallVector<SCEVUse, 3> Ops = {Op0, Op1, Op2};
758 return getMulExpr(Ops, Flags, Depth);
759 }
763 LLVM_ABI const SCEV *getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L,
764 SCEV::NoWrapFlags Flags);
766 const Loop *L, SCEV::NoWrapFlags Flags);
768 const Loop *L, SCEV::NoWrapFlags Flags) {
769 SmallVector<SCEVUse, 4> NewOp(Operands.begin(), Operands.end());
770 return getAddRecExpr(NewOp, L, Flags);
771 }
772
773 /// Checks if \p SymbolicPHI can be rewritten as an AddRecExpr under some
774 /// Predicates. If successful return these <AddRecExpr, Predicates>;
775 /// The function is intended to be called from PSCEV (the caller will decide
776 /// whether to actually add the predicates and carry out the rewrites).
777 LLVM_ABI std::optional<
778 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
779 createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI);
780
781 /// Returns an expression for a GEP
782 ///
783 /// \p GEP The GEP. The indices contained in the GEP itself are ignored,
784 /// instead we use IndexExprs.
785 /// \p IndexExprs The expressions for the indices.
787 ArrayRef<SCEVUse> IndexExprs);
788 LLVM_ABI const SCEV *getGEPExpr(SCEVUse BaseExpr,
789 ArrayRef<SCEVUse> IndexExprs,
790 Type *SrcElementTy,
792 LLVM_ABI const SCEV *getAbsExpr(const SCEV *Op, bool IsNSW);
794 SmallVectorImpl<SCEVUse> &Operands);
795 LLVM_ABI const SCEV *
804 bool Sequential = false);
806 bool Sequential = false);
807 LLVM_ABI const SCEV *getUnknown(Value *V);
809
810 /// Return a SCEV for the constant 0 of a specific type.
811 const SCEV *getZero(Type *Ty) { return getConstant(Ty, 0); }
812
813 /// Return a SCEV for the constant 1 of a specific type.
814 const SCEV *getOne(Type *Ty) { return getConstant(Ty, 1); }
815
816 /// Return a SCEV for the constant \p Power of two.
817 const SCEV *getPowerOfTwo(Type *Ty, unsigned Power) {
818 assert(Power < getTypeSizeInBits(Ty) && "Power out of range");
820 }
821
822 /// Return a SCEV for the constant -1 of a specific type.
823 const SCEV *getMinusOne(Type *Ty) {
824 return getConstant(Ty, -1, /*isSigned=*/true);
825 }
826
827 /// Return an expression for a TypeSize.
829
830 /// Return an expression for the alloc size of AllocTy that is type IntTy
831 LLVM_ABI const SCEV *getSizeOfExpr(Type *IntTy, Type *AllocTy);
832
833 /// Return an expression for the store size of StoreTy that is type IntTy
834 LLVM_ABI const SCEV *getStoreSizeOfExpr(Type *IntTy, Type *StoreTy);
835
836 /// Return an expression for offsetof on the given field with type IntTy
837 LLVM_ABI const SCEV *getOffsetOfExpr(Type *IntTy, StructType *STy,
838 unsigned FieldNo);
839
840 /// Return the SCEV object corresponding to -V.
841 LLVM_ABI const SCEV *
843
844 /// Return the SCEV object corresponding to ~V.
845 LLVM_ABI const SCEV *getNotSCEV(const SCEV *V);
846
847 /// Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
848 ///
849 /// If the LHS and RHS are pointers which don't share a common base
850 /// (according to getPointerBase()), this returns a SCEVCouldNotCompute.
851 /// To compute the difference between two unrelated pointers, you can
852 /// explicitly convert the arguments using getPtrToIntExpr(), for pointer
853 /// types that support it.
856 unsigned Depth = 0);
857
858 /// Compute ceil(N / D). N and D are treated as unsigned values.
859 ///
860 /// Since SCEV doesn't have native ceiling division, this generates a
861 /// SCEV expression of the following form:
862 ///
863 /// umin(N, 1) + floor((N - umin(N, 1)) / D)
864 ///
865 /// A denominator of zero or poison is handled the same way as getUDivExpr().
866 LLVM_ABI const SCEV *getUDivCeilSCEV(const SCEV *N, const SCEV *D);
867
868 /// Return a SCEV corresponding to a conversion of the input value to the
869 /// specified type. If the type must be extended, it is zero extended.
870 LLVM_ABI const SCEV *getTruncateOrZeroExtend(const SCEV *V, Type *Ty,
871 unsigned Depth = 0);
872
873 /// Return a SCEV corresponding to a conversion of the input value to the
874 /// specified type. If the type must be extended, it is sign extended.
875 LLVM_ABI const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty,
876 unsigned Depth = 0);
877
878 /// Return a SCEV corresponding to a conversion of the input value to the
879 /// specified type. If the type must be extended, it is zero extended. The
880 /// conversion must not be narrowing.
881 LLVM_ABI const SCEV *getNoopOrZeroExtend(const SCEV *V, Type *Ty);
882
883 /// Return a SCEV corresponding to a conversion of the input value to the
884 /// specified type. If the type must be extended, it is sign extended. The
885 /// conversion must not be narrowing.
886 LLVM_ABI const SCEV *getNoopOrSignExtend(const SCEV *V, Type *Ty);
887
888 /// Return a SCEV corresponding to a conversion of the input value to the
889 /// specified type. If the type must be extended, it is extended with
890 /// unspecified bits. The conversion must not be narrowing.
891 LLVM_ABI const SCEV *getNoopOrAnyExtend(const SCEV *V, Type *Ty);
892
893 /// Return a SCEV corresponding to a conversion of the input value to the
894 /// specified type. The conversion must not be widening.
895 LLVM_ABI const SCEV *getTruncateOrNoop(const SCEV *V, Type *Ty);
896
897 /// Promote the operands to the wider of the types using zero-extension, and
898 /// then perform a umax operation with them.
900 const SCEV *RHS);
901
902 /// Promote the operands to the wider of the types using zero-extension, and
903 /// then perform a umin operation with them.
905 const SCEV *RHS,
906 bool Sequential = false);
907
908 /// Promote the operands to the wider of the types using zero-extension, and
909 /// then perform a umin operation with them. N-ary function.
911 bool Sequential = false);
912
913 /// Transitively follow the chain of pointer-type operands until reaching a
914 /// SCEV that does not have a single pointer operand. This returns a
915 /// SCEVUnknown pointer for well-formed pointer-type expressions, but corner
916 /// cases do exist.
917 LLVM_ABI const SCEV *getPointerBase(const SCEV *V);
918
919 /// Compute an expression equivalent to S - getPointerBase(S).
920 LLVM_ABI const SCEV *removePointerBase(const SCEV *S);
921
922 /// Return a SCEV expression for the specified value at the specified scope
923 /// in the program. The L value specifies a loop nest to evaluate the
924 /// expression at, where null is the top-level or a specified loop is
925 /// immediately inside of the loop.
926 ///
927 /// This method can be used to compute the exit value for a variable defined
928 /// in a loop by querying what the value will hold in the parent loop.
929 ///
930 /// In the case that a relevant loop exit value cannot be computed, the
931 /// original value V is returned.
932 LLVM_ABI const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
933
934 /// This is a convenience function which does getSCEVAtScope(getSCEV(V), L).
935 LLVM_ABI const SCEV *getSCEVAtScope(Value *V, const Loop *L);
936
937 /// Test whether entry to the loop is protected by a conditional between LHS
938 /// and RHS. This is used to help avoid max expressions in loop trip
939 /// counts, and to eliminate casts.
941 const SCEV *LHS, const SCEV *RHS);
942
943 /// Test whether entry to the basic block is protected by a conditional
944 /// between LHS and RHS.
946 CmpPredicate Pred,
947 const SCEV *LHS,
948 const SCEV *RHS);
949
950 /// Test whether the backedge of the loop is protected by a conditional
951 /// between LHS and RHS. This is used to eliminate casts.
953 const SCEV *LHS, const SCEV *RHS);
954
955 /// A version of getTripCountFromExitCount below which always picks an
956 /// evaluation type which can not result in overflow.
957 LLVM_ABI const SCEV *getTripCountFromExitCount(const SCEV *ExitCount);
958
959 /// Convert from an "exit count" (i.e. "backedge taken count") to a "trip
960 /// count". A "trip count" is the number of times the header of the loop
961 /// will execute if an exit is taken after the specified number of backedges
962 /// have been taken. (e.g. TripCount = ExitCount + 1). Note that the
963 /// expression can overflow if ExitCount = UINT_MAX. If EvalTy is not wide
964 /// enough to hold the result without overflow, result unsigned wraps with
965 /// 2s-complement semantics. ex: EC = 255 (i8), TC = 0 (i8)
966 LLVM_ABI const SCEV *getTripCountFromExitCount(const SCEV *ExitCount,
967 Type *EvalTy, const Loop *L);
968
969 /// Returns the exact trip count of the loop if we can compute it, and
970 /// the result is a small constant. '0' is used to represent an unknown
971 /// or non-constant trip count. Note that a trip count is simply one more
972 /// than the backedge taken count for the loop.
973 LLVM_ABI unsigned getSmallConstantTripCount(const Loop *L);
974
975 /// Return the exact trip count for this loop if we exit through ExitingBlock.
976 /// '0' is used to represent an unknown or non-constant trip count. Note
977 /// that a trip count is simply one more than the backedge taken count for
978 /// the same exit.
979 /// This "trip count" assumes that control exits via ExitingBlock. More
980 /// precisely, it is the number of times that control will reach ExitingBlock
981 /// before taking the branch. For loops with multiple exits, it may not be
982 /// the number times that the loop header executes if the loop exits
983 /// prematurely via another branch.
984 LLVM_ABI unsigned getSmallConstantTripCount(const Loop *L,
985 const BasicBlock *ExitingBlock);
986
987 /// Returns the upper bound of the loop trip count as a normal unsigned
988 /// value.
989 /// Returns 0 if the trip count is unknown, not constant or requires
990 /// SCEV predicates and \p Predicates is nullptr.
992 const Loop *L,
993 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
994
995 /// Returns the largest constant divisor of the trip count as a normal
996 /// unsigned value, if possible. This means that the actual trip count is
997 /// always a multiple of the returned value. Returns 1 if the trip count is
998 /// unknown or not guaranteed to be the multiple of a constant., Will also
999 /// return 1 if the trip count is very large (>= 2^32).
1000 /// Note that the argument is an exit count for loop L, NOT a trip count.
1001 LLVM_ABI unsigned getSmallConstantTripMultiple(const Loop *L,
1002 const SCEV *ExitCount);
1003
1004 /// Returns the largest constant divisor of the trip count of the
1005 /// loop. Will return 1 if no trip count could be computed, or if a
1006 /// divisor could not be found.
1007 LLVM_ABI unsigned getSmallConstantTripMultiple(const Loop *L);
1008
1009 /// Returns the largest constant divisor of the trip count of this loop as a
1010 /// normal unsigned value, if possible. This means that the actual trip
1011 /// count is always a multiple of the returned value (don't forget the trip
1012 /// count could very well be zero as well!). As explained in the comments
1013 /// for getSmallConstantTripCount, this assumes that control exits the loop
1014 /// via ExitingBlock.
1015 LLVM_ABI unsigned
1016 getSmallConstantTripMultiple(const Loop *L, const BasicBlock *ExitingBlock);
1017
1018 /// The terms "backedge taken count" and "exit count" are used
1019 /// interchangeably to refer to the number of times the backedge of a loop
1020 /// has executed before the loop is exited.
1022 /// An expression exactly describing the number of times the backedge has
1023 /// executed when a loop is exited.
1025 /// A constant which provides an upper bound on the exact trip count.
1027 /// An expression which provides an upper bound on the exact trip count.
1029 };
1030
1031 /// Return the number of times the backedge executes before the given exit
1032 /// would be taken; if not exactly computable, return SCEVCouldNotCompute.
1033 /// For a single exit loop, this value is equivelent to the result of
1034 /// getBackedgeTakenCount. The loop is guaranteed to exit (via *some* exit)
1035 /// before the backedge is executed (ExitCount + 1) times. Note that there
1036 /// is no guarantee about *which* exit is taken on the exiting iteration.
1037 LLVM_ABI const SCEV *getExitCount(const Loop *L,
1038 const BasicBlock *ExitingBlock,
1039 ExitCountKind Kind = Exact);
1040
1041 /// Same as above except this uses the predicated backedge taken info and
1042 /// may require predicates.
1043 LLVM_ABI const SCEV *
1044 getPredicatedExitCount(const Loop *L, const BasicBlock *ExitingBlock,
1046 ExitCountKind Kind = Exact);
1047
1048 /// If the specified loop has a predictable backedge-taken count, return it,
1049 /// otherwise return a SCEVCouldNotCompute object. The backedge-taken count is
1050 /// the number of times the loop header will be branched to from within the
1051 /// loop, assuming there are no abnormal exists like exception throws. This is
1052 /// one less than the trip count of the loop, since it doesn't count the first
1053 /// iteration, when the header is branched to from outside the loop.
1054 ///
1055 /// Note that it is not valid to call this method on a loop without a
1056 /// loop-invariant backedge-taken count (see
1057 /// hasLoopInvariantBackedgeTakenCount).
1058 LLVM_ABI const SCEV *getBackedgeTakenCount(const Loop *L,
1059 ExitCountKind Kind = Exact);
1060
1061 /// Similar to getBackedgeTakenCount, except it will add a set of
1062 /// SCEV predicates to Predicates that are required to be true in order for
1063 /// the answer to be correct. Predicates can be checked with run-time
1064 /// checks and can be used to perform loop versioning.
1066 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Predicates);
1067
1068 /// When successful, this returns a SCEVConstant that is greater than or equal
1069 /// to (i.e. a "conservative over-approximation") of the value returend by
1070 /// getBackedgeTakenCount. If such a value cannot be computed, it returns the
1071 /// SCEVCouldNotCompute object.
1075
1076 /// Similar to getConstantMaxBackedgeTakenCount, except it will add a set of
1077 /// SCEV predicates to Predicates that are required to be true in order for
1078 /// the answer to be correct. Predicates can be checked with run-time
1079 /// checks and can be used to perform loop versioning.
1081 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Predicates);
1082
1083 /// When successful, this returns a SCEV that is greater than or equal
1084 /// to (i.e. a "conservative over-approximation") of the value returend by
1085 /// getBackedgeTakenCount. If such a value cannot be computed, it returns the
1086 /// SCEVCouldNotCompute object.
1090
1091 /// Similar to getSymbolicMaxBackedgeTakenCount, except it will add a set of
1092 /// SCEV predicates to Predicates that are required to be true in order for
1093 /// the answer to be correct. Predicates can be checked with run-time
1094 /// checks and can be used to perform loop versioning.
1096 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Predicates);
1097
1098 /// Return true if the backedge taken count is either the value returned by
1099 /// getConstantMaxBackedgeTakenCount or zero.
1101
1102 /// Return true if the specified loop has an analyzable loop-invariant
1103 /// backedge-taken count.
1105
1106 // This method should be called by the client when it made any change that
1107 // would invalidate SCEV's answers, and the client wants to remove all loop
1108 // information held internally by ScalarEvolution. This is intended to be used
1109 // when the alternative to forget a loop is too expensive (i.e. large loop
1110 // bodies).
1111 LLVM_ABI void forgetAllLoops();
1112
1113 /// This method should be called by the client when it has changed a loop in
1114 /// a way that may effect ScalarEvolution's ability to compute a trip count,
1115 /// or if the loop is deleted. This call is potentially expensive for large
1116 /// loop bodies.
1117 LLVM_ABI void forgetLoop(const Loop *L);
1118
1119 // This method invokes forgetLoop for the outermost loop of the given loop
1120 // \p L, making ScalarEvolution forget about all this subtree. This needs to
1121 // be done whenever we make a transform that may affect the parameters of the
1122 // outer loop, such as exit counts for branches.
1123 LLVM_ABI void forgetTopmostLoop(const Loop *L);
1124
1125 /// This method should be called by the client when it has changed a value
1126 /// in a way that may effect its value, or which may disconnect it from a
1127 /// def-use chain linking it to a loop.
1128 LLVM_ABI void forgetValue(Value *V);
1129
1130 /// Forget LCSSA phi node V of loop L to which a new predecessor was added,
1131 /// such that it may no longer be trivial.
1133
1134 /// Called when the client has changed the disposition of values in
1135 /// this loop.
1136 ///
1137 /// We don't have a way to invalidate per-loop dispositions. Clear and
1138 /// recompute is simpler.
1140
1141 /// Called when the client has changed the disposition of values in
1142 /// a loop or block.
1143 ///
1144 /// We don't have a way to invalidate per-loop/per-block dispositions. Clear
1145 /// and recompute is simpler.
1147
1148 /// Determine the minimum number of zero bits that S is guaranteed to end in
1149 /// (at every loop iteration). It is, at the same time, the minimum number
1150 /// of times S is divisible by 2. For example, given {4,+,8} it returns 2.
1151 /// If S is guaranteed to be 0, it returns the bitwidth of S.
1152 /// If \p CtxI is not nullptr, return a constant multiple valid at \p CtxI.
1154 const Instruction *CtxI = nullptr);
1155
1156 /// Returns the max constant multiple of S. If \p CtxI is not nullptr, return
1157 /// a constant multiple valid at \p CtxI.
1159 const Instruction *CtxI = nullptr);
1160
1161 // Returns the max constant multiple of S. If S is exactly 0, return 1.
1163
1164 /// Determine the unsigned range for a particular SCEV.
1165 /// NOTE: This returns a copy of the reference returned by getRangeRef.
1167 return getRangeRef(S, HINT_RANGE_UNSIGNED);
1168 }
1169
1170 /// Determine the min of the unsigned range for a particular SCEV.
1172 return getRangeRef(S, HINT_RANGE_UNSIGNED).getUnsignedMin();
1173 }
1174
1175 /// Determine the max of the unsigned range for a particular SCEV.
1177 return getRangeRef(S, HINT_RANGE_UNSIGNED).getUnsignedMax();
1178 }
1179
1180 /// Determine the signed range for a particular SCEV.
1181 /// NOTE: This returns a copy of the reference returned by getRangeRef.
1183 return getRangeRef(S, HINT_RANGE_SIGNED);
1184 }
1185
1186 /// Determine the min of the signed range for a particular SCEV.
1188 return getRangeRef(S, HINT_RANGE_SIGNED).getSignedMin();
1189 }
1190
1191 /// Determine the max of the signed range for a particular SCEV.
1193 return getRangeRef(S, HINT_RANGE_SIGNED).getSignedMax();
1194 }
1195
1196 /// Test if the given expression is known to be negative.
1197 LLVM_ABI bool isKnownNegative(const SCEV *S);
1198
1199 /// Test if the given expression is known to be positive.
1200 LLVM_ABI bool isKnownPositive(const SCEV *S);
1201
1202 /// Test if the given expression is known to be non-negative.
1203 LLVM_ABI bool isKnownNonNegative(const SCEV *S);
1204
1205 /// Test if the given expression is known to be non-positive.
1206 LLVM_ABI bool isKnownNonPositive(const SCEV *S);
1207
1208 /// Test if the given expression is known to be non-zero.
1209 LLVM_ABI bool isKnownNonZero(const SCEV *S);
1210
1211 /// Test if the given expression is known to be a power of 2. OrNegative
1212 /// allows matching negative power of 2s, and OrZero allows matching 0.
1213 LLVM_ABI bool isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero = false,
1214 bool OrNegative = false);
1215
1216 /// Check that \p S is a multiple of \p M. When \p S is an AddRecExpr, \p S is
1217 /// a multiple of \p M if \p S starts with a multiple of \p M and at every
1218 /// iteration step \p S only adds multiples of \p M. \p Assumptions records
1219 /// the runtime predicates under which \p S is a multiple of \p M.
1220 LLVM_ABI bool
1221 isKnownMultipleOf(const SCEV *S, uint64_t M,
1223
1224 /// Return true if we know that S1 and S2 must have the same sign.
1225 LLVM_ABI bool haveSameSign(const SCEV *S1, const SCEV *S2);
1226
1227 /// Splits SCEV expression \p S into two SCEVs. One of them is obtained from
1228 /// \p S by substitution of all AddRec sub-expression related to loop \p L
1229 /// with initial value of that SCEV. The second is obtained from \p S by
1230 /// substitution of all AddRec sub-expressions related to loop \p L with post
1231 /// increment of this AddRec in the loop \p L. In both cases all other AddRec
1232 /// sub-expressions (not related to \p L) remain the same.
1233 /// If the \p S contains non-invariant unknown SCEV the function returns
1234 /// CouldNotCompute SCEV in both values of std::pair.
1235 /// For example, for SCEV S={0, +, 1}<L1> + {0, +, 1}<L2> and loop L=L1
1236 /// the function returns pair:
1237 /// first = {0, +, 1}<L2>
1238 /// second = {1, +, 1}<L1> + {0, +, 1}<L2>
1239 /// We can see that for the first AddRec sub-expression it was replaced with
1240 /// 0 (initial value) for the first element and to {1, +, 1}<L1> (post
1241 /// increment value) for the second one. In both cases AddRec expression
1242 /// related to L2 remains the same.
1243 LLVM_ABI std::pair<const SCEV *, const SCEV *>
1244 SplitIntoInitAndPostInc(const Loop *L, const SCEV *S);
1245
1246 /// We'd like to check the predicate on every iteration of the most dominated
1247 /// loop between loops used in LHS and RHS.
1248 /// To do this we use the following list of steps:
1249 /// 1. Collect set S all loops on which either LHS or RHS depend.
1250 /// 2. If S is non-empty
1251 /// a. Let PD be the element of S which is dominated by all other elements.
1252 /// b. Let E(LHS) be value of LHS on entry of PD.
1253 /// To get E(LHS), we should just take LHS and replace all AddRecs that are
1254 /// attached to PD on with their entry values.
1255 /// Define E(RHS) in the same way.
1256 /// c. Let B(LHS) be value of L on backedge of PD.
1257 /// To get B(LHS), we should just take LHS and replace all AddRecs that are
1258 /// attached to PD on with their backedge values.
1259 /// Define B(RHS) in the same way.
1260 /// d. Note that E(LHS) and E(RHS) are automatically available on entry of PD,
1261 /// so we can assert on that.
1262 /// e. Return true if isLoopEntryGuardedByCond(Pred, E(LHS), E(RHS)) &&
1263 /// isLoopBackedgeGuardedByCond(Pred, B(LHS), B(RHS))
1265 SCEVUse RHS);
1266
1267 /// Test if the given expression is known to satisfy the condition described
1268 /// by Pred, LHS, and RHS.
1270
1271 /// Check whether the condition described by Pred, LHS, and RHS is true or
1272 /// false. If we know it, return the evaluation of this condition. If neither
1273 /// is proved, return std::nullopt.
1274 LLVM_ABI std::optional<bool>
1275 evaluatePredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS);
1276
1277 /// Test if the given expression is known to satisfy the condition described
1278 /// by Pred, LHS, and RHS in the given Context.
1280 const SCEV *RHS, const Instruction *CtxI);
1281
1282 /// Check whether the condition described by Pred, LHS, and RHS is true or
1283 /// false in the given \p Context. If we know it, return the evaluation of
1284 /// this condition. If neither is proved, return std::nullopt.
1285 LLVM_ABI std::optional<bool> evaluatePredicateAt(CmpPredicate Pred,
1286 const SCEV *LHS,
1287 const SCEV *RHS,
1288 const Instruction *CtxI);
1289
1290 /// Test if the condition described by Pred, LHS, RHS is known to be true on
1291 /// every iteration of the loop of the recurrency LHS.
1293 const SCEVAddRecExpr *LHS,
1294 const SCEV *RHS);
1295
1296 /// Information about the number of loop iterations for which a loop exit's
1297 /// branch condition evaluates to the not-taken path. This is a temporary
1298 /// pair of exact and max expressions that are eventually summarized in
1299 /// ExitNotTakenInfo and BackedgeTakenInfo.
1300 struct ExitLimit {
1301 const SCEV *ExactNotTaken; // The exit is not taken exactly this many times
1302 const SCEV *ConstantMaxNotTaken; // The exit is not taken at most this many
1303 // times
1305
1306 // Not taken either exactly ConstantMaxNotTaken or zero times
1307 bool MaxOrZero = false;
1308
1309 /// A vector of predicate guards for this ExitLimit. The result is only
1310 /// valid if all of the predicates in \c Predicates evaluate to 'true' at
1311 /// run-time.
1313
1314 /// Construct either an exact exit limit from a constant, or an unknown
1315 /// one from a SCEVCouldNotCompute. No other types of SCEVs are allowed
1316 /// as arguments and asserts enforce that internally.
1317 /*implicit*/ LLVM_ABI ExitLimit(const SCEV *E);
1318 /*implicit*/ ExitLimit(SCEVUse E) : ExitLimit((const SCEV *)E) {}
1319
1320 LLVM_ABI
1321 ExitLimit(const SCEV *E, const SCEV *ConstantMaxNotTaken,
1322 const SCEV *SymbolicMaxNotTaken, bool MaxOrZero,
1324
1326 const SCEV *SymbolicMaxNotTaken, bool MaxOrZero,
1328
1329 /// Test whether this ExitLimit contains any computed information, or
1330 /// whether it's all SCEVCouldNotCompute values.
1335
1336 /// Test whether this ExitLimit contains all information.
1337 bool hasFullInfo() const {
1339 }
1340 };
1341
1342 /// Compute the number of times the backedge of the specified loop will
1343 /// execute if its exit condition were a conditional branch of ExitCond.
1344 ///
1345 /// \p ControlsOnlyExit is true if ExitCond directly controls the only exit
1346 /// branch. In this case, we can assume that the loop exits only if the
1347 /// condition is true and can infer that failing to meet the condition prior
1348 /// to integer wraparound results in undefined behavior.
1349 ///
1350 /// If \p AllowPredicates is set, this call will try to use a minimal set of
1351 /// SCEV predicates in order to return an exact answer.
1352 LLVM_ABI ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond,
1353 bool ExitIfTrue,
1354 bool ControlsOnlyExit,
1355 bool AllowPredicates = false);
1356
1357 /// A predicate is said to be monotonically increasing if may go from being
1358 /// false to being true as the loop iterates, but never the other way
1359 /// around. A predicate is said to be monotonically decreasing if may go
1360 /// from being true to being false as the loop iterates, but never the other
1361 /// way around.
1366
1367 /// If, for all loop invariant X, the predicate "LHS `Pred` X" is
1368 /// monotonically increasing or decreasing, returns
1369 /// Some(MonotonicallyIncreasing) and Some(MonotonicallyDecreasing)
1370 /// respectively. If we could not prove either of these facts, returns
1371 /// std::nullopt.
1372 LLVM_ABI std::optional<MonotonicPredicateType>
1374 ICmpInst::Predicate Pred);
1375
1384 /// If the result of the predicate LHS `Pred` RHS is loop invariant with
1385 /// respect to L, return a LoopInvariantPredicate with LHS and RHS being
1386 /// invariants, available at L's entry. Otherwise, return std::nullopt.
1387 LLVM_ABI std::optional<LoopInvariantPredicate>
1389 const Loop *L, const Instruction *CtxI = nullptr);
1390
1391 /// If the result of the predicate LHS `Pred` RHS is loop invariant with
1392 /// respect to L at given Context during at least first MaxIter iterations,
1393 /// return a LoopInvariantPredicate with LHS and RHS being invariants,
1394 /// available at L's entry. Otherwise, return std::nullopt. The predicate
1395 /// should be the loop's exit condition.
1396 LLVM_ABI std::optional<LoopInvariantPredicate>
1398 const SCEV *LHS,
1399 const SCEV *RHS, const Loop *L,
1400 const Instruction *CtxI,
1401 const SCEV *MaxIter);
1402
1403 LLVM_ABI std::optional<LoopInvariantPredicate>
1405 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
1406 const Instruction *CtxI, const SCEV *MaxIter);
1407
1408 /// Simplify LHS and RHS in a comparison with predicate Pred. Return true
1409 /// iff any changes were made. If the operands are provably equal or
1410 /// unequal, LHS and RHS are set to the same value and Pred is set to either
1411 /// ICMP_EQ or ICMP_NE.
1413 SCEVUse &RHS, unsigned Depth = 0);
1414
1415 /// Return the "disposition" of the given SCEV with respect to the given
1416 /// loop.
1418
1419 /// Return true if the value of the given SCEV is unchanging in the
1420 /// specified loop.
1421 LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L);
1422
1423 /// Determine if the SCEV can be evaluated at loop's entry. It is true if it
1424 /// doesn't depend on a SCEVUnknown of an instruction which is dominated by
1425 /// the header of loop L.
1426 LLVM_ABI bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L);
1427
1428 /// Return true if the given SCEV changes value in a known way in the
1429 /// specified loop. This property being true implies that the value is
1430 /// variant in the loop AND that we can emit an expression to compute the
1431 /// value of the expression at any particular loop iteration.
1432 LLVM_ABI bool hasComputableLoopEvolution(const SCEV *S, const Loop *L);
1433
1434 /// Return the "disposition" of the given SCEV with respect to the given
1435 /// block.
1437 const BasicBlock *BB);
1438
1439 /// Return true if elements that makes up the given SCEV dominate the
1440 /// specified basic block.
1441 LLVM_ABI bool dominates(const SCEV *S, const BasicBlock *BB);
1442
1443 /// Return true if elements that makes up the given SCEV properly dominate
1444 /// the specified basic block.
1445 LLVM_ABI bool properlyDominates(const SCEV *S, const BasicBlock *BB);
1446
1447 /// Test whether the given SCEV has Op as a direct or indirect operand.
1448 LLVM_ABI bool hasOperand(const SCEV *S, const SCEV *Op) const;
1449
1450 /// Return the size of an element read or written by Inst.
1452
1453 LLVM_ABI void print(raw_ostream &OS) const;
1454 LLVM_ABI void verify() const;
1456 FunctionAnalysisManager::Invalidator &Inv);
1457
1458 /// Return the DataLayout associated with the module this SCEV instance is
1459 /// operating on.
1460 const DataLayout &getDataLayout() const { return DL; }
1461
1463 const SCEV *RHS);
1465 const SCEV *LHS,
1466 const SCEV *RHS);
1467
1468 LLVM_ABI const SCEVPredicate *
1471
1472 /// Re-writes the SCEV according to the Predicates in \p A.
1473 LLVM_ABI const SCEV *rewriteUsingPredicate(const SCEV *S, const Loop *L,
1474 const SCEVPredicate &A);
1475 /// Tries to convert the \p S expression to an AddRec expression,
1476 /// adding additional predicates to \p Preds as required.
1478 const SCEV *S, const Loop *L,
1480
1481 /// Compute \p LHS - \p RHS and returns the result as an APInt if it is a
1482 /// constant, and std::nullopt if it isn't.
1483 ///
1484 /// This is intended to be a cheaper version of getMinusSCEV. We can be
1485 /// frugal here since we just bail out of actually constructing and
1486 /// canonicalizing an expression in the cases where the result isn't going
1487 /// to be a constant.
1488 LLVM_ABI std::optional<APInt> computeConstantDifference(const SCEV *LHS,
1489 const SCEV *RHS);
1490
1491 /// Update no-wrap flags of an AddRec. This may drop the cached info about
1492 /// this AddRec (such as range info) in case if new flags may potentially
1493 /// sharpen it.
1495
1496 class LoopGuards {
1499 bool PreserveNUW = false;
1500 bool PreserveNSW = false;
1501 ScalarEvolution &SE;
1502
1503 LoopGuards(ScalarEvolution &SE) : SE(SE) {}
1504
1505 /// Recursively collect loop guards in \p Guards, starting from
1506 /// block \p Block with predecessor \p Pred. The intended starting point
1507 /// is to collect from a loop header and its predecessor.
1508 static void
1509 collectFromBlock(ScalarEvolution &SE, ScalarEvolution::LoopGuards &Guards,
1510 const BasicBlock *Block, const BasicBlock *Pred,
1512 unsigned Depth = 0);
1513
1514 /// Collect loop guards in \p Guards, starting from PHINode \p
1515 /// Phi, by calling \p collectFromBlock on the incoming blocks of
1516 /// \Phi and trying to merge the found constraints into a single
1517 /// combined one for \p Phi.
1518 static void collectFromPHI(
1522 unsigned Depth);
1523
1524 public:
1525 /// Collect rewrite map for loop guards for loop \p L, together with flags
1526 /// indicating if NUW and NSW can be preserved during rewriting.
1527 LLVM_ABI static LoopGuards collect(const Loop *L, ScalarEvolution &SE);
1528
1529 /// Try to apply the collected loop guards to \p Expr.
1530 LLVM_ABI const SCEV *rewrite(const SCEV *Expr) const;
1531 };
1532
1533 /// Try to apply information from loop guards for \p L to \p Expr.
1534 LLVM_ABI const SCEV *applyLoopGuards(const SCEV *Expr, const Loop *L);
1535 LLVM_ABI const SCEV *applyLoopGuards(const SCEV *Expr,
1536 const LoopGuards &Guards);
1537
1538 /// Return true if the loop has no abnormal exits. That is, if the loop
1539 /// is not infinite, it must exit through an explicit edge in the CFG.
1540 /// (As opposed to either a) throwing out of the function or b) entering a
1541 /// well defined infinite loop in some callee.)
1543 return getLoopProperties(L).HasNoAbnormalExits;
1544 }
1545
1546 /// Return true if this loop is finite by assumption. That is,
1547 /// to be infinite, it must also be undefined.
1548 LLVM_ABI bool loopIsFiniteByAssumption(const Loop *L);
1549
1550 /// Return the set of Values that, if poison, will definitively result in S
1551 /// being poison as well. The returned set may be incomplete, i.e. there can
1552 /// be additional Values that also result in S being poison.
1553 LLVM_ABI void
1555 const SCEV *S);
1556
1557 /// Check whether it is poison-safe to represent the expression S using the
1558 /// instruction I. If such a replacement is performed, the poison flags of
1559 /// instructions in DropPoisonGeneratingInsts must be dropped.
1561 const SCEV *S, Instruction *I,
1562 SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts);
1563
1564 class FoldID {
1565 const SCEV *Op = nullptr;
1566 const Type *Ty = nullptr;
1567 unsigned short C;
1568
1569 public:
1570 FoldID(SCEVTypes C, const SCEV *Op, const Type *Ty) : Op(Op), Ty(Ty), C(C) {
1571 assert(Op);
1572 assert(Ty);
1573 }
1574
1575 FoldID(unsigned short C) : C(C) {}
1576
1577 unsigned computeHash() const {
1579 C, detail::combineHashValue(reinterpret_cast<uintptr_t>(Op),
1580 reinterpret_cast<uintptr_t>(Ty)));
1581 }
1582
1583 bool operator==(const FoldID &RHS) const {
1584 return std::tie(Op, Ty, C) == std::tie(RHS.Op, RHS.Ty, RHS.C);
1585 }
1586 };
1587
1588private:
1589 /// A CallbackVH to arrange for ScalarEvolution to be notified whenever a
1590 /// Value is deleted.
1591 class LLVM_ABI SCEVCallbackVH final : public CallbackVH {
1592 ScalarEvolution *SE;
1593
1594 void deleted() override;
1595 void allUsesReplacedWith(Value *New) override;
1596
1597 public:
1598 SCEVCallbackVH(Value *V, ScalarEvolution *SE = nullptr);
1599 };
1600
1601 friend class SCEVCallbackVH;
1602 friend class SCEVExpander;
1603 friend class SCEVUnknown;
1604
1605 /// The function we are analyzing.
1606 Function &F;
1607
1608 /// Data layout of the module.
1609 const DataLayout &DL;
1610
1611 /// Does the module have any calls to the llvm.experimental.guard intrinsic
1612 /// at all? If this is false, we avoid doing work that will only help if
1613 /// thare are guards present in the IR.
1614 bool HasGuards;
1615
1616 /// The target library information for the target we are targeting.
1617 TargetLibraryInfo &TLI;
1618
1619 /// The tracker for \@llvm.assume intrinsics in this function.
1620 AssumptionCache &AC;
1621
1622 /// The dominator tree.
1623 DominatorTree &DT;
1624
1625 /// The loop information for the function we are currently analyzing.
1626 LoopInfo &LI;
1627
1628 /// This SCEV is used to represent unknown trip counts and things.
1629 std::unique_ptr<SCEVCouldNotCompute> CouldNotCompute;
1630
1631 /// The type for HasRecMap.
1632 using HasRecMapType = DenseMap<const SCEV *, bool>;
1633
1634 /// This is a cache to record whether a SCEV contains any scAddRecExpr.
1635 HasRecMapType HasRecMap;
1636
1637 /// The type for ExprValueMap.
1638 using ValueSetVector = SmallSetVector<Value *, 4>;
1639 using ExprValueMapType = DenseMap<const SCEV *, ValueSetVector>;
1640
1641 /// ExprValueMap -- This map records the original values from which
1642 /// the SCEV expr is generated from.
1643 ExprValueMapType ExprValueMap;
1644
1645 /// The type for ValueExprMap.
1646 using ValueExprMapType =
1648
1649 /// This is a cache of the values we have analyzed so far.
1650 ValueExprMapType ValueExprMap;
1651
1652 /// This is a cache for expressions that got folded to a different existing
1653 /// SCEV.
1656
1657 /// Mark predicate values currently being processed by isImpliedCond.
1658 SmallPtrSet<const Value *, 6> PendingLoopPredicates;
1659
1660 // Mark SCEVUnknown Phis currently being processed by isImpliedViaMerge.
1661 SmallPtrSet<const PHINode *, 6> PendingMerges;
1662
1663 /// Set to true by isLoopBackedgeGuardedByCond when we're walking the set of
1664 /// conditions dominating the backedge of a loop.
1665 bool WalkingBEDominatingConds = false;
1666
1667 /// Set to true by isKnownPredicateViaSplitting when we're trying to prove a
1668 /// predicate by splitting it into a set of independent predicates.
1669 bool ProvingSplitPredicate = false;
1670
1671 /// Memoized values for the getConstantMultiple
1672 DenseMap<const SCEV *, APInt> ConstantMultipleCache;
1673
1674 /// Return the Value set from which the SCEV expr is generated.
1675 ArrayRef<Value *> getSCEVValues(const SCEV *S);
1676
1677 /// Private helper method for the getConstantMultiple method. If \p CtxI is
1678 /// not nullptr, return a constant multiple valid at \p CtxI.
1679 APInt getConstantMultipleImpl(const SCEV *S,
1680 const Instruction *Ctx = nullptr);
1681
1682 /// Information about the number of times a particular loop exit may be
1683 /// reached before exiting the loop.
1684 struct ExitNotTakenInfo {
1685 PoisoningVH<BasicBlock> ExitingBlock;
1686 const SCEV *ExactNotTaken;
1687 const SCEV *ConstantMaxNotTaken;
1688 const SCEV *SymbolicMaxNotTaken;
1690
1691 explicit ExitNotTakenInfo(PoisoningVH<BasicBlock> ExitingBlock,
1692 const SCEV *ExactNotTaken,
1693 const SCEV *ConstantMaxNotTaken,
1694 const SCEV *SymbolicMaxNotTaken,
1696 : ExitingBlock(ExitingBlock), ExactNotTaken(ExactNotTaken),
1697 ConstantMaxNotTaken(ConstantMaxNotTaken),
1698 SymbolicMaxNotTaken(SymbolicMaxNotTaken), Predicates(Predicates) {}
1699
1700 bool hasAlwaysTruePredicate() const {
1701 return Predicates.empty();
1702 }
1703 };
1704
1705 /// Information about the backedge-taken count of a loop. This currently
1706 /// includes an exact count and a maximum count.
1707 ///
1708 class BackedgeTakenInfo {
1709 friend class ScalarEvolution;
1710
1711 /// A list of computable exits and their not-taken counts. Loops almost
1712 /// never have more than one computable exit.
1713 SmallVector<ExitNotTakenInfo, 1> ExitNotTaken;
1714
1715 /// Expression indicating the least constant maximum backedge-taken count of
1716 /// the loop that is known, or a SCEVCouldNotCompute. This expression is
1717 /// only valid if the predicates associated with all loop exits are true.
1718 const SCEV *ConstantMax = nullptr;
1719
1720 /// Indicating if \c ExitNotTaken has an element for every exiting block in
1721 /// the loop.
1722 bool IsComplete = false;
1723
1724 /// Expression indicating the least maximum backedge-taken count of the loop
1725 /// that is known, or a SCEVCouldNotCompute. Lazily computed on first query.
1726 const SCEV *SymbolicMax = nullptr;
1727
1728 /// True iff the backedge is taken either exactly Max or zero times.
1729 bool MaxOrZero = false;
1730
1731 bool isComplete() const { return IsComplete; }
1732 const SCEV *getConstantMax() const { return ConstantMax; }
1733
1734 LLVM_ABI const ExitNotTakenInfo *getExitNotTaken(
1735 const BasicBlock *ExitingBlock,
1736 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const;
1737
1738 public:
1739 BackedgeTakenInfo() = default;
1740 BackedgeTakenInfo(BackedgeTakenInfo &&) = default;
1741 BackedgeTakenInfo &operator=(BackedgeTakenInfo &&) = default;
1742
1743 using EdgeExitInfo = std::pair<BasicBlock *, ExitLimit>;
1744
1745 /// Initialize BackedgeTakenInfo from a list of exact exit counts.
1746 LLVM_ABI BackedgeTakenInfo(ArrayRef<EdgeExitInfo> ExitCounts,
1747 bool IsComplete, const SCEV *ConstantMax,
1748 bool MaxOrZero);
1749
1750 /// Test whether this BackedgeTakenInfo contains any computed information,
1751 /// or whether it's all SCEVCouldNotCompute values.
1752 bool hasAnyInfo() const {
1753 return !ExitNotTaken.empty() ||
1754 !isa<SCEVCouldNotCompute>(getConstantMax());
1755 }
1756
1757 /// Test whether this BackedgeTakenInfo contains complete information.
1758 bool hasFullInfo() const { return isComplete(); }
1759
1760 /// Return an expression indicating the exact *backedge-taken*
1761 /// count of the loop if it is known or SCEVCouldNotCompute
1762 /// otherwise. If execution makes it to the backedge on every
1763 /// iteration (i.e. there are no abnormal exists like exception
1764 /// throws and thread exits) then this is the number of times the
1765 /// loop header will execute minus one.
1766 ///
1767 /// If the SCEV predicate associated with the answer can be different
1768 /// from AlwaysTrue, we must add a (non null) Predicates argument.
1769 /// The SCEV predicate associated with the answer will be added to
1770 /// Predicates. A run-time check needs to be emitted for the SCEV
1771 /// predicate in order for the answer to be valid.
1772 ///
1773 /// Note that we should always know if we need to pass a predicate
1774 /// argument or not from the way the ExitCounts vector was computed.
1775 /// If we allowed SCEV predicates to be generated when populating this
1776 /// vector, this information can contain them and therefore a
1777 /// SCEVPredicate argument should be added to getExact.
1778 LLVM_ABI const SCEV *getExact(
1779 const Loop *L, ScalarEvolution *SE,
1780 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const;
1781
1782 /// Return the number of times this loop exit may fall through to the back
1783 /// edge, or SCEVCouldNotCompute. The loop is guaranteed not to exit via
1784 /// this block before this number of iterations, but may exit via another
1785 /// block. If \p Predicates is null the function returns CouldNotCompute if
1786 /// predicates are required, otherwise it fills in the required predicates.
1787 const SCEV *getExact(
1788 const BasicBlock *ExitingBlock, ScalarEvolution *SE,
1789 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const {
1790 if (auto *ENT = getExitNotTaken(ExitingBlock, Predicates))
1791 return ENT->ExactNotTaken;
1792 else
1793 return SE->getCouldNotCompute();
1794 }
1795
1796 /// Get the constant max backedge taken count for the loop.
1797 LLVM_ABI const SCEV *getConstantMax(
1798 ScalarEvolution *SE,
1799 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const;
1800
1801 /// Get the constant max backedge taken count for the particular loop exit.
1802 const SCEV *getConstantMax(
1803 const BasicBlock *ExitingBlock, ScalarEvolution *SE,
1804 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const {
1805 if (auto *ENT = getExitNotTaken(ExitingBlock, Predicates))
1806 return ENT->ConstantMaxNotTaken;
1807 else
1808 return SE->getCouldNotCompute();
1809 }
1810
1811 /// Get the symbolic max backedge taken count for the loop.
1812 LLVM_ABI const SCEV *getSymbolicMax(
1813 const Loop *L, ScalarEvolution *SE,
1814 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
1815
1816 /// Get the symbolic max backedge taken count for the particular loop exit.
1817 const SCEV *getSymbolicMax(
1818 const BasicBlock *ExitingBlock, ScalarEvolution *SE,
1819 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) const {
1820 if (auto *ENT = getExitNotTaken(ExitingBlock, Predicates))
1821 return ENT->SymbolicMaxNotTaken;
1822 else
1823 return SE->getCouldNotCompute();
1824 }
1825
1826 /// Return true if the number of times this backedge is taken is either the
1827 /// value returned by getConstantMax or zero.
1828 LLVM_ABI bool isConstantMaxOrZero(ScalarEvolution *SE) const;
1829 };
1830
1831 /// Cache the backedge-taken count of the loops for this function as they
1832 /// are computed.
1833 DenseMap<const Loop *, BackedgeTakenInfo> BackedgeTakenCounts;
1834
1835 /// Cache the predicated backedge-taken count of the loops for this
1836 /// function as they are computed.
1837 DenseMap<const Loop *, BackedgeTakenInfo> PredicatedBackedgeTakenCounts;
1838
1839 /// Loops whose backedge taken counts directly use this non-constant SCEV.
1840 DenseMap<const SCEV *, SmallPtrSet<PointerIntPair<const Loop *, 1, bool>, 4>>
1841 BECountUsers;
1842
1843 /// This map contains entries for all of the PHI instructions that we
1844 /// attempt to compute constant evolutions for. This allows us to avoid
1845 /// potentially expensive recomputation of these properties. An instruction
1846 /// maps to null if we are unable to compute its exit value.
1847 DenseMap<PHINode *, Constant *> ConstantEvolutionLoopExitValue;
1848
1849 /// This map contains entries for all the expressions that we attempt to
1850 /// compute getSCEVAtScope information for, which can be expensive in
1851 /// extreme cases.
1852 DenseMap<const SCEV *, SmallVector<std::pair<const Loop *, const SCEV *>, 2>>
1853 ValuesAtScopes;
1854
1855 /// Reverse map for invalidation purposes: Stores of which SCEV and which
1856 /// loop this is the value-at-scope of.
1857 DenseMap<const SCEV *, SmallVector<std::pair<const Loop *, const SCEV *>, 2>>
1858 ValuesAtScopesUsers;
1859
1860 /// Memoized computeLoopDisposition results.
1861 DenseMap<const SCEV *,
1863 LoopDispositions;
1864
1865 struct LoopProperties {
1866 /// Set to true if the loop contains no instruction that can abnormally exit
1867 /// the loop (i.e. via throwing an exception, by terminating the thread
1868 /// cleanly or by infinite looping in a called function). Strictly
1869 /// speaking, the last one is not leaving the loop, but is identical to
1870 /// leaving the loop for reasoning about undefined behavior.
1871 bool HasNoAbnormalExits;
1872
1873 /// Set to true if the loop contains no instruction that can have side
1874 /// effects (i.e. via throwing an exception, volatile or atomic access).
1875 bool HasNoSideEffects;
1876 };
1877
1878 /// Cache for \c getLoopProperties.
1879 DenseMap<const Loop *, LoopProperties> LoopPropertiesCache;
1880
1881 /// Return a \c LoopProperties instance for \p L, creating one if necessary.
1882 LLVM_ABI LoopProperties getLoopProperties(const Loop *L);
1883
1884 bool loopHasNoSideEffects(const Loop *L) {
1885 return getLoopProperties(L).HasNoSideEffects;
1886 }
1887
1888 /// Compute a LoopDisposition value.
1889 LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L);
1890
1891 /// Memoized computeBlockDisposition results.
1892 DenseMap<
1893 const SCEV *,
1895 BlockDispositions;
1896
1897 /// Compute a BlockDisposition value.
1898 BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB);
1899
1900 /// Stores all SCEV that use a given SCEV as its direct operand.
1901 DenseMap<const SCEV *, SmallPtrSet<const SCEV *, 8> > SCEVUsers;
1902
1903 /// Memoized results from getRange
1904 DenseMap<const SCEV *, ConstantRange> UnsignedRanges;
1905
1906 /// Memoized results from getRange
1907 DenseMap<const SCEV *, ConstantRange> SignedRanges;
1908
1909 /// Used to parameterize getRange
1910 enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED };
1911
1912 /// Set the memoized range for the given SCEV.
1913 const ConstantRange &setRange(const SCEV *S, RangeSignHint Hint,
1914 ConstantRange CR) {
1915 DenseMap<const SCEV *, ConstantRange> &Cache =
1916 Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges;
1917
1918 auto Pair = Cache.insert_or_assign(S, std::move(CR));
1919 return Pair.first->second;
1920 }
1921
1922 /// Determine the range for a particular SCEV.
1923 /// NOTE: This returns a reference to an entry in a cache. It must be
1924 /// copied if its needed for longer.
1925 LLVM_ABI const ConstantRange &getRangeRef(const SCEV *S, RangeSignHint Hint,
1926 unsigned Depth = 0);
1927
1928 /// Determine the range for a particular SCEV, but evaluates ranges for
1929 /// operands iteratively first.
1930 const ConstantRange &getRangeRefIter(const SCEV *S, RangeSignHint Hint);
1931
1932 /// Determines the range for the affine SCEVAddRecExpr {\p Start,+,\p Step}.
1933 /// Helper for \c getRange.
1934 ConstantRange getRangeForAffineAR(const SCEV *Start, const SCEV *Step,
1935 const APInt &MaxBECount);
1936
1937 /// Determines the range for the affine non-self-wrapping SCEVAddRecExpr {\p
1938 /// Start,+,\p Step}<nw>.
1939 ConstantRange getRangeForAffineNoSelfWrappingAR(const SCEVAddRecExpr *AddRec,
1940 const SCEV *MaxBECount,
1941 unsigned BitWidth,
1942 RangeSignHint SignHint);
1943
1944 /// Try to compute a range for the affine SCEVAddRecExpr {\p Start,+,\p
1945 /// Step} by "factoring out" a ternary expression from the add recurrence.
1946 /// Helper called by \c getRange.
1947 ConstantRange getRangeViaFactoring(const SCEV *Start, const SCEV *Step,
1948 const APInt &MaxBECount);
1949
1950 /// If the unknown expression U corresponds to a simple recurrence, return
1951 /// a constant range which represents the entire recurrence. Note that
1952 /// *add* recurrences with loop invariant steps aren't represented by
1953 /// SCEVUnknowns and thus don't use this mechanism.
1954 ConstantRange getRangeForUnknownRecurrence(const SCEVUnknown *U);
1955
1956 /// We know that there is no SCEV for the specified value. Analyze the
1957 /// expression recursively.
1958 const SCEV *createSCEV(Value *V);
1959
1960 /// We know that there is no SCEV for the specified value. Create a new SCEV
1961 /// for \p V iteratively.
1962 const SCEV *createSCEVIter(Value *V);
1963 /// Collect operands of \p V for which SCEV expressions should be constructed
1964 /// first. Returns a SCEV directly if it can be constructed trivially for \p
1965 /// V.
1966 const SCEV *getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops);
1967
1968 /// Returns SCEV for the first operand of a phi if all phi operands have
1969 /// identical opcodes and operands.
1970 const SCEV *createNodeForPHIWithIdenticalOperands(PHINode *PN);
1971
1972 /// Provide the special handling we need to analyze PHI SCEVs.
1973 const SCEV *createNodeForPHI(PHINode *PN);
1974
1975 /// Helper function called from createNodeForPHI.
1976 const SCEV *createAddRecFromPHI(PHINode *PN);
1977
1978 /// A helper function for createAddRecFromPHI to handle simple cases.
1979 const SCEV *createSimpleAffineAddRec(PHINode *PN, Value *BEValueV,
1980 Value *StartValueV);
1981
1982 /// Helper function called from createNodeForPHI.
1983 const SCEV *createNodeFromSelectLikePHI(PHINode *PN);
1984
1985 /// Provide special handling for a select-like instruction (currently this
1986 /// is either a select instruction or a phi node). \p Ty is the type of the
1987 /// instruction being processed, that is assumed equivalent to
1988 /// "Cond ? TrueVal : FalseVal".
1989 std::optional<const SCEV *>
1990 createNodeForSelectOrPHIInstWithICmpInstCond(Type *Ty, ICmpInst *Cond,
1991 Value *TrueVal, Value *FalseVal);
1992
1993 /// See if we can model this select-like instruction via umin_seq expression.
1994 const SCEV *createNodeForSelectOrPHIViaUMinSeq(Value *I, Value *Cond,
1995 Value *TrueVal,
1996 Value *FalseVal);
1997
1998 /// Given a value \p V, which is a select-like instruction (currently this is
1999 /// either a select instruction or a phi node), which is assumed equivalent to
2000 /// Cond ? TrueVal : FalseVal
2001 /// see if we can model it as a SCEV expression.
2002 const SCEV *createNodeForSelectOrPHI(Value *V, Value *Cond, Value *TrueVal,
2003 Value *FalseVal);
2004
2005 /// Provide the special handling we need to analyze GEP SCEVs.
2006 const SCEV *createNodeForGEP(GEPOperator *GEP);
2007
2008 /// Implementation code for getSCEVAtScope; called at most once for each
2009 /// SCEV+Loop pair.
2010 const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
2011
2012 /// Return the BackedgeTakenInfo for the given loop, lazily computing new
2013 /// values if the loop hasn't been analyzed yet. The returned result is
2014 /// guaranteed not to be predicated.
2015 BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
2016
2017 /// Similar to getBackedgeTakenInfo, but will add predicates as required
2018 /// with the purpose of returning complete information.
2019 BackedgeTakenInfo &getPredicatedBackedgeTakenInfo(const Loop *L);
2020
2021 /// Compute the number of times the specified loop will iterate.
2022 /// If AllowPredicates is set, we will create new SCEV predicates as
2023 /// necessary in order to return an exact answer.
2024 BackedgeTakenInfo computeBackedgeTakenCount(const Loop *L,
2025 bool AllowPredicates = false);
2026
2027 /// Compute the number of times the backedge of the specified loop will
2028 /// execute if it exits via the specified block. If AllowPredicates is set,
2029 /// this call will try to use a minimal set of SCEV predicates in order to
2030 /// return an exact answer.
2031 ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
2032 bool IsOnlyExit, bool AllowPredicates = false);
2033
2034 // Helper functions for computeExitLimitFromCond to avoid exponential time
2035 // complexity.
2036
2037 class ExitLimitCache {
2038 // It may look like we need key on the whole (L, ExitIfTrue,
2039 // ControlsOnlyExit, AllowPredicates) tuple, but recursive calls to
2040 // computeExitLimitFromCondCached from computeExitLimitFromCondImpl only
2041 // vary the in \c ExitCond and \c ControlsOnlyExit parameters. We remember
2042 // the initial values of the other values to assert our assumption.
2043 SmallDenseMap<PointerIntPair<Value *, 1>, ExitLimit> TripCountMap;
2044
2045 const Loop *L;
2046 bool ExitIfTrue;
2047 bool AllowPredicates;
2048
2049 public:
2050 ExitLimitCache(const Loop *L, bool ExitIfTrue, bool AllowPredicates)
2051 : L(L), ExitIfTrue(ExitIfTrue), AllowPredicates(AllowPredicates) {}
2052
2053 LLVM_ABI std::optional<ExitLimit> find(const Loop *L, Value *ExitCond,
2054 bool ExitIfTrue,
2055 bool ControlsOnlyExit,
2056 bool AllowPredicates);
2057
2058 LLVM_ABI void insert(const Loop *L, Value *ExitCond, bool ExitIfTrue,
2059 bool ControlsOnlyExit, bool AllowPredicates,
2060 const ExitLimit &EL);
2061 };
2062
2063 using ExitLimitCacheTy = ExitLimitCache;
2064
2065 ExitLimit computeExitLimitFromCondCached(ExitLimitCacheTy &Cache,
2066 const Loop *L, Value *ExitCond,
2067 bool ExitIfTrue,
2068 bool ControlsOnlyExit,
2069 bool AllowPredicates);
2070 ExitLimit computeExitLimitFromCondImpl(ExitLimitCacheTy &Cache, const Loop *L,
2071 Value *ExitCond, bool ExitIfTrue,
2072 bool ControlsOnlyExit,
2073 bool AllowPredicates);
2074 std::optional<ScalarEvolution::ExitLimit> computeExitLimitFromCondFromBinOp(
2075 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
2076 bool ControlsOnlyExit, bool AllowPredicates);
2077
2078 /// Compute the number of times the backedge of the specified loop will
2079 /// execute if its exit condition were a conditional branch of the ICmpInst
2080 /// ExitCond and ExitIfTrue. If AllowPredicates is set, this call will try
2081 /// to use a minimal set of SCEV predicates in order to return an exact
2082 /// answer.
2083 ExitLimit computeExitLimitFromICmp(const Loop *L, ICmpInst *ExitCond,
2084 bool ExitIfTrue,
2085 bool IsSubExpr,
2086 bool AllowPredicates = false);
2087
2088 /// Variant of previous which takes the components representing an ICmp
2089 /// as opposed to the ICmpInst itself. Note that the prior version can
2090 /// return more precise results in some cases and is preferred when caller
2091 /// has a materialized ICmp.
2092 ExitLimit computeExitLimitFromICmp(const Loop *L, CmpPredicate Pred,
2093 SCEVUse LHS, SCEVUse RHS, bool IsSubExpr,
2094 bool AllowPredicates = false);
2095
2096 /// Compute the number of times the backedge of the specified loop will
2097 /// execute if its exit condition were a switch with a single exiting case
2098 /// to ExitingBB.
2099 ExitLimit computeExitLimitFromSingleExitSwitch(const Loop *L,
2100 SwitchInst *Switch,
2101 BasicBlock *ExitingBB,
2102 bool IsSubExpr);
2103
2104 /// Compute the exit limit of a loop that is controlled by a
2105 /// "(IV >> 1) != 0" type comparison. We cannot compute the exact trip
2106 /// count in these cases (since SCEV has no way of expressing them), but we
2107 /// can still sometimes compute an upper bound.
2108 ///
2109 /// Return an ExitLimit for a loop whose backedge is guarded by `LHS Pred
2110 /// RHS`.
2111 ExitLimit computeShiftCompareExitLimit(Value *LHS, Value *RHS, const Loop *L,
2112 ICmpInst::Predicate Pred);
2113
2114 /// If the loop is known to execute a constant number of times (the
2115 /// condition evolves only from constants), try to evaluate a few iterations
2116 /// of the loop until we get the exit condition gets a value of ExitWhen
2117 /// (true or false). If we cannot evaluate the exit count of the loop,
2118 /// return CouldNotCompute.
2119 const SCEV *computeExitCountExhaustively(const Loop *L, Value *Cond,
2120 bool ExitWhen);
2121
2122 /// Return the number of times an exit condition comparing the specified
2123 /// value to zero will execute. If not computable, return CouldNotCompute.
2124 /// If AllowPredicates is set, this call will try to use a minimal set of
2125 /// SCEV predicates in order to return an exact answer.
2126 ExitLimit howFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr,
2127 bool AllowPredicates = false);
2128
2129 /// Return the number of times an exit condition checking the specified
2130 /// value for nonzero will execute. If not computable, return
2131 /// CouldNotCompute.
2132 ExitLimit howFarToNonZero(const SCEV *V, const Loop *L);
2133
2134 /// Return the number of times an exit condition containing the specified
2135 /// less-than comparison will execute. If not computable, return
2136 /// CouldNotCompute.
2137 ///
2138 /// \p isSigned specifies whether the less-than is signed.
2139 ///
2140 /// \p ControlsOnlyExit is true when the LHS < RHS condition directly controls
2141 /// the branch (loops exits only if condition is true). In this case, we can
2142 /// use NoWrapFlags to skip overflow checks.
2143 ///
2144 /// If \p AllowPredicates is set, this call will try to use a minimal set of
2145 /// SCEV predicates in order to return an exact answer.
2146 ExitLimit howManyLessThans(const SCEV *LHS, const SCEV *RHS, const Loop *L,
2147 bool isSigned, bool ControlsOnlyExit,
2148 bool AllowPredicates = false);
2149
2150 ExitLimit howManyGreaterThans(const SCEV *LHS, const SCEV *RHS, const Loop *L,
2151 bool isSigned, bool IsSubExpr,
2152 bool AllowPredicates = false);
2153
2154 /// Return a predecessor of BB (which may not be an immediate predecessor)
2155 /// which has exactly one successor from which BB is reachable, or null if
2156 /// no such block is found.
2157 std::pair<const BasicBlock *, const BasicBlock *>
2158 getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB) const;
2159
2160 /// Test whether the condition described by Pred, LHS, and RHS is true
2161 /// whenever the given FoundCondValue value evaluates to true in given
2162 /// Context. If Context is nullptr, then the found predicate is true
2163 /// everywhere. LHS and FoundLHS may have different type width.
2164 LLVM_ABI bool isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
2165 const SCEV *RHS, const Value *FoundCondValue,
2166 bool Inverse,
2167 const Instruction *Context = nullptr);
2168
2169 /// Test whether the condition described by Pred, LHS, and RHS is true
2170 /// whenever the given FoundCondValue value evaluates to true in given
2171 /// Context. If Context is nullptr, then the found predicate is true
2172 /// everywhere. LHS and FoundLHS must have same type width.
2173 LLVM_ABI bool isImpliedCondBalancedTypes(CmpPredicate Pred, SCEVUse LHS,
2174 SCEVUse RHS, CmpPredicate FoundPred,
2175 SCEVUse FoundLHS, SCEVUse FoundRHS,
2176 const Instruction *CtxI);
2177
2178 /// Test whether the condition described by Pred, LHS, and RHS is true
2179 /// whenever the condition described by FoundPred, FoundLHS, FoundRHS is
2180 /// true in given Context. If Context is nullptr, then the found predicate is
2181 /// true everywhere.
2182 LLVM_ABI bool isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
2183 const SCEV *RHS, CmpPredicate FoundPred,
2184 const SCEV *FoundLHS, const SCEV *FoundRHS,
2185 const Instruction *Context = nullptr);
2186
2187 /// Test whether the condition described by Pred, LHS, and RHS is true
2188 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
2189 /// true in given Context. If Context is nullptr, then the found predicate is
2190 /// true everywhere.
2191 bool isImpliedCondOperands(CmpPredicate Pred, const SCEV *LHS,
2192 const SCEV *RHS, const SCEV *FoundLHS,
2193 const SCEV *FoundRHS,
2194 const Instruction *Context = nullptr);
2195
2196 /// Test whether the condition described by Pred, LHS, and RHS is true
2197 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
2198 /// true. Here LHS is an operation that includes FoundLHS as one of its
2199 /// arguments.
2200 bool isImpliedViaOperations(CmpPredicate Pred, const SCEV *LHS,
2201 const SCEV *RHS, const SCEV *FoundLHS,
2202 const SCEV *FoundRHS, unsigned Depth = 0);
2203
2204 /// Test whether the condition described by Pred, LHS, and RHS is true.
2205 /// Use only simple non-recursive types of checks, such as range analysis etc.
2206 bool isKnownViaNonRecursiveReasoning(CmpPredicate Pred, SCEVUse LHS,
2207 SCEVUse RHS);
2208
2209 /// Test whether the condition described by Pred, LHS, and RHS is true
2210 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
2211 /// true.
2212 bool isImpliedCondOperandsHelper(CmpPredicate Pred, const SCEV *LHS,
2213 const SCEV *RHS, const SCEV *FoundLHS,
2214 const SCEV *FoundRHS);
2215
2216 /// Test whether the condition described by Pred, LHS, and RHS is true
2217 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
2218 /// true. Utility function used by isImpliedCondOperands. Tries to get
2219 /// cases like "X `sgt` 0 => X - 1 `sgt` -1".
2220 bool isImpliedCondOperandsViaRanges(CmpPredicate Pred, const SCEV *LHS,
2221 const SCEV *RHS, CmpPredicate FoundPred,
2222 const SCEV *FoundLHS,
2223 const SCEV *FoundRHS);
2224
2225 /// Return true if the condition denoted by \p LHS \p Pred \p RHS is implied
2226 /// by a call to @llvm.experimental.guard in \p BB.
2227 bool isImpliedViaGuard(const BasicBlock *BB, CmpPredicate Pred,
2228 const SCEV *LHS, const SCEV *RHS);
2229
2230 /// Test whether the condition described by Pred, LHS, and RHS is true
2231 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
2232 /// true.
2233 ///
2234 /// This routine tries to rule out certain kinds of integer overflow, and
2235 /// then tries to reason about arithmetic properties of the predicates.
2236 bool isImpliedCondOperandsViaNoOverflow(CmpPredicate Pred, const SCEV *LHS,
2237 const SCEV *RHS, const SCEV *FoundLHS,
2238 const SCEV *FoundRHS);
2239
2240 /// Test whether the condition described by Pred, LHS, and RHS is true
2241 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
2242 /// true.
2243 ///
2244 /// This routine tries to weaken the known condition basing on fact that
2245 /// FoundLHS is an AddRec.
2246 bool isImpliedCondOperandsViaAddRecStart(CmpPredicate Pred, const SCEV *LHS,
2247 const SCEV *RHS,
2248 const SCEV *FoundLHS,
2249 const SCEV *FoundRHS,
2250 const Instruction *CtxI);
2251
2252 /// Test whether the condition described by Pred, LHS, and RHS is true
2253 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
2254 /// true.
2255 ///
2256 /// This routine tries to figure out predicate for Phis which are SCEVUnknown
2257 /// if it is true for every possible incoming value from their respective
2258 /// basic blocks.
2259 bool isImpliedViaMerge(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS,
2260 const SCEV *FoundLHS, const SCEV *FoundRHS,
2261 unsigned Depth);
2262
2263 /// Test whether the condition described by Pred, LHS, and RHS is true
2264 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
2265 /// true.
2266 ///
2267 /// This routine tries to reason about shifts.
2268 bool isImpliedCondOperandsViaShift(CmpPredicate Pred, const SCEV *LHS,
2269 const SCEV *RHS, const SCEV *FoundLHS,
2270 const SCEV *FoundRHS);
2271
2272 /// If we know that the specified Phi is in the header of its containing
2273 /// loop, we know the loop executes a constant number of times, and the PHI
2274 /// node is just a recurrence involving constants, fold it.
2275 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt &BEs,
2276 const Loop *L);
2277
2278 /// Test if the given expression is known to satisfy the condition described
2279 /// by Pred and the known constant ranges of LHS and RHS.
2280 bool isKnownPredicateViaConstantRanges(CmpPredicate Pred, SCEVUse LHS,
2281 SCEVUse RHS);
2282
2283 /// Try to prove the condition described by "LHS Pred RHS" by ruling out
2284 /// integer overflow.
2285 ///
2286 /// For instance, this will return true for "A s< (A + C)<nsw>" if C is
2287 /// positive.
2288 bool isKnownPredicateViaNoOverflow(CmpPredicate Pred, SCEVUse LHS,
2289 SCEVUse RHS);
2290
2291 /// Try to split Pred LHS RHS into logical conjunctions (and's) and try to
2292 /// prove them individually.
2293 bool isKnownPredicateViaSplitting(CmpPredicate Pred, SCEVUse LHS,
2294 SCEVUse RHS);
2295
2296 /// Try to match the Expr as "(L + R)<Flags>".
2297 bool splitBinaryAdd(SCEVUse Expr, SCEVUse &L, SCEVUse &R,
2298 SCEV::NoWrapFlags &Flags);
2299
2300 /// Forget predicated/non-predicated backedge taken counts for the given loop.
2301 void forgetBackedgeTakenCounts(const Loop *L, bool Predicated);
2302
2303 /// Drop memoized information for all \p SCEVs.
2304 void forgetMemoizedResults(ArrayRef<SCEVUse> SCEVs);
2305
2306 /// Helper for forgetMemoizedResults.
2307 void forgetMemoizedResultsImpl(const SCEV *S);
2308
2309 /// Iterate over instructions in \p Worklist and their users. Erase entries
2310 /// from ValueExprMap and collect SCEV expressions in \p ToForget
2311 void visitAndClearUsers(SmallVectorImpl<Instruction *> &Worklist,
2312 SmallPtrSetImpl<Instruction *> &Visited,
2313 SmallVectorImpl<SCEVUse> &ToForget);
2314
2315 /// Erase Value from ValueExprMap and ExprValueMap.
2316 void eraseValueFromMap(Value *V);
2317
2318 /// Insert V to S mapping into ValueExprMap and ExprValueMap.
2319 void insertValueToMap(Value *V, const SCEV *S);
2320
2321 /// Return false iff given SCEV contains a SCEVUnknown with NULL value-
2322 /// pointer.
2323 bool checkValidity(const SCEV *S) const;
2324
2325 /// Return true if `ExtendOpTy`({`Start`,+,`Step`}) can be proved to be
2326 /// equal to {`ExtendOpTy`(`Start`),+,`ExtendOpTy`(`Step`)}. This is
2327 /// equivalent to proving no signed (resp. unsigned) wrap in
2328 /// {`Start`,+,`Step`} if `ExtendOpTy` is `SCEVSignExtendExpr`
2329 /// (resp. `SCEVZeroExtendExpr`).
2330 template <typename ExtendOpTy>
2331 bool proveNoWrapByVaryingStart(const SCEV *Start, const SCEV *Step,
2332 const Loop *L);
2333
2334 /// Try to prove NSW or NUW on \p AR relying on ConstantRange manipulation.
2335 SCEV::NoWrapFlags proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR);
2336
2337 /// Try to prove NSW on \p AR by proving facts about conditions known on
2338 /// entry and backedge.
2339 SCEV::NoWrapFlags proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR);
2340
2341 /// Try to prove NUW on \p AR by proving facts about conditions known on
2342 /// entry and backedge.
2343 SCEV::NoWrapFlags proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR);
2344
2345 std::optional<MonotonicPredicateType>
2346 getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS,
2347 ICmpInst::Predicate Pred);
2348
2349 /// Return SCEV no-wrap flags that can be proven based on reasoning about
2350 /// how poison produced from no-wrap flags on this value (e.g. a nuw add)
2351 /// would trigger undefined behavior on overflow.
2352 SCEV::NoWrapFlags getNoWrapFlagsFromUB(const Value *V);
2353
2354 /// Return a scope which provides an upper bound on the defining scope of
2355 /// 'S'. Specifically, return the first instruction in said bounding scope.
2356 /// Return nullptr if the scope is trivial (function entry).
2357 /// (See scope definition rules associated with flag discussion above)
2358 const Instruction *getNonTrivialDefiningScopeBound(const SCEV *S);
2359
2360 /// Return a scope which provides an upper bound on the defining scope for
2361 /// a SCEV with the operands in Ops. The outparam Precise is set if the
2362 /// bound found is a precise bound (i.e. must be the defining scope.)
2363 const Instruction *getDefiningScopeBound(ArrayRef<SCEVUse> Ops,
2364 bool &Precise);
2365
2366 /// Wrapper around the above for cases which don't care if the bound
2367 /// is precise.
2368 const Instruction *getDefiningScopeBound(ArrayRef<SCEVUse> Ops);
2369
2370 /// Given two instructions in the same function, return true if we can
2371 /// prove B must execute given A executes.
2372 bool isGuaranteedToTransferExecutionTo(const Instruction *A,
2373 const Instruction *B);
2374
2375 /// Returns true if \p Op is guaranteed not to cause immediate UB.
2376 bool isGuaranteedNotToCauseUB(const SCEV *Op);
2377
2378 /// Returns true if \p Op is guaranteed to not be poison.
2379 static bool isGuaranteedNotToBePoison(const SCEV *Op);
2380
2381 /// Return true if the SCEV corresponding to \p I is never poison. Proving
2382 /// this is more complex than proving that just \p I is never poison, since
2383 /// SCEV commons expressions across control flow, and you can have cases
2384 /// like:
2385 ///
2386 /// idx0 = a + b;
2387 /// ptr[idx0] = 100;
2388 /// if (<condition>) {
2389 /// idx1 = a +nsw b;
2390 /// ptr[idx1] = 200;
2391 /// }
2392 ///
2393 /// where the SCEV expression (+ a b) is guaranteed to not be poison (and
2394 /// hence not sign-overflow) only if "<condition>" is true. Since both
2395 /// `idx0` and `idx1` will be mapped to the same SCEV expression, (+ a b),
2396 /// it is not okay to annotate (+ a b) with <nsw> in the above example.
2397 bool isSCEVExprNeverPoison(const Instruction *I);
2398
2399 /// This is like \c isSCEVExprNeverPoison but it specifically works for
2400 /// instructions that will get mapped to SCEV add recurrences. Return true
2401 /// if \p I will never generate poison under the assumption that \p I is an
2402 /// add recurrence on the loop \p L.
2403 bool isAddRecNeverPoison(const Instruction *I, const Loop *L);
2404
2405 /// Similar to createAddRecFromPHI, but with the additional flexibility of
2406 /// suggesting runtime overflow checks in case casts are encountered.
2407 /// If successful, the analysis records that for this loop, \p SymbolicPHI,
2408 /// which is the UnknownSCEV currently representing the PHI, can be rewritten
2409 /// into an AddRec, assuming some predicates; The function then returns the
2410 /// AddRec and the predicates as a pair, and caches this pair in
2411 /// PredicatedSCEVRewrites.
2412 /// If the analysis is not successful, a mapping from the \p SymbolicPHI to
2413 /// itself (with no predicates) is recorded, and a nullptr with an empty
2414 /// predicates vector is returned as a pair.
2415 std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
2416 createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI);
2417
2418 /// Compute the maximum backedge count based on the range of values
2419 /// permitted by Start, End, and Stride. This is for loops of the form
2420 /// {Start, +, Stride} LT End.
2421 ///
2422 /// Preconditions:
2423 /// * the induction variable is known to be positive.
2424 /// * the induction variable is assumed not to overflow (i.e. either it
2425 /// actually doesn't, or we'd have to immediately execute UB)
2426 /// We *don't* assert these preconditions so please be careful.
2427 const SCEV *computeMaxBECountForLT(const SCEV *Start, const SCEV *Stride,
2428 const SCEV *End, unsigned BitWidth,
2429 bool IsSigned);
2430
2431 /// Verify if an linear IV with positive stride can overflow when in a
2432 /// less-than comparison, knowing the invariant term of the comparison,
2433 /// the stride.
2434 bool canIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, bool IsSigned);
2435
2436 /// Verify if an linear IV with negative stride can overflow when in a
2437 /// greater-than comparison, knowing the invariant term of the comparison,
2438 /// the stride.
2439 bool canIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, bool IsSigned);
2440
2441 /// Get add expr already created or create a new one.
2442 const SCEV *getOrCreateAddExpr(ArrayRef<SCEVUse> Ops,
2443 SCEV::NoWrapFlags Flags);
2444
2445 /// Get mul expr already created or create a new one.
2446 const SCEV *getOrCreateMulExpr(ArrayRef<SCEVUse> Ops,
2447 SCEV::NoWrapFlags Flags);
2448
2449 // Get addrec expr already created or create a new one.
2450 const SCEV *getOrCreateAddRecExpr(ArrayRef<SCEVUse> Ops, const Loop *L,
2451 SCEV::NoWrapFlags Flags);
2452
2453 /// Return x if \p Val is f(x) where f is a 1-1 function.
2454 const SCEV *stripInjectiveFunctions(const SCEV *Val) const;
2455
2456 /// Find all of the loops transitively used in \p S, and fill \p LoopsUsed.
2457 /// A loop is considered "used" by an expression if it contains
2458 /// an add rec on said loop.
2459 void getUsedLoops(const SCEV *S, SmallPtrSetImpl<const Loop *> &LoopsUsed);
2460
2461 /// Look for a SCEV expression with type `SCEVType` and operands `Ops` in
2462 /// `UniqueSCEVs`. Return if found, else nullptr.
2463 SCEV *findExistingSCEVInCache(SCEVTypes SCEVType, ArrayRef<const SCEV *> Ops);
2464 SCEV *findExistingSCEVInCache(SCEVTypes SCEVType, ArrayRef<SCEVUse> Ops);
2465
2466 /// Get reachable blocks in this function, making limited use of SCEV
2467 /// reasoning about conditions.
2468 void getReachableBlocks(SmallPtrSetImpl<BasicBlock *> &Reachable,
2469 Function &F);
2470
2471 /// Return the given SCEV expression with a new set of operands.
2472 /// This preserves the origial nowrap flags.
2473 const SCEV *getWithOperands(const SCEV *S, SmallVectorImpl<SCEVUse> &NewOps);
2474
2475 FoldingSet<SCEV> UniqueSCEVs;
2476 FoldingSet<SCEVPredicate> UniquePreds;
2477 BumpPtrAllocator SCEVAllocator;
2478
2479 /// This maps loops to a list of addrecs that directly use said loop.
2480 DenseMap<const Loop *, SmallVector<const SCEVAddRecExpr *, 4>> LoopUsers;
2481
2482 /// Cache tentative mappings from UnknownSCEVs in a Loop, to a SCEV expression
2483 /// they can be rewritten into under certain predicates.
2484 DenseMap<std::pair<const SCEVUnknown *, const Loop *>,
2485 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
2486 PredicatedSCEVRewrites;
2487
2488 /// Set of AddRecs for which proving NUW via an induction has already been
2489 /// tried.
2490 SmallPtrSet<const SCEVAddRecExpr *, 16> UnsignedWrapViaInductionTried;
2491
2492 /// Set of AddRecs for which proving NSW via an induction has already been
2493 /// tried.
2494 SmallPtrSet<const SCEVAddRecExpr *, 16> SignedWrapViaInductionTried;
2495
2496 /// The head of a linked list of all SCEVUnknown values that have been
2497 /// allocated. This is used by releaseMemory to locate them all and call
2498 /// their destructors.
2499 SCEVUnknown *FirstUnknown = nullptr;
2500};
2501
2502/// Analysis pass that exposes the \c ScalarEvolution for a function.
2504 : public AnalysisInfoMixin<ScalarEvolutionAnalysis> {
2506
2507 LLVM_ABI static AnalysisKey Key;
2508
2509public:
2511
2513};
2514
2515/// Verifier pass for the \c ScalarEvolutionAnalysis results.
2517 : public PassInfoMixin<ScalarEvolutionVerifierPass> {
2518public:
2520 static bool isRequired() { return true; }
2521};
2522
2523/// Printer pass for the \c ScalarEvolutionAnalysis results.
2525 : public PassInfoMixin<ScalarEvolutionPrinterPass> {
2526 raw_ostream &OS;
2527
2528public:
2529 explicit ScalarEvolutionPrinterPass(raw_ostream &OS) : OS(OS) {}
2530
2532
2533 static bool isRequired() { return true; }
2534};
2535
2537 std::unique_ptr<ScalarEvolution> SE;
2538
2539public:
2540 static char ID;
2541
2543
2544 ScalarEvolution &getSE() { return *SE; }
2545 const ScalarEvolution &getSE() const { return *SE; }
2546
2547 bool runOnFunction(Function &F) override;
2548 void releaseMemory() override;
2549 void getAnalysisUsage(AnalysisUsage &AU) const override;
2550 void print(raw_ostream &OS, const Module * = nullptr) const override;
2551 void verifyAnalysis() const override;
2552};
2553
2554/// An interface layer with SCEV used to manage how we see SCEV expressions
2555/// for values in the context of existing predicates. We can add new
2556/// predicates, but we cannot remove them.
2557///
2558/// This layer has multiple purposes:
2559/// - provides a simple interface for SCEV versioning.
2560/// - guarantees that the order of transformations applied on a SCEV
2561/// expression for a single Value is consistent across two different
2562/// getSCEV calls. This means that, for example, once we've obtained
2563/// an AddRec expression for a certain value through expression
2564/// rewriting, we will continue to get an AddRec expression for that
2565/// Value.
2566/// - lowers the number of expression rewrites.
2568public:
2570
2571 LLVM_ABI const SCEVPredicate &getPredicate() const;
2572
2573 /// Returns the SCEV expression of V, in the context of the current SCEV
2574 /// predicate. The order of transformations applied on the expression of V
2575 /// returned by ScalarEvolution is guaranteed to be preserved, even when
2576 /// adding new predicates.
2577 LLVM_ABI const SCEV *getSCEV(Value *V);
2578
2579 /// Returns the rewritten SCEV for \p Expr in the context of the current SCEV
2580 /// predicate. The order of transformations applied on the expression of \p
2581 /// Expr returned by ScalarEvolution is guaranteed to be preserved, even when
2582 /// adding new predicates.
2583 LLVM_ABI const SCEV *getPredicatedSCEV(const SCEV *Expr);
2584
2585 /// Get the (predicated) backedge count for the analyzed loop.
2587
2588 /// Get the (predicated) symbolic max backedge count for the analyzed loop.
2590
2591 /// Returns the upper bound of the loop trip count as a normal unsigned
2592 /// value, or 0 if the trip count is unknown.
2594
2595 /// Adds a new predicate.
2596 LLVM_ABI void addPredicate(const SCEVPredicate &Pred);
2597
2598 /// Attempts to produce an AddRecExpr for V by adding additional SCEV
2599 /// predicates. If we can't transform the expression into an AddRecExpr we
2600 /// return nullptr and not add additional SCEV predicates to the current
2601 /// context.
2603
2604 /// Proves that V doesn't overflow by adding SCEV predicate.
2607
2608 /// Returns true if we've proved that V doesn't wrap by means of a SCEV
2609 /// predicate.
2612
2613 /// Returns the ScalarEvolution analysis used.
2614 ScalarEvolution *getSE() const { return &SE; }
2615
2616 /// We need to explicitly define the copy constructor because of FlagsMap.
2618
2619 /// Print the SCEV mappings done by the Predicated Scalar Evolution.
2620 /// The printed text is indented by \p Depth.
2621 LLVM_ABI void print(raw_ostream &OS, unsigned Depth) const;
2622
2623 /// Check if \p AR1 and \p AR2 are equal, while taking into account
2624 /// Equal predicates in Preds.
2626 const SCEVAddRecExpr *AR2) const;
2627
2628private:
2629 /// Increments the version number of the predicate. This needs to be called
2630 /// every time the SCEV predicate changes.
2631 void updateGeneration();
2632
2633 /// Holds a SCEV and the version number of the SCEV predicate used to
2634 /// perform the rewrite of the expression.
2635 using RewriteEntry = std::pair<unsigned, const SCEV *>;
2636
2637 /// Maps a SCEV to the rewrite result of that SCEV at a certain version
2638 /// number. If this number doesn't match the current Generation, we will
2639 /// need to do a rewrite. To preserve the transformation order of previous
2640 /// rewrites, we will rewrite the previous result instead of the original
2641 /// SCEV.
2643
2644 /// Records what NoWrap flags we've added to a Value *.
2646
2647 /// The ScalarEvolution analysis.
2648 ScalarEvolution &SE;
2649
2650 /// The analyzed Loop.
2651 const Loop &L;
2652
2653 /// The SCEVPredicate that forms our context. We will rewrite all
2654 /// expressions assuming that this predicate true.
2655 std::unique_ptr<SCEVUnionPredicate> Preds;
2656
2657 /// Marks the version of the SCEV predicate used. When rewriting a SCEV
2658 /// expression we mark it with the version of the predicate. We use this to
2659 /// figure out if the predicate has changed from the last rewrite of the
2660 /// SCEV. If so, we need to perform a new rewrite.
2661 unsigned Generation = 0;
2662
2663 /// The backedge taken count.
2664 const SCEV *BackedgeCount = nullptr;
2665
2666 /// The symbolic backedge taken count.
2667 const SCEV *SymbolicMaxBackedgeCount = nullptr;
2668
2669 /// The constant max trip count for the loop.
2670 std::optional<unsigned> SmallConstantMaxTripCount;
2671};
2672
2673template <> struct DenseMapInfo<ScalarEvolution::FoldID> {
2676 return ID;
2677 }
2680 return ID;
2681 }
2682
2683 static unsigned getHashValue(const ScalarEvolution::FoldID &Val) {
2684 return Val.computeHash();
2685 }
2686
2689 return LHS == RHS;
2690 }
2691};
2692
2693template <> inline const SCEV *SCEVUseT<const SCEV *>::getCanonical() const {
2694 return Base::getPointer()->getCanonical();
2695}
2696
2697template <typename SCEVPtrT>
2699 Base::getPointer()->print(OS);
2700 SCEV::NoWrapFlags Flags = static_cast<SCEV::NoWrapFlags>(Base::getInt());
2701 if (Flags & SCEV::FlagNUW)
2702 OS << "(u nuw)";
2703 if (Flags & SCEV::FlagNSW)
2704 OS << "(u nsw)";
2705}
2706
2707#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2708template <typename SCEVPtrT>
2710 print(dbgs());
2711 dbgs() << '\n';
2712}
2713#endif
2714
2715} // end namespace llvm
2716
2717#endif // LLVM_ANALYSIS_SCALAREVOLUTION_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
constexpr LLT S1
This file implements a class to represent arbitrary precision integral constant values and operations...
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define X(NUM, ENUM, NAME)
Definition ELF.h:851
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
SmallPtrSet< const BasicBlock *, 8 > VisitedBlocks
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
This file defines a hash set that can be used to remove duplication of nodes in a graph.
Hexagon Common GEP
This header defines various interfaces for pass management in LLVM.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
This file defines the PointerIntPair class.
const SmallVectorImpl< MachineOperand > & Cond
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
Represent the analysis usage information of a pass.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Value handle with callbacks on RAUW and destruction.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This class represents a range of values.
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
FoldingSetNodeIDRef - This class describes a reference to an interned FoldingSetNodeID,...
Definition FoldingSet.h:172
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:209
FunctionPass(char &pid)
Definition Pass.h:316
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags none()
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
Value handle that poisons itself if the Value is deleted.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
LLVM_ABI void addPredicate(const SCEVPredicate &Pred)
Adds a new predicate.
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
LLVM_ABI const SCEVPredicate & getPredicate() const
LLVM_ABI const SCEV * getPredicatedSCEV(const SCEV *Expr)
Returns the rewritten SCEV for Expr in the context of the current SCEV predicate.
LLVM_ABI bool hasNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags)
Returns true if we've proved that V doesn't wrap by means of a SCEV predicate.
LLVM_ABI void setNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags)
Proves that V doesn't overflow by adding SCEV predicate.
LLVM_ABI void print(raw_ostream &OS, unsigned Depth) const
Print the SCEV mappings done by the Predicated Scalar Evolution.
LLVM_ABI bool areAddRecsEqualWithPreds(const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const
Check if AR1 and AR2 are equal, while taking into account Equal predicates in Preds.
LLVM_ABI PredicatedScalarEvolution(ScalarEvolution &SE, Loop &L)
LLVM_ABI const SCEVAddRecExpr * getAsAddRec(Value *V)
Attempts to produce an AddRecExpr for V by adding additional SCEV predicates.
LLVM_ABI unsigned getSmallConstantMaxTripCount()
Returns the upper bound of the loop trip count as a normal unsigned value, or 0 if the trip count is ...
LLVM_ABI const SCEV * getBackedgeTakenCount()
Get the (predicated) backedge count for the analyzed loop.
LLVM_ABI const SCEV * getSymbolicMaxBackedgeTakenCount()
Get the (predicated) symbolic max backedge count for the analyzed loop.
LLVM_ABI const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This node represents a polynomial recurrence on the trip count of the specified loop.
SCEVComparePredicate(const FoldingSetNodeIDRef ID, const ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
const SCEV * getRHS() const
Returns the right hand side of the predicate.
ICmpInst::Predicate getPredicate() const
bool isAlwaysTrue() const override
Returns true if the predicate is always true.
const SCEV * getLHS() const
Returns the left hand side of the predicate.
static bool classof(const SCEVPredicate *P)
Methods for support type inquiry through isa, cast, and dyn_cast:
bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override
Implementation of the SCEVPredicate interface.
This class represents an assumption made using SCEV expressions which can be checked at run-time.
SCEVPredicateKind getKind() const
virtual unsigned getComplexity() const
Returns the estimated complexity of this predicate.
SCEVPredicate & operator=(const SCEVPredicate &)=default
SCEVPredicate(const SCEVPredicate &)=default
virtual bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const =0
Returns true if this predicate implies N.
virtual void print(raw_ostream &OS, unsigned Depth=0) const =0
Prints a textual representation of this predicate with an indentation of Depth.
~SCEVPredicate()=default
virtual bool isAlwaysTrue() const =0
Returns true if the predicate is always true.
SCEVPredicateKind Kind
unsigned getComplexity() const override
We estimate the complexity of a union predicate as the size number of predicates in the union.
SCEVUnionPredicate(ArrayRef< const SCEVPredicate * > Preds, ScalarEvolution &SE)
Union predicates don't get cached so create a dummy set ID for it.
SCEVUnionPredicate getUnionWith(const SCEVPredicate *N, ScalarEvolution &SE) const
Returns a new SCEVUnionPredicate that is the union of this predicate and the given predicate N.
ArrayRef< const SCEVPredicate * > getPredicates() const
static bool classof(const SCEVPredicate *P)
Methods for support type inquiry through isa, cast, and dyn_cast:
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents an assumption made on an AddRec expression.
IncrementWrapFlags
Similar to SCEV::NoWrapFlags, but with slightly different semantics for FlagNUSW.
SCEVWrapPredicate(const FoldingSetNodeIDRef ID, const SCEVAddRecExpr *AR, IncrementWrapFlags Flags)
static SCEVWrapPredicate::IncrementWrapFlags setFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, SCEVWrapPredicate::IncrementWrapFlags OnFlags)
static SCEVWrapPredicate::IncrementWrapFlags clearFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, SCEVWrapPredicate::IncrementWrapFlags OffFlags)
Convenient IncrementWrapFlags manipulation methods.
static bool classof(const SCEVPredicate *P)
Methods for support type inquiry through isa, cast, and dyn_cast:
IncrementWrapFlags getFlags() const
Returns the set assumed no overflow flags.
static SCEVWrapPredicate::IncrementWrapFlags maskFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, int Mask)
This class represents an analyzed expression in the program.
LLVM_ABI const SCEV * getCanonical() const
Return the canonical SCEV.
unsigned short getExpressionSize() const
SCEV & operator=(const SCEV &)=delete
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
LLVM_ABI void computeAndSetCanonical(ScalarEvolution &SE)
Compute and set the canonical SCEV, by constructing a SCEV with the same operands,...
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
SCEV(const SCEV &)=delete
const SCEV * CanonicalSCEV
Pointer to the canonical version of the SCEV, i.e.
LLVM_ABI void dump() const
This method is used for debugging.
LLVM_ABI bool isAllOnesValue() const
Return true if the expression is a constant all-ones value.
LLVM_ABI bool isNonConstantNegative() const
Return true if the specified scev is negated, but not a constant.
LLVM_ABI ArrayRef< SCEVUse > operands() const
Return operands of this SCEV expression.
const unsigned short ExpressionSize
LLVM_ABI void print(raw_ostream &OS) const
Print out the internal representation of this scalar to the specified stream.
SCEV(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, unsigned short ExpressionSize)
SCEVTypes getSCEVType() const
unsigned short SubclassData
This field is initialized to zero and may be used in subclasses to store miscellaneous information.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
Analysis pass that exposes the ScalarEvolution for a function.
LLVM_ABI ScalarEvolution run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Verifier pass for the ScalarEvolutionAnalysis results.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
const ScalarEvolution & getSE() const
bool operator==(const FoldID &RHS) const
FoldID(SCEVTypes C, const SCEV *Op, const Type *Ty)
static LLVM_ABI LoopGuards collect(const Loop *L, ScalarEvolution &SE)
Collect rewrite map for loop guards for loop L, together with flags indicating if NUW and NSW can be ...
LLVM_ABI const SCEV * rewrite(const SCEV *Expr) const
Try to apply the collected loop guards to Expr.
The main scalar evolution driver.
LLVM_ABI const SCEV * getUDivExpr(SCEVUse LHS, SCEVUse RHS)
Get a canonical unsigned division expression, or something simpler if possible.
const SCEV * getConstantMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEVConstant that is greater than or equal to (i.e.
static bool hasFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags TestFlags)
const DataLayout & getDataLayout() const
Return the DataLayout associated with the module this SCEV instance is operating on.
LLVM_ABI bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI bool isKnownOnEveryIteration(CmpPredicate Pred, const SCEVAddRecExpr *LHS, const SCEV *RHS)
Test if the condition described by Pred, LHS, RHS is known to be true on every iteration of the loop ...
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantExitCondDuringFirstIterationsImpl(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI, const SCEV *MaxIter)
LLVM_ABI const SCEV * getUDivCeilSCEV(const SCEV *N, const SCEV *D)
Compute ceil(N / D).
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantExitCondDuringFirstIterations(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI, const SCEV *MaxIter)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L at given Context duri...
LLVM_ABI Type * getWiderType(Type *Ty1, Type *Ty2) const
LLVM_ABI const SCEV * getAbsExpr(const SCEV *Op, bool IsNSW)
LLVM_ABI bool isKnownNonPositive(const SCEV *S)
Test if the given expression is known to be non-positive.
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
LLVM_ABI const SCEV * getPredicatedConstantMaxBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getConstantMaxBackedgeTakenCount, except it will add a set of SCEV predicates to Predicate...
LLVM_ABI const SCEV * removePointerBase(const SCEV *S)
Compute an expression equivalent to S - getPointerBase(S).
LLVM_ABI bool isLoopEntryGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS.
LLVM_ABI bool isKnownNonZero(const SCEV *S)
Test if the given expression is known to be non-zero.
LLVM_ABI const SCEV * getURemExpr(SCEVUse LHS, SCEVUse RHS)
Represents an unsigned remainder expression based on unsigned division.
LLVM_ABI const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
LLVM_ABI const SCEV * getSMinExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI void setNoWrapFlags(SCEVAddRecExpr *AddRec, SCEV::NoWrapFlags Flags)
Update no-wrap flags of an AddRec.
LLVM_ABI const SCEV * getUMaxFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS)
Promote the operands to the wider of the types using zero-extension, and then perform a umax operatio...
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI=nullptr)
Is operation BinOp between LHS and RHS provably does not have a signed/unsigned overflow (Signed)?
LLVM_ABI ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, bool AllowPredicates=false)
Compute the number of times the backedge of the specified loop will execute if its exit condition wer...
LLVM_ABI const SCEV * getZeroExtendExprImpl(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getMinMaxExpr(SCEVTypes Kind, SmallVectorImpl< SCEVUse > &Operands)
LLVM_ABI const SCEVPredicate * getEqualPredicate(const SCEV *LHS, const SCEV *RHS)
LLVM_ABI unsigned getSmallConstantTripMultiple(const Loop *L, const SCEV *ExitCount)
Returns the largest constant divisor of the trip count as a normal unsigned value,...
LLVM_ABI uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getPredicatedBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getBackedgeTakenCount, except it will add a set of SCEV predicates to Predicates that are ...
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
ConstantRange getSignedRange(const SCEV *S)
Determine the signed range for a particular SCEV.
LLVM_ABI const SCEV * getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
LLVM_ABI const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
bool loopHasNoAbnormalExits(const Loop *L)
Return true if the loop has no abnormal exits.
LLVM_ABI const SCEV * getTripCountFromExitCount(const SCEV *ExitCount)
A version of getTripCountFromExitCount below which always picks an evaluation type which can not resu...
LLVM_ABI ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI)
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI const SCEV * getTruncateOrNoop(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI const SCEV * getLosslessPtrToIntExpr(const SCEV *Op)
const SCEV * getMulExpr(SCEVUse Op0, SCEVUse Op1, SCEVUse Op2, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
LLVM_ABI const SCEV * getCastExpr(SCEVTypes Kind, const SCEV *Op, Type *Ty)
LLVM_ABI const SCEV * getSequentialMinMaxExpr(SCEVTypes Kind, SmallVectorImpl< SCEVUse > &Operands)
LLVM_ABI std::optional< bool > evaluatePredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Check whether the condition described by Pred, LHS, and RHS is true or false in the given Context.
LLVM_ABI unsigned getSmallConstantMaxTripCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Returns the upper bound of the loop trip count as a normal unsigned value.
LLVM_ABI const SCEV * getPtrToIntExpr(const SCEV *Op, Type *Ty)
LLVM_ABI bool isBackedgeTakenCountMaxOrZero(const Loop *L)
Return true if the backedge taken count is either the value returned by getConstantMaxBackedgeTakenCo...
LLVM_ABI void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
LLVM_ABI bool SimplifyICmpOperands(CmpPredicate &Pred, SCEVUse &LHS, SCEVUse &RHS, unsigned Depth=0)
Simplify LHS and RHS in a comparison with predicate Pred.
APInt getUnsignedRangeMin(const SCEV *S)
Determine the min of the unsigned range for a particular SCEV.
LLVM_ABI const SCEV * getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo)
Return an expression for offsetof on the given field with type IntTy.
LLVM_ABI LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
Return the "disposition" of the given SCEV with respect to the given loop.
LLVM_ABI bool containsAddRecurrence(const SCEV *S)
Return true if the SCEV is a scAddRecExpr or it contains scAddRecExpr.
LLVM_ABI const SCEV * getSignExtendExprImpl(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool hasOperand(const SCEV *S, const SCEV *Op) const
Test whether the given SCEV has Op as a direct or indirect operand.
LLVM_ABI const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
LLVM_ABI const SCEVPredicate * getComparePredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
LLVM_ABI bool haveSameSign(const SCEV *S1, const SCEV *S2)
Return true if we know that S1 and S2 must have the same sign.
LLVM_ABI const SCEV * getNotSCEV(const SCEV *V)
Return the SCEV object corresponding to ~V.
LLVM_ABI const SCEV * getElementCount(Type *Ty, ElementCount EC, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
LLVM_ABI bool instructionCouldExistWithOperands(const SCEV *A, const SCEV *B)
Return true if there exists a point in the program at which both A and B could be operands to the sam...
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI const SCEV * getPredicatedExitCount(const Loop *L, const BasicBlock *ExitingBlock, SmallVectorImpl< const SCEVPredicate * > *Predicates, ExitCountKind Kind=Exact)
Same as above except this uses the predicated backedge taken info and may require predicates.
static SCEV::NoWrapFlags clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags)
LLVM_ABI void forgetTopmostLoop(const Loop *L)
friend class ScalarEvolutionsTest
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
APInt getSignedRangeMin(const SCEV *S)
Determine the min of the signed range for a particular SCEV.
LLVM_ABI const SCEV * getNoopOrAnyExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
LLVM_ABI const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getUMaxExpr(SCEVUse LHS, SCEVUse RHS)
MonotonicPredicateType
A predicate is said to be monotonically increasing if may go from being false to being true as the lo...
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI=nullptr)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L, return a LoopInvaria...
LLVM_ABI const SCEV * getStoreSizeOfExpr(Type *IntTy, Type *StoreTy)
Return an expression for the store size of StoreTy that is type IntTy.
LLVM_ABI const SCEVPredicate * getWrapPredicate(const SCEVAddRecExpr *AR, SCEVWrapPredicate::IncrementWrapFlags AddedFlags)
LLVM_ABI bool isLoopBackedgeGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether the backedge of the loop is protected by a conditional between LHS and RHS.
LLVM_ABI APInt getNonZeroConstantMultiple(const SCEV *S)
const SCEV * getMinusOne(Type *Ty)
Return a SCEV for the constant -1 of a specific type.
static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags)
LLVM_ABI bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count.
LLVM_ABI BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB)
Return the "disposition" of the given SCEV with respect to the given block.
LLVM_ABI const SCEV * getNoopOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
LLVM_ABI const SCEV * getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS, bool Sequential=false)
Promote the operands to the wider of the types using zero-extension, and then perform a umin operatio...
LLVM_ABI bool loopIsFiniteByAssumption(const Loop *L)
Return true if this loop is finite by assumption.
LLVM_ABI const SCEV * getExistingSCEV(Value *V)
Return an existing SCEV for V if there is one, otherwise return nullptr.
LLVM_ABI APInt getConstantMultiple(const SCEV *S, const Instruction *CtxI=nullptr)
Returns the max constant multiple of S.
LoopDisposition
An enum describing the relationship between a SCEV and a loop.
@ LoopComputable
The SCEV varies predictably with the loop.
@ LoopVariant
The SCEV is loop-variant (unknown).
@ LoopInvariant
The SCEV is loop-invariant.
LLVM_ABI bool isKnownMultipleOf(const SCEV *S, uint64_t M, SmallVectorImpl< const SCEVPredicate * > &Assumptions)
Check that S is a multiple of M.
LLVM_ABI const SCEV * getAnyExtendExpr(const SCEV *Op, Type *Ty)
getAnyExtendExpr - Return a SCEV for the given operand extended with unspecified bits out to the give...
const SCEV * getAddRecExpr(const SmallVectorImpl< SCEVUse > &Operands, const Loop *L, SCEV::NoWrapFlags Flags)
LLVM_ABI bool isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero=false, bool OrNegative=false)
Test if the given expression is known to be a power of 2.
LLVM_ABI std::optional< SCEV::NoWrapFlags > getStrengthenedNoWrapFlagsFromBinOp(const OverflowingBinaryOperator *OBO)
Parse NSW/NUW flags from add/sub/mul IR binary operation Op into SCEV no-wrap flags,...
LLVM_ABI void forgetLcssaPhiWithNewPredecessor(Loop *L, PHINode *V)
Forget LCSSA phi node V of loop L to which a new predecessor was added, such that it may no longer be...
LLVM_ABI bool containsUndefs(const SCEV *S) const
Return true if the SCEV expression contains an undef value.
LLVM_ABI std::optional< MonotonicPredicateType > getMonotonicPredicateType(const SCEVAddRecExpr *LHS, ICmpInst::Predicate Pred)
If, for all loop invariant X, the predicate "LHS `Pred` X" is monotonically increasing or decreasing,...
LLVM_ABI const SCEV * getCouldNotCompute()
LLVM_ABI const SCEV * getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L)
Determine if the SCEV can be evaluated at loop's entry.
LLVM_ABI uint32_t getMinTrailingZeros(const SCEV *S, const Instruction *CtxI=nullptr)
Determine the minimum number of zero bits that S is guaranteed to end in (at every loop iteration).
BlockDisposition
An enum describing the relationship between a SCEV and a basic block.
@ DominatesBlock
The SCEV dominates the block.
@ ProperlyDominatesBlock
The SCEV properly dominates the block.
@ DoesNotDominateBlock
The SCEV does not dominate the block.
LLVM_ABI const SCEV * getExitCount(const Loop *L, const BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
LLVM_ABI const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI void getPoisonGeneratingValues(SmallPtrSetImpl< const Value * > &Result, const SCEV *S)
Return the set of Values that, if poison, will definitively result in S being poison as well.
LLVM_ABI void forgetLoopDispositions()
Called when the client has changed the disposition of values in this loop.
LLVM_ABI const SCEV * getVScale(Type *Ty)
LLVM_ABI unsigned getSmallConstantTripCount(const Loop *L)
Returns the exact trip count of the loop if we can compute it, and the result is a small constant.
LLVM_ABI bool hasComputableLoopEvolution(const SCEV *S, const Loop *L)
Return true if the given SCEV changes value in a known way in the specified loop.
LLVM_ABI const SCEV * getPointerBase(const SCEV *V)
Transitively follow the chain of pointer-type operands until reaching a SCEV that does not have a sin...
const SCEV * getPowerOfTwo(Type *Ty, unsigned Power)
Return a SCEV for the constant Power of two.
LLVM_ABI void forgetAllLoops()
LLVM_ABI bool dominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV dominate the specified basic block.
const SCEV * getAddExpr(SCEVUse Op0, SCEVUse Op1, SCEVUse Op2, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
APInt getUnsignedRangeMax(const SCEV *S)
Determine the max of the unsigned range for a particular SCEV.
LLVM_ABI const SCEV * getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
ExitCountKind
The terms "backedge taken count" and "exit count" are used interchangeably to refer to the number of ...
@ SymbolicMaximum
An expression which provides an upper bound on the exact trip count.
@ ConstantMaximum
A constant which provides an upper bound on the exact trip count.
@ Exact
An expression exactly describing the number of times the backedge has executed when a loop is exited.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
LLVM_ABI const SCEV * getPtrToAddrExpr(const SCEV *Op)
LLVM_ABI const SCEVAddRecExpr * convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Preds)
Tries to convert the S expression to an AddRec expression, adding additional predicates to Preds as r...
LLVM_ABI const SCEV * getSMaxExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI const SCEV * getElementSize(Instruction *Inst)
Return the size of an element read or written by Inst.
LLVM_ABI const SCEV * getSizeOfExpr(Type *IntTy, TypeSize Size)
Return an expression for a TypeSize.
LLVM_ABI std::optional< bool > evaluatePredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Check whether the condition described by Pred, LHS, and RHS is true or false.
LLVM_ABI const SCEV * getUnknown(Value *V)
const SCEV * getAddExpr(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
LLVM_ABI std::optional< std::pair< const SCEV *, SmallVector< const SCEVPredicate *, 3 > > > createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI)
Checks if SymbolicPHI can be rewritten as an AddRecExpr under some Predicates.
LLVM_ABI const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI bool isKnownViaInduction(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS)
We'd like to check the predicate on every iteration of the most dominated loop between loops used in ...
static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags, int Mask)
Convenient NoWrapFlags manipulation that hides enum casts and is visible in the ScalarEvolution name ...
LLVM_ABI std::optional< APInt > computeConstantDifference(const SCEV *LHS, const SCEV *RHS)
Compute LHS - RHS and returns the result as an APInt if it is a constant, and std::nullopt if it isn'...
LLVM_ABI bool properlyDominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV properly dominate the specified basic block.
LLVM_ABI const SCEV * getUDivExactExpr(SCEVUse LHS, SCEVUse RHS)
Get a canonical unsigned division expression, or something simpler if possible.
LLVM_ABI const SCEV * rewriteUsingPredicate(const SCEV *S, const Loop *L, const SCEVPredicate &A)
Re-writes the SCEV according to the Predicates in A.
LLVM_ABI std::pair< const SCEV *, const SCEV * > SplitIntoInitAndPostInc(const Loop *L, const SCEV *S)
Splits SCEV expression S into two SCEVs.
LLVM_ABI bool canReuseInstruction(const SCEV *S, Instruction *I, SmallVectorImpl< Instruction * > &DropPoisonGeneratingInsts)
Check whether it is poison-safe to represent the expression S using the instruction I.
LLVM_ABI bool isKnownPredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
LLVM_ABI const SCEV * getPredicatedSymbolicMaxBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getSymbolicMaxBackedgeTakenCount, except it will add a set of SCEV predicates to Predicate...
LLVM_ABI const SCEV * getGEPExpr(GEPOperator *GEP, ArrayRef< SCEVUse > IndexExprs)
Returns an expression for a GEP.
LLVM_ABI const SCEV * getUMinExpr(SCEVUse LHS, SCEVUse RHS, bool Sequential=false)
LLVM_ABI void registerUser(const SCEV *User, ArrayRef< const SCEV * > Ops)
Notify this ScalarEvolution that User directly uses SCEVs in Ops.
LLVM_ABI bool isBasicBlockEntryGuardedByCond(const BasicBlock *BB, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the basic block is protected by a conditional between LHS and RHS.
LLVM_ABI const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI bool containsErasedValue(const SCEV *S) const
Return true if the SCEV expression contains a Value that has been optimised out and is now a nullptr.
const SCEV * getSymbolicMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEV that is greater than or equal to (i.e.
const SCEV * getMulExpr(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
APInt getSignedRangeMax(const SCEV *S)
Determine the max of the signed range for a particular SCEV.
LLVM_ABI void verify() const
LLVMContext & getContext() const
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:291
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class to represent struct types.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
See the file comment.
Definition ValueMap.h:84
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
unsigned combineHashValue(unsigned a, unsigned b)
Simplistic combination of 32-bit hash values into 32-bit hash values.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
hash_code hash_value(const FixedPointSemantics &Val)
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
FoldingSetBase::Node FoldingSetNode
Definition FoldingSet.h:408
LLVM_ABI bool VerifySCEV
SCEVUseT(SCEVPtrT) -> SCEVUseT< SCEVPtrT >
Deduction guide for various SCEV subclass pointers.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Other
Any other memory.
Definition ModRef.h:68
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
SCEVUseT< const SCEV * > SCEVUse
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
#define N
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
This struct provides a method for customizing the way a cast is performed.
Definition Casting.h:476
static CastReturnType castFailed()
Definition Casting.h:490
static CastReturnType doCast(const From &f)
Definition Casting.h:481
typename cast_retty< To, From >::ret_type CastReturnType
Definition Casting.h:479
static bool isPossible(const From &f)
Definition Casting.h:254
DefaultFoldingSetTrait - This class provides default implementations for FoldingSetTrait implementati...
Definition FoldingSet.h:115
static bool isEqual(const SCEVUse LHS, const SCEVUse RHS)
static unsigned getHashValue(SCEVUse U)
static unsigned getHashValue(const ScalarEvolution::FoldID &Val)
static ScalarEvolution::FoldID getTombstoneKey()
static ScalarEvolution::FoldID getEmptyKey()
static bool isEqual(const ScalarEvolution::FoldID &LHS, const ScalarEvolution::FoldID &RHS)
An information struct used to provide DenseMap with the various necessary components for a given valu...
static void Profile(const SCEVPredicate &X, FoldingSetNodeID &ID)
static bool Equals(const SCEVPredicate &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID)
static unsigned ComputeHash(const SCEVPredicate &X, FoldingSetNodeID &TempID)
static bool Equals(const SCEV &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID)
static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID)
static void Profile(const SCEV &X, FoldingSetNodeID &ID)
FoldingSetTrait - This trait class is used to define behavior of how to "profile" (in the FoldingSet ...
Definition FoldingSet.h:145
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70
static constexpr int NumLowBitsAvailable
The Low bits are used by the PointerIntPair.
static void * getAsVoidPointer(SCEVUse U)
static SCEVUse getFromVoidPointer(void *P)
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
static LLVM_ABI bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
bool operator==(const SCEVUseT &RHS) const
const SCEV * getCanonical() const
Return the canonical SCEV for this SCEVUse.
SCEVPtrT operator->() const
SCEVUseT(const SCEVUseT< OtherPtrT > &Other)
unsigned getFlags() const
SCEVUseT(SCEVPtrT S, unsigned Flags)
bool isCanonical() const
Returns true of the SCEVUse is canonical, i.e.
bool operator==(const SCEV *RHS) const
void dump() const
This method is used for debugging.
PointerIntPair< SCEVPtrT, 2 > Base
void * getRawPointer() const
void print(raw_ostream &OS) const
Print out the internal representation of this scalar to the specified stream.
SCEVUseT(SCEVPtrT S)
Information about the number of loop iterations for which a loop exit's branch condition evaluates to...
LLVM_ABI ExitLimit(const SCEV *E)
Construct either an exact exit limit from a constant, or an unknown one from a SCEVCouldNotCompute.
bool hasAnyInfo() const
Test whether this ExitLimit contains any computed information, or whether it's all SCEVCouldNotComput...
SmallVector< const SCEVPredicate *, 4 > Predicates
A vector of predicate guards for this ExitLimit.
bool hasFullInfo() const
Test whether this ExitLimit contains all information.
LoopInvariantPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
static SimpleType getSimplifiedValue(SCEVUse &Val)
Define a template that can be specialized by smart pointers to reflect the fact that they are automat...
Definition Casting.h:34