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