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