Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / Analysis / ScalarEvolution.h
blobf3a035117b3a907d1854545a7b4c7e4a6ea38df0
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.
15 // This analysis is primarily useful for induction variable substitution and
16 // strength reduction.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
21 #define LLVM_ANALYSIS_SCALAREVOLUTION_H
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/DenseMapInfo.h"
27 #include "llvm/ADT/FoldingSet.h"
28 #include "llvm/ADT/Hashing.h"
29 #include "llvm/ADT/Optional.h"
30 #include "llvm/ADT/PointerIntPair.h"
31 #include "llvm/ADT/SetVector.h"
32 #include "llvm/ADT/SmallPtrSet.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/Analysis/LoopInfo.h"
35 #include "llvm/IR/ConstantRange.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/InstrTypes.h"
38 #include "llvm/IR/Instructions.h"
39 #include "llvm/IR/Operator.h"
40 #include "llvm/IR/PassManager.h"
41 #include "llvm/IR/ValueHandle.h"
42 #include "llvm/IR/ValueMap.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/Allocator.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/Compiler.h"
47 #include <algorithm>
48 #include <cassert>
49 #include <cstdint>
50 #include <memory>
51 #include <utility>
53 namespace llvm {
55 class AssumptionCache;
56 class BasicBlock;
57 class Constant;
58 class ConstantInt;
59 class DataLayout;
60 class DominatorTree;
61 class GEPOperator;
62 class Instruction;
63 class LLVMContext;
64 class raw_ostream;
65 class ScalarEvolution;
66 class SCEVAddRecExpr;
67 class SCEVUnknown;
68 class StructType;
69 class TargetLibraryInfo;
70 class Type;
71 class Value;
73 /// This class represents an analyzed expression in the program. These are
74 /// opaque objects that the client is not allowed to do much with directly.
75 ///
76 class SCEV : public FoldingSetNode {
77 friend struct FoldingSetTrait<SCEV>;
79 /// A reference to an Interned FoldingSetNodeID for this node. The
80 /// ScalarEvolution's BumpPtrAllocator holds the data.
81 FoldingSetNodeIDRef FastID;
83 // The SCEV baseclass this node corresponds to
84 const unsigned short SCEVType;
86 protected:
87 // Estimated complexity of this node's expression tree size.
88 const unsigned short ExpressionSize;
90 /// This field is initialized to zero and may be used in subclasses to store
91 /// miscellaneous information.
92 unsigned short SubclassData = 0;
94 public:
95 /// NoWrapFlags are bitfield indices into SubclassData.
96 ///
97 /// Add and Mul expressions may have no-unsigned-wrap <NUW> or
98 /// no-signed-wrap <NSW> properties, which are derived from the IR
99 /// operator. NSW is a misnomer that we use to mean no signed overflow or
100 /// underflow.
102 /// AddRec expressions may have a no-self-wraparound <NW> property if, in
103 /// the integer domain, abs(step) * max-iteration(loop) <=
104 /// unsigned-max(bitwidth). This means that the recurrence will never reach
105 /// its start value if the step is non-zero. Computing the same value on
106 /// each iteration is not considered wrapping, and recurrences with step = 0
107 /// are trivially <NW>. <NW> is independent of the sign of step and the
108 /// value the add recurrence starts with.
110 /// Note that NUW and NSW are also valid properties of a recurrence, and
111 /// either implies NW. For convenience, NW will be set for a recurrence
112 /// whenever either NUW or NSW are set.
113 enum NoWrapFlags {
114 FlagAnyWrap = 0, // No guarantee.
115 FlagNW = (1 << 0), // No self-wrap.
116 FlagNUW = (1 << 1), // No unsigned wrap.
117 FlagNSW = (1 << 2), // No signed wrap.
118 NoWrapMask = (1 << 3) - 1
121 explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy,
122 unsigned short ExpressionSize)
123 : FastID(ID), SCEVType(SCEVTy), ExpressionSize(ExpressionSize) {}
124 SCEV(const SCEV &) = delete;
125 SCEV &operator=(const SCEV &) = delete;
127 unsigned getSCEVType() const { return SCEVType; }
129 /// Return the LLVM type of this SCEV expression.
130 Type *getType() const;
132 /// Return true if the expression is a constant zero.
133 bool isZero() const;
135 /// Return true if the expression is a constant one.
136 bool isOne() const;
138 /// Return true if the expression is a constant all-ones value.
139 bool isAllOnesValue() const;
141 /// Return true if the specified scev is negated, but not a constant.
142 bool isNonConstantNegative() const;
144 // Returns estimated size of the mathematical expression represented by this
145 // SCEV. The rules of its calculation are following:
146 // 1) Size of a SCEV without operands (like constants and SCEVUnknown) is 1;
147 // 2) Size SCEV with operands Op1, Op2, ..., OpN is calculated by formula:
148 // (1 + Size(Op1) + ... + Size(OpN)).
149 // This value gives us an estimation of time we need to traverse through this
150 // SCEV and all its operands recursively. We may use it to avoid performing
151 // heavy transformations on SCEVs of excessive size for sake of saving the
152 // compilation time.
153 unsigned short getExpressionSize() const {
154 return ExpressionSize;
157 /// Print out the internal representation of this scalar to the specified
158 /// stream. This should really only be used for debugging purposes.
159 void print(raw_ostream &OS) const;
161 /// This method is used for debugging.
162 void dump() const;
165 // Specialize FoldingSetTrait for SCEV to avoid needing to compute
166 // temporary FoldingSetNodeID values.
167 template <> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> {
168 static void Profile(const SCEV &X, FoldingSetNodeID &ID) { ID = X.FastID; }
170 static bool Equals(const SCEV &X, const FoldingSetNodeID &ID, unsigned IDHash,
171 FoldingSetNodeID &TempID) {
172 return ID == X.FastID;
175 static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) {
176 return X.FastID.ComputeHash();
180 inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
181 S.print(OS);
182 return OS;
185 /// An object of this class is returned by queries that could not be answered.
186 /// For example, if you ask for the number of iterations of a linked-list
187 /// traversal loop, you will get one of these. None of the standard SCEV
188 /// operations are valid on this class, it is just a marker.
189 struct SCEVCouldNotCompute : public SCEV {
190 SCEVCouldNotCompute();
192 /// Methods for support type inquiry through isa, cast, and dyn_cast:
193 static bool classof(const SCEV *S);
196 /// This class represents an assumption made using SCEV expressions which can
197 /// be checked at run-time.
198 class SCEVPredicate : public FoldingSetNode {
199 friend struct FoldingSetTrait<SCEVPredicate>;
201 /// A reference to an Interned FoldingSetNodeID for this node. The
202 /// ScalarEvolution's BumpPtrAllocator holds the data.
203 FoldingSetNodeIDRef FastID;
205 public:
206 enum SCEVPredicateKind { P_Union, P_Equal, P_Wrap };
208 protected:
209 SCEVPredicateKind Kind;
210 ~SCEVPredicate() = default;
211 SCEVPredicate(const SCEVPredicate &) = default;
212 SCEVPredicate &operator=(const SCEVPredicate &) = default;
214 public:
215 SCEVPredicate(const FoldingSetNodeIDRef ID, SCEVPredicateKind Kind);
217 SCEVPredicateKind getKind() const { return Kind; }
219 /// Returns the estimated complexity of this predicate. This is roughly
220 /// measured in the number of run-time checks required.
221 virtual unsigned getComplexity() const { return 1; }
223 /// Returns true if the predicate is always true. This means that no
224 /// assumptions were made and nothing needs to be checked at run-time.
225 virtual bool isAlwaysTrue() const = 0;
227 /// Returns true if this predicate implies \p N.
228 virtual bool implies(const SCEVPredicate *N) const = 0;
230 /// Prints a textual representation of this predicate with an indentation of
231 /// \p Depth.
232 virtual void print(raw_ostream &OS, unsigned Depth = 0) const = 0;
234 /// Returns the SCEV to which this predicate applies, or nullptr if this is
235 /// a SCEVUnionPredicate.
236 virtual const SCEV *getExpr() const = 0;
239 inline raw_ostream &operator<<(raw_ostream &OS, const SCEVPredicate &P) {
240 P.print(OS);
241 return OS;
244 // Specialize FoldingSetTrait for SCEVPredicate to avoid needing to compute
245 // temporary FoldingSetNodeID values.
246 template <>
247 struct FoldingSetTrait<SCEVPredicate> : DefaultFoldingSetTrait<SCEVPredicate> {
248 static void Profile(const SCEVPredicate &X, FoldingSetNodeID &ID) {
249 ID = X.FastID;
252 static bool Equals(const SCEVPredicate &X, const FoldingSetNodeID &ID,
253 unsigned IDHash, FoldingSetNodeID &TempID) {
254 return ID == X.FastID;
257 static unsigned ComputeHash(const SCEVPredicate &X,
258 FoldingSetNodeID &TempID) {
259 return X.FastID.ComputeHash();
263 /// This class represents an assumption that two SCEV expressions are equal,
264 /// and this can be checked at run-time.
265 class SCEVEqualPredicate final : public SCEVPredicate {
266 /// We assume that LHS == RHS.
267 const SCEV *LHS;
268 const SCEV *RHS;
270 public:
271 SCEVEqualPredicate(const FoldingSetNodeIDRef ID, const SCEV *LHS,
272 const SCEV *RHS);
274 /// Implementation of the SCEVPredicate interface
275 bool implies(const SCEVPredicate *N) const override;
276 void print(raw_ostream &OS, unsigned Depth = 0) const override;
277 bool isAlwaysTrue() const override;
278 const SCEV *getExpr() const override;
280 /// Returns the left hand side of the equality.
281 const SCEV *getLHS() const { return LHS; }
283 /// Returns the right hand side of the equality.
284 const SCEV *getRHS() const { return RHS; }
286 /// Methods for support type inquiry through isa, cast, and dyn_cast:
287 static bool classof(const SCEVPredicate *P) {
288 return P->getKind() == P_Equal;
292 /// This class represents an assumption made on an AddRec expression. Given an
293 /// affine AddRec expression {a,+,b}, we assume that it has the nssw or nusw
294 /// flags (defined below) in the first X iterations of the loop, where X is a
295 /// SCEV expression returned by getPredicatedBackedgeTakenCount).
297 /// Note that this does not imply that X is equal to the backedge taken
298 /// count. This means that if we have a nusw predicate for i32 {0,+,1} with a
299 /// predicated backedge taken count of X, we only guarantee that {0,+,1} has
300 /// nusw in the first X iterations. {0,+,1} may still wrap in the loop if we
301 /// have more than X iterations.
302 class SCEVWrapPredicate final : public SCEVPredicate {
303 public:
304 /// Similar to SCEV::NoWrapFlags, but with slightly different semantics
305 /// for FlagNUSW. The increment is considered to be signed, and a + b
306 /// (where b is the increment) is considered to wrap if:
307 /// zext(a + b) != zext(a) + sext(b)
309 /// If Signed is a function that takes an n-bit tuple and maps to the
310 /// integer domain as the tuples value interpreted as twos complement,
311 /// and Unsigned a function that takes an n-bit tuple and maps to the
312 /// integer domain as as the base two value of input tuple, then a + b
313 /// has IncrementNUSW iff:
315 /// 0 <= Unsigned(a) + Signed(b) < 2^n
317 /// The IncrementNSSW flag has identical semantics with SCEV::FlagNSW.
319 /// Note that the IncrementNUSW flag is not commutative: if base + inc
320 /// has IncrementNUSW, then inc + base doesn't neccessarily have this
321 /// property. The reason for this is that this is used for sign/zero
322 /// extending affine AddRec SCEV expressions when a SCEVWrapPredicate is
323 /// assumed. A {base,+,inc} expression is already non-commutative with
324 /// regards to base and inc, since it is interpreted as:
325 /// (((base + inc) + inc) + inc) ...
326 enum IncrementWrapFlags {
327 IncrementAnyWrap = 0, // No guarantee.
328 IncrementNUSW = (1 << 0), // No unsigned with signed increment wrap.
329 IncrementNSSW = (1 << 1), // No signed with signed increment wrap
330 // (equivalent with SCEV::NSW)
331 IncrementNoWrapMask = (1 << 2) - 1
334 /// Convenient IncrementWrapFlags manipulation methods.
335 LLVM_NODISCARD static SCEVWrapPredicate::IncrementWrapFlags
336 clearFlags(SCEVWrapPredicate::IncrementWrapFlags Flags,
337 SCEVWrapPredicate::IncrementWrapFlags OffFlags) {
338 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
339 assert((OffFlags & IncrementNoWrapMask) == OffFlags &&
340 "Invalid flags value!");
341 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & ~OffFlags);
344 LLVM_NODISCARD static SCEVWrapPredicate::IncrementWrapFlags
345 maskFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, int Mask) {
346 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
347 assert((Mask & IncrementNoWrapMask) == Mask && "Invalid mask value!");
349 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags & Mask);
352 LLVM_NODISCARD static SCEVWrapPredicate::IncrementWrapFlags
353 setFlags(SCEVWrapPredicate::IncrementWrapFlags Flags,
354 SCEVWrapPredicate::IncrementWrapFlags OnFlags) {
355 assert((Flags & IncrementNoWrapMask) == Flags && "Invalid flags value!");
356 assert((OnFlags & IncrementNoWrapMask) == OnFlags &&
357 "Invalid flags value!");
359 return (SCEVWrapPredicate::IncrementWrapFlags)(Flags | OnFlags);
362 /// Returns the set of SCEVWrapPredicate no wrap flags implied by a
363 /// SCEVAddRecExpr.
364 LLVM_NODISCARD static SCEVWrapPredicate::IncrementWrapFlags
365 getImpliedFlags(const SCEVAddRecExpr *AR, ScalarEvolution &SE);
367 private:
368 const SCEVAddRecExpr *AR;
369 IncrementWrapFlags Flags;
371 public:
372 explicit SCEVWrapPredicate(const FoldingSetNodeIDRef ID,
373 const SCEVAddRecExpr *AR,
374 IncrementWrapFlags Flags);
376 /// Returns the set assumed no overflow flags.
377 IncrementWrapFlags getFlags() const { return Flags; }
379 /// Implementation of the SCEVPredicate interface
380 const SCEV *getExpr() const override;
381 bool implies(const SCEVPredicate *N) const override;
382 void print(raw_ostream &OS, unsigned Depth = 0) const override;
383 bool isAlwaysTrue() const override;
385 /// Methods for support type inquiry through isa, cast, and dyn_cast:
386 static bool classof(const SCEVPredicate *P) {
387 return P->getKind() == P_Wrap;
391 /// This class represents a composition of other SCEV predicates, and is the
392 /// class that most clients will interact with. This is equivalent to a
393 /// logical "AND" of all the predicates in the union.
395 /// NB! Unlike other SCEVPredicate sub-classes this class does not live in the
396 /// ScalarEvolution::Preds folding set. This is why the \c add function is sound.
397 class SCEVUnionPredicate final : public SCEVPredicate {
398 private:
399 using PredicateMap =
400 DenseMap<const SCEV *, SmallVector<const SCEVPredicate *, 4>>;
402 /// Vector with references to all predicates in this union.
403 SmallVector<const SCEVPredicate *, 16> Preds;
405 /// Maps SCEVs to predicates for quick look-ups.
406 PredicateMap SCEVToPreds;
408 public:
409 SCEVUnionPredicate();
411 const SmallVectorImpl<const SCEVPredicate *> &getPredicates() const {
412 return Preds;
415 /// Adds a predicate to this union.
416 void add(const SCEVPredicate *N);
418 /// Returns a reference to a vector containing all predicates which apply to
419 /// \p Expr.
420 ArrayRef<const SCEVPredicate *> getPredicatesForExpr(const SCEV *Expr);
422 /// Implementation of the SCEVPredicate interface
423 bool isAlwaysTrue() const override;
424 bool implies(const SCEVPredicate *N) const override;
425 void print(raw_ostream &OS, unsigned Depth) const override;
426 const SCEV *getExpr() const override;
428 /// We estimate the complexity of a union predicate as the size number of
429 /// predicates in the union.
430 unsigned getComplexity() const override { return Preds.size(); }
432 /// Methods for support type inquiry through isa, cast, and dyn_cast:
433 static bool classof(const SCEVPredicate *P) {
434 return P->getKind() == P_Union;
438 struct ExitLimitQuery {
439 ExitLimitQuery(const Loop *L, BasicBlock *ExitingBlock, bool AllowPredicates)
440 : L(L), ExitingBlock(ExitingBlock), AllowPredicates(AllowPredicates) {}
442 const Loop *L;
443 BasicBlock *ExitingBlock;
444 bool AllowPredicates;
447 template <> struct DenseMapInfo<ExitLimitQuery> {
448 static inline ExitLimitQuery getEmptyKey() {
449 return ExitLimitQuery(nullptr, nullptr, true);
452 static inline ExitLimitQuery getTombstoneKey() {
453 return ExitLimitQuery(nullptr, nullptr, false);
456 static unsigned getHashValue(ExitLimitQuery Val) {
457 return hash_combine(hash_combine(Val.L, Val.ExitingBlock),
458 Val.AllowPredicates);
461 static bool isEqual(ExitLimitQuery LHS, ExitLimitQuery RHS) {
462 return LHS.L == RHS.L && LHS.ExitingBlock == RHS.ExitingBlock &&
463 LHS.AllowPredicates == RHS.AllowPredicates;
467 /// The main scalar evolution driver. Because client code (intentionally)
468 /// can't do much with the SCEV objects directly, they must ask this class
469 /// for services.
470 class ScalarEvolution {
471 public:
472 /// An enum describing the relationship between a SCEV and a loop.
473 enum LoopDisposition {
474 LoopVariant, ///< The SCEV is loop-variant (unknown).
475 LoopInvariant, ///< The SCEV is loop-invariant.
476 LoopComputable ///< The SCEV varies predictably with the loop.
479 /// An enum describing the relationship between a SCEV and a basic block.
480 enum BlockDisposition {
481 DoesNotDominateBlock, ///< The SCEV does not dominate the block.
482 DominatesBlock, ///< The SCEV dominates the block.
483 ProperlyDominatesBlock ///< The SCEV properly dominates the block.
486 /// Convenient NoWrapFlags manipulation that hides enum casts and is
487 /// visible in the ScalarEvolution name space.
488 LLVM_NODISCARD static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags,
489 int Mask) {
490 return (SCEV::NoWrapFlags)(Flags & Mask);
492 LLVM_NODISCARD static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags,
493 SCEV::NoWrapFlags OnFlags) {
494 return (SCEV::NoWrapFlags)(Flags | OnFlags);
496 LLVM_NODISCARD static SCEV::NoWrapFlags
497 clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags) {
498 return (SCEV::NoWrapFlags)(Flags & ~OffFlags);
501 ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC,
502 DominatorTree &DT, LoopInfo &LI);
503 ScalarEvolution(ScalarEvolution &&Arg);
504 ~ScalarEvolution();
506 LLVMContext &getContext() const { return F.getContext(); }
508 /// Test if values of the given type are analyzable within the SCEV
509 /// framework. This primarily includes integer types, and it can optionally
510 /// include pointer types if the ScalarEvolution class has access to
511 /// target-specific information.
512 bool isSCEVable(Type *Ty) const;
514 /// Return the size in bits of the specified type, for which isSCEVable must
515 /// return true.
516 uint64_t getTypeSizeInBits(Type *Ty) const;
518 /// Return a type with the same bitwidth as the given type and which
519 /// represents how SCEV will treat the given type, for which isSCEVable must
520 /// return true. For pointer types, this is the pointer-sized integer type.
521 Type *getEffectiveSCEVType(Type *Ty) const;
523 // Returns a wider type among {Ty1, Ty2}.
524 Type *getWiderType(Type *Ty1, Type *Ty2) const;
526 /// Return true if the SCEV is a scAddRecExpr or it contains
527 /// scAddRecExpr. The result will be cached in HasRecMap.
528 bool containsAddRecurrence(const SCEV *S);
530 /// Erase Value from ValueExprMap and ExprValueMap.
531 void eraseValueFromMap(Value *V);
533 /// Return a SCEV expression for the full generality of the specified
534 /// expression.
535 const SCEV *getSCEV(Value *V);
537 const SCEV *getConstant(ConstantInt *V);
538 const SCEV *getConstant(const APInt &Val);
539 const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false);
540 const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty);
541 const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0);
542 const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0);
543 const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty);
544 const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
545 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
546 unsigned Depth = 0);
547 const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
548 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
549 unsigned Depth = 0) {
550 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
551 return getAddExpr(Ops, Flags, Depth);
553 const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
554 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
555 unsigned Depth = 0) {
556 SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2};
557 return getAddExpr(Ops, Flags, Depth);
559 const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
560 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
561 unsigned Depth = 0);
562 const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
563 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
564 unsigned Depth = 0) {
565 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
566 return getMulExpr(Ops, Flags, Depth);
568 const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
569 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
570 unsigned Depth = 0) {
571 SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2};
572 return getMulExpr(Ops, Flags, Depth);
574 const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
575 const SCEV *getUDivExactExpr(const SCEV *LHS, const SCEV *RHS);
576 const SCEV *getURemExpr(const SCEV *LHS, const SCEV *RHS);
577 const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L,
578 SCEV::NoWrapFlags Flags);
579 const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
580 const Loop *L, SCEV::NoWrapFlags Flags);
581 const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
582 const Loop *L, SCEV::NoWrapFlags Flags) {
583 SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
584 return getAddRecExpr(NewOp, L, Flags);
587 /// Checks if \p SymbolicPHI can be rewritten as an AddRecExpr under some
588 /// Predicates. If successful return these <AddRecExpr, Predicates>;
589 /// The function is intended to be called from PSCEV (the caller will decide
590 /// whether to actually add the predicates and carry out the rewrites).
591 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
592 createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI);
594 /// Returns an expression for a GEP
596 /// \p GEP The GEP. The indices contained in the GEP itself are ignored,
597 /// instead we use IndexExprs.
598 /// \p IndexExprs The expressions for the indices.
599 const SCEV *getGEPExpr(GEPOperator *GEP,
600 const SmallVectorImpl<const SCEV *> &IndexExprs);
601 const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
602 const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
603 const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
604 const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
605 const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
606 const SCEV *getSMinExpr(SmallVectorImpl<const SCEV *> &Operands);
607 const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
608 const SCEV *getUMinExpr(SmallVectorImpl<const SCEV *> &Operands);
609 const SCEV *getUnknown(Value *V);
610 const SCEV *getCouldNotCompute();
612 /// Return a SCEV for the constant 0 of a specific type.
613 const SCEV *getZero(Type *Ty) { return getConstant(Ty, 0); }
615 /// Return a SCEV for the constant 1 of a specific type.
616 const SCEV *getOne(Type *Ty) { return getConstant(Ty, 1); }
618 /// Return an expression for sizeof AllocTy that is type IntTy
619 const SCEV *getSizeOfExpr(Type *IntTy, Type *AllocTy);
621 /// Return an expression for offsetof on the given field with type IntTy
622 const SCEV *getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo);
624 /// Return the SCEV object corresponding to -V.
625 const SCEV *getNegativeSCEV(const SCEV *V,
626 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
628 /// Return the SCEV object corresponding to ~V.
629 const SCEV *getNotSCEV(const SCEV *V);
631 /// Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
632 const SCEV *getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
633 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
634 unsigned Depth = 0);
636 /// Return a SCEV corresponding to a conversion of the input value to the
637 /// specified type. If the type must be extended, it is zero extended.
638 const SCEV *getTruncateOrZeroExtend(const SCEV *V, Type *Ty);
640 /// Return a SCEV corresponding to a conversion of the input value to the
641 /// specified type. If the type must be extended, it is sign extended.
642 const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty);
644 /// Return a SCEV corresponding to a conversion of the input value to the
645 /// specified type. If the type must be extended, it is zero extended. The
646 /// conversion must not be narrowing.
647 const SCEV *getNoopOrZeroExtend(const SCEV *V, Type *Ty);
649 /// Return a SCEV corresponding to a conversion of the input value to the
650 /// specified type. If the type must be extended, it is sign extended. The
651 /// conversion must not be narrowing.
652 const SCEV *getNoopOrSignExtend(const SCEV *V, Type *Ty);
654 /// Return a SCEV corresponding to a conversion of the input value to the
655 /// specified type. If the type must be extended, it is extended with
656 /// unspecified bits. The conversion must not be narrowing.
657 const SCEV *getNoopOrAnyExtend(const SCEV *V, Type *Ty);
659 /// Return a SCEV corresponding to a conversion of the input value to the
660 /// specified type. The conversion must not be widening.
661 const SCEV *getTruncateOrNoop(const SCEV *V, Type *Ty);
663 /// Promote the operands to the wider of the types using zero-extension, and
664 /// then perform a umax operation with them.
665 const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS);
667 /// Promote the operands to the wider of the types using zero-extension, and
668 /// then perform a umin operation with them.
669 const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS);
671 /// Promote the operands to the wider of the types using zero-extension, and
672 /// then perform a umin operation with them. N-ary function.
673 const SCEV *getUMinFromMismatchedTypes(SmallVectorImpl<const SCEV *> &Ops);
675 /// Transitively follow the chain of pointer-type operands until reaching a
676 /// SCEV that does not have a single pointer operand. This returns a
677 /// SCEVUnknown pointer for well-formed pointer-type expressions, but corner
678 /// cases do exist.
679 const SCEV *getPointerBase(const SCEV *V);
681 /// Return a SCEV expression for the specified value at the specified scope
682 /// in the program. The L value specifies a loop nest to evaluate the
683 /// expression at, where null is the top-level or a specified loop is
684 /// immediately inside of the loop.
686 /// This method can be used to compute the exit value for a variable defined
687 /// in a loop by querying what the value will hold in the parent loop.
689 /// In the case that a relevant loop exit value cannot be computed, the
690 /// original value V is returned.
691 const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
693 /// This is a convenience function which does getSCEVAtScope(getSCEV(V), L).
694 const SCEV *getSCEVAtScope(Value *V, const Loop *L);
696 /// Test whether entry to the loop is protected by a conditional between LHS
697 /// and RHS. This is used to help avoid max expressions in loop trip
698 /// counts, and to eliminate casts.
699 bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
700 const SCEV *LHS, const SCEV *RHS);
702 /// Test whether the backedge of the loop is protected by a conditional
703 /// between LHS and RHS. This is used to eliminate casts.
704 bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
705 const SCEV *LHS, const SCEV *RHS);
707 /// Returns the maximum trip count of the loop if it is a single-exit
708 /// loop and we can compute a small maximum for that loop.
710 /// Implemented in terms of the \c getSmallConstantTripCount overload with
711 /// the single exiting block passed to it. See that routine for details.
712 unsigned getSmallConstantTripCount(const Loop *L);
714 /// Returns the maximum trip count of this loop as a normal unsigned
715 /// value. Returns 0 if the trip count is unknown or not constant. This
716 /// "trip count" assumes that control exits via ExitingBlock. More
717 /// precisely, it is the number of times that control may reach ExitingBlock
718 /// before taking the branch. For loops with multiple exits, it may not be
719 /// the number times that the loop header executes if the loop exits
720 /// prematurely via another branch.
721 unsigned getSmallConstantTripCount(const Loop *L, BasicBlock *ExitingBlock);
723 /// Returns the upper bound of the loop trip count as a normal unsigned
724 /// value.
725 /// Returns 0 if the trip count is unknown or not constant.
726 unsigned getSmallConstantMaxTripCount(const Loop *L);
728 /// Returns the largest constant divisor of the trip count of the
729 /// loop if it is a single-exit loop and we can compute a small maximum for
730 /// that loop.
732 /// Implemented in terms of the \c getSmallConstantTripMultiple overload with
733 /// the single exiting block passed to it. See that routine for details.
734 unsigned getSmallConstantTripMultiple(const Loop *L);
736 /// Returns the largest constant divisor of the trip count of this loop as a
737 /// normal unsigned value, if possible. This means that the actual trip
738 /// count is always a multiple of the returned value (don't forget the trip
739 /// count could very well be zero as well!). As explained in the comments
740 /// for getSmallConstantTripCount, this assumes that control exits the loop
741 /// via ExitingBlock.
742 unsigned getSmallConstantTripMultiple(const Loop *L,
743 BasicBlock *ExitingBlock);
745 /// Get the expression for the number of loop iterations for which this loop
746 /// is guaranteed not to exit via ExitingBlock. Otherwise return
747 /// SCEVCouldNotCompute.
748 const SCEV *getExitCount(const Loop *L, BasicBlock *ExitingBlock);
750 /// If the specified loop has a predictable backedge-taken count, return it,
751 /// otherwise return a SCEVCouldNotCompute object. The backedge-taken count is
752 /// the number of times the loop header will be branched to from within the
753 /// loop, assuming there are no abnormal exists like exception throws. This is
754 /// one less than the trip count of the loop, since it doesn't count the first
755 /// iteration, when the header is branched to from outside the loop.
757 /// Note that it is not valid to call this method on a loop without a
758 /// loop-invariant backedge-taken count (see
759 /// hasLoopInvariantBackedgeTakenCount).
760 const SCEV *getBackedgeTakenCount(const Loop *L);
762 /// Similar to getBackedgeTakenCount, except it will add a set of
763 /// SCEV predicates to Predicates that are required to be true in order for
764 /// the answer to be correct. Predicates can be checked with run-time
765 /// checks and can be used to perform loop versioning.
766 const SCEV *getPredicatedBackedgeTakenCount(const Loop *L,
767 SCEVUnionPredicate &Predicates);
769 /// When successful, this returns a SCEVConstant that is greater than or equal
770 /// to (i.e. a "conservative over-approximation") of the value returend by
771 /// getBackedgeTakenCount. If such a value cannot be computed, it returns the
772 /// SCEVCouldNotCompute object.
773 const SCEV *getMaxBackedgeTakenCount(const Loop *L);
775 /// Return true if the backedge taken count is either the value returned by
776 /// getMaxBackedgeTakenCount or zero.
777 bool isBackedgeTakenCountMaxOrZero(const Loop *L);
779 /// Return true if the specified loop has an analyzable loop-invariant
780 /// backedge-taken count.
781 bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
783 /// This method should be called by the client when it has changed a loop in
784 /// a way that may effect ScalarEvolution's ability to compute a trip count,
785 /// or if the loop is deleted. This call is potentially expensive for large
786 /// loop bodies.
787 void forgetLoop(const Loop *L);
789 // This method invokes forgetLoop for the outermost loop of the given loop
790 // \p L, making ScalarEvolution forget about all this subtree. This needs to
791 // be done whenever we make a transform that may affect the parameters of the
792 // outer loop, such as exit counts for branches.
793 void forgetTopmostLoop(const Loop *L);
795 /// This method should be called by the client when it has changed a value
796 /// in a way that may effect its value, or which may disconnect it from a
797 /// def-use chain linking it to a loop.
798 void forgetValue(Value *V);
800 /// Called when the client has changed the disposition of values in
801 /// this loop.
803 /// We don't have a way to invalidate per-loop dispositions. Clear and
804 /// recompute is simpler.
805 void forgetLoopDispositions(const Loop *L) { LoopDispositions.clear(); }
807 /// Determine the minimum number of zero bits that S is guaranteed to end in
808 /// (at every loop iteration). It is, at the same time, the minimum number
809 /// of times S is divisible by 2. For example, given {4,+,8} it returns 2.
810 /// If S is guaranteed to be 0, it returns the bitwidth of S.
811 uint32_t GetMinTrailingZeros(const SCEV *S);
813 /// Determine the unsigned range for a particular SCEV.
814 /// NOTE: This returns a copy of the reference returned by getRangeRef.
815 ConstantRange getUnsignedRange(const SCEV *S) {
816 return getRangeRef(S, HINT_RANGE_UNSIGNED);
819 /// Determine the min of the unsigned range for a particular SCEV.
820 APInt getUnsignedRangeMin(const SCEV *S) {
821 return getRangeRef(S, HINT_RANGE_UNSIGNED).getUnsignedMin();
824 /// Determine the max of the unsigned range for a particular SCEV.
825 APInt getUnsignedRangeMax(const SCEV *S) {
826 return getRangeRef(S, HINT_RANGE_UNSIGNED).getUnsignedMax();
829 /// Determine the signed range for a particular SCEV.
830 /// NOTE: This returns a copy of the reference returned by getRangeRef.
831 ConstantRange getSignedRange(const SCEV *S) {
832 return getRangeRef(S, HINT_RANGE_SIGNED);
835 /// Determine the min of the signed range for a particular SCEV.
836 APInt getSignedRangeMin(const SCEV *S) {
837 return getRangeRef(S, HINT_RANGE_SIGNED).getSignedMin();
840 /// Determine the max of the signed range for a particular SCEV.
841 APInt getSignedRangeMax(const SCEV *S) {
842 return getRangeRef(S, HINT_RANGE_SIGNED).getSignedMax();
845 /// Test if the given expression is known to be negative.
846 bool isKnownNegative(const SCEV *S);
848 /// Test if the given expression is known to be positive.
849 bool isKnownPositive(const SCEV *S);
851 /// Test if the given expression is known to be non-negative.
852 bool isKnownNonNegative(const SCEV *S);
854 /// Test if the given expression is known to be non-positive.
855 bool isKnownNonPositive(const SCEV *S);
857 /// Test if the given expression is known to be non-zero.
858 bool isKnownNonZero(const SCEV *S);
860 /// Splits SCEV expression \p S into two SCEVs. One of them is obtained from
861 /// \p S by substitution of all AddRec sub-expression related to loop \p L
862 /// with initial value of that SCEV. The second is obtained from \p S by
863 /// substitution of all AddRec sub-expressions related to loop \p L with post
864 /// increment of this AddRec in the loop \p L. In both cases all other AddRec
865 /// sub-expressions (not related to \p L) remain the same.
866 /// If the \p S contains non-invariant unknown SCEV the function returns
867 /// CouldNotCompute SCEV in both values of std::pair.
868 /// For example, for SCEV S={0, +, 1}<L1> + {0, +, 1}<L2> and loop L=L1
869 /// the function returns pair:
870 /// first = {0, +, 1}<L2>
871 /// second = {1, +, 1}<L1> + {0, +, 1}<L2>
872 /// We can see that for the first AddRec sub-expression it was replaced with
873 /// 0 (initial value) for the first element and to {1, +, 1}<L1> (post
874 /// increment value) for the second one. In both cases AddRec expression
875 /// related to L2 remains the same.
876 std::pair<const SCEV *, const SCEV *> SplitIntoInitAndPostInc(const Loop *L,
877 const SCEV *S);
879 /// We'd like to check the predicate on every iteration of the most dominated
880 /// loop between loops used in LHS and RHS.
881 /// To do this we use the following list of steps:
882 /// 1. Collect set S all loops on which either LHS or RHS depend.
883 /// 2. If S is non-empty
884 /// a. Let PD be the element of S which is dominated by all other elements.
885 /// b. Let E(LHS) be value of LHS on entry of PD.
886 /// To get E(LHS), we should just take LHS and replace all AddRecs that are
887 /// attached to PD on with their entry values.
888 /// Define E(RHS) in the same way.
889 /// c. Let B(LHS) be value of L on backedge of PD.
890 /// To get B(LHS), we should just take LHS and replace all AddRecs that are
891 /// attached to PD on with their backedge values.
892 /// Define B(RHS) in the same way.
893 /// d. Note that E(LHS) and E(RHS) are automatically available on entry of PD,
894 /// so we can assert on that.
895 /// e. Return true if isLoopEntryGuardedByCond(Pred, E(LHS), E(RHS)) &&
896 /// isLoopBackedgeGuardedByCond(Pred, B(LHS), B(RHS))
897 bool isKnownViaInduction(ICmpInst::Predicate Pred, const SCEV *LHS,
898 const SCEV *RHS);
900 /// Test if the given expression is known to satisfy the condition described
901 /// by Pred, LHS, and RHS.
902 bool isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
903 const SCEV *RHS);
905 /// Test if the condition described by Pred, LHS, RHS is known to be true on
906 /// every iteration of the loop of the recurrency LHS.
907 bool isKnownOnEveryIteration(ICmpInst::Predicate Pred,
908 const SCEVAddRecExpr *LHS, const SCEV *RHS);
910 /// Return true if, for all loop invariant X, the predicate "LHS `Pred` X"
911 /// is monotonically increasing or decreasing. In the former case set
912 /// `Increasing` to true and in the latter case set `Increasing` to false.
914 /// A predicate is said to be monotonically increasing if may go from being
915 /// false to being true as the loop iterates, but never the other way
916 /// around. A predicate is said to be monotonically decreasing if may go
917 /// from being true to being false as the loop iterates, but never the other
918 /// way around.
919 bool isMonotonicPredicate(const SCEVAddRecExpr *LHS, ICmpInst::Predicate Pred,
920 bool &Increasing);
922 /// Return true if the result of the predicate LHS `Pred` RHS is loop
923 /// invariant with respect to L. Set InvariantPred, InvariantLHS and
924 /// InvariantLHS so that InvariantLHS `InvariantPred` InvariantRHS is the
925 /// loop invariant form of LHS `Pred` RHS.
926 bool isLoopInvariantPredicate(ICmpInst::Predicate Pred, const SCEV *LHS,
927 const SCEV *RHS, const Loop *L,
928 ICmpInst::Predicate &InvariantPred,
929 const SCEV *&InvariantLHS,
930 const SCEV *&InvariantRHS);
932 /// Simplify LHS and RHS in a comparison with predicate Pred. Return true
933 /// iff any changes were made. If the operands are provably equal or
934 /// unequal, LHS and RHS are set to the same value and Pred is set to either
935 /// ICMP_EQ or ICMP_NE.
936 bool SimplifyICmpOperands(ICmpInst::Predicate &Pred, const SCEV *&LHS,
937 const SCEV *&RHS, unsigned Depth = 0);
939 /// Return the "disposition" of the given SCEV with respect to the given
940 /// loop.
941 LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L);
943 /// Return true if the value of the given SCEV is unchanging in the
944 /// specified loop.
945 bool isLoopInvariant(const SCEV *S, const Loop *L);
947 /// Determine if the SCEV can be evaluated at loop's entry. It is true if it
948 /// doesn't depend on a SCEVUnknown of an instruction which is dominated by
949 /// the header of loop L.
950 bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L);
952 /// Return true if the given SCEV changes value in a known way in the
953 /// specified loop. This property being true implies that the value is
954 /// variant in the loop AND that we can emit an expression to compute the
955 /// value of the expression at any particular loop iteration.
956 bool hasComputableLoopEvolution(const SCEV *S, const Loop *L);
958 /// Return the "disposition" of the given SCEV with respect to the given
959 /// block.
960 BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB);
962 /// Return true if elements that makes up the given SCEV dominate the
963 /// specified basic block.
964 bool dominates(const SCEV *S, const BasicBlock *BB);
966 /// Return true if elements that makes up the given SCEV properly dominate
967 /// the specified basic block.
968 bool properlyDominates(const SCEV *S, const BasicBlock *BB);
970 /// Test whether the given SCEV has Op as a direct or indirect operand.
971 bool hasOperand(const SCEV *S, const SCEV *Op) const;
973 /// Return the size of an element read or written by Inst.
974 const SCEV *getElementSize(Instruction *Inst);
976 /// Compute the array dimensions Sizes from the set of Terms extracted from
977 /// the memory access function of this SCEVAddRecExpr (second step of
978 /// delinearization).
979 void findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
980 SmallVectorImpl<const SCEV *> &Sizes,
981 const SCEV *ElementSize);
983 void print(raw_ostream &OS) const;
984 void verify() const;
985 bool invalidate(Function &F, const PreservedAnalyses &PA,
986 FunctionAnalysisManager::Invalidator &Inv);
988 /// Collect parametric terms occurring in step expressions (first step of
989 /// delinearization).
990 void collectParametricTerms(const SCEV *Expr,
991 SmallVectorImpl<const SCEV *> &Terms);
993 /// Return in Subscripts the access functions for each dimension in Sizes
994 /// (third step of delinearization).
995 void computeAccessFunctions(const SCEV *Expr,
996 SmallVectorImpl<const SCEV *> &Subscripts,
997 SmallVectorImpl<const SCEV *> &Sizes);
999 /// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
1000 /// subscripts and sizes of an array access.
1002 /// The delinearization is a 3 step process: the first two steps compute the
1003 /// sizes of each subscript and the third step computes the access functions
1004 /// for the delinearized array:
1006 /// 1. Find the terms in the step functions
1007 /// 2. Compute the array size
1008 /// 3. Compute the access function: divide the SCEV by the array size
1009 /// starting with the innermost dimensions found in step 2. The Quotient
1010 /// is the SCEV to be divided in the next step of the recursion. The
1011 /// Remainder is the subscript of the innermost dimension. Loop over all
1012 /// array dimensions computed in step 2.
1014 /// To compute a uniform array size for several memory accesses to the same
1015 /// object, one can collect in step 1 all the step terms for all the memory
1016 /// accesses, and compute in step 2 a unique array shape. This guarantees
1017 /// that the array shape will be the same across all memory accesses.
1019 /// FIXME: We could derive the result of steps 1 and 2 from a description of
1020 /// the array shape given in metadata.
1022 /// Example:
1024 /// A[][n][m]
1026 /// for i
1027 /// for j
1028 /// for k
1029 /// A[j+k][2i][5i] =
1031 /// The initial SCEV:
1033 /// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k]
1035 /// 1. Find the different terms in the step functions:
1036 /// -> [2*m, 5, n*m, n*m]
1038 /// 2. Compute the array size: sort and unique them
1039 /// -> [n*m, 2*m, 5]
1040 /// find the GCD of all the terms = 1
1041 /// divide by the GCD and erase constant terms
1042 /// -> [n*m, 2*m]
1043 /// GCD = m
1044 /// divide by GCD -> [n, 2]
1045 /// remove constant terms
1046 /// -> [n]
1047 /// size of the array is A[unknown][n][m]
1049 /// 3. Compute the access function
1050 /// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m
1051 /// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k
1052 /// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k
1053 /// The remainder is the subscript of the innermost array dimension: [5i].
1055 /// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n
1056 /// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k
1057 /// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k
1058 /// The Remainder is the subscript of the next array dimension: [2i].
1060 /// The subscript of the outermost dimension is the Quotient: [j+k].
1062 /// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
1063 void delinearize(const SCEV *Expr, SmallVectorImpl<const SCEV *> &Subscripts,
1064 SmallVectorImpl<const SCEV *> &Sizes,
1065 const SCEV *ElementSize);
1067 /// Return the DataLayout associated with the module this SCEV instance is
1068 /// operating on.
1069 const DataLayout &getDataLayout() const {
1070 return F.getParent()->getDataLayout();
1073 const SCEVPredicate *getEqualPredicate(const SCEV *LHS, const SCEV *RHS);
1075 const SCEVPredicate *
1076 getWrapPredicate(const SCEVAddRecExpr *AR,
1077 SCEVWrapPredicate::IncrementWrapFlags AddedFlags);
1079 /// Re-writes the SCEV according to the Predicates in \p A.
1080 const SCEV *rewriteUsingPredicate(const SCEV *S, const Loop *L,
1081 SCEVUnionPredicate &A);
1082 /// Tries to convert the \p S expression to an AddRec expression,
1083 /// adding additional predicates to \p Preds as required.
1084 const SCEVAddRecExpr *convertSCEVToAddRecWithPredicates(
1085 const SCEV *S, const Loop *L,
1086 SmallPtrSetImpl<const SCEVPredicate *> &Preds);
1088 private:
1089 /// A CallbackVH to arrange for ScalarEvolution to be notified whenever a
1090 /// Value is deleted.
1091 class SCEVCallbackVH final : public CallbackVH {
1092 ScalarEvolution *SE;
1094 void deleted() override;
1095 void allUsesReplacedWith(Value *New) override;
1097 public:
1098 SCEVCallbackVH(Value *V, ScalarEvolution *SE = nullptr);
1101 friend class SCEVCallbackVH;
1102 friend class SCEVExpander;
1103 friend class SCEVUnknown;
1105 /// The function we are analyzing.
1106 Function &F;
1108 /// Does the module have any calls to the llvm.experimental.guard intrinsic
1109 /// at all? If this is false, we avoid doing work that will only help if
1110 /// thare are guards present in the IR.
1111 bool HasGuards;
1113 /// The target library information for the target we are targeting.
1114 TargetLibraryInfo &TLI;
1116 /// The tracker for \@llvm.assume intrinsics in this function.
1117 AssumptionCache &AC;
1119 /// The dominator tree.
1120 DominatorTree &DT;
1122 /// The loop information for the function we are currently analyzing.
1123 LoopInfo &LI;
1125 /// This SCEV is used to represent unknown trip counts and things.
1126 std::unique_ptr<SCEVCouldNotCompute> CouldNotCompute;
1128 /// The type for HasRecMap.
1129 using HasRecMapType = DenseMap<const SCEV *, bool>;
1131 /// This is a cache to record whether a SCEV contains any scAddRecExpr.
1132 HasRecMapType HasRecMap;
1134 /// The type for ExprValueMap.
1135 using ValueOffsetPair = std::pair<Value *, ConstantInt *>;
1136 using ExprValueMapType = DenseMap<const SCEV *, SetVector<ValueOffsetPair>>;
1138 /// ExprValueMap -- This map records the original values from which
1139 /// the SCEV expr is generated from.
1141 /// We want to represent the mapping as SCEV -> ValueOffsetPair instead
1142 /// of SCEV -> Value:
1143 /// Suppose we know S1 expands to V1, and
1144 /// S1 = S2 + C_a
1145 /// S3 = S2 + C_b
1146 /// where C_a and C_b are different SCEVConstants. Then we'd like to
1147 /// expand S3 as V1 - C_a + C_b instead of expanding S2 literally.
1148 /// It is helpful when S2 is a complex SCEV expr.
1150 /// In order to do that, we represent ExprValueMap as a mapping from
1151 /// SCEV to ValueOffsetPair. We will save both S1->{V1, 0} and
1152 /// S2->{V1, C_a} into the map when we create SCEV for V1. When S3
1153 /// is expanded, it will first expand S2 to V1 - C_a because of
1154 /// S2->{V1, C_a} in the map, then expand S3 to V1 - C_a + C_b.
1156 /// Note: S->{V, Offset} in the ExprValueMap means S can be expanded
1157 /// to V - Offset.
1158 ExprValueMapType ExprValueMap;
1160 /// The type for ValueExprMap.
1161 using ValueExprMapType =
1162 DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *>>;
1164 /// This is a cache of the values we have analyzed so far.
1165 ValueExprMapType ValueExprMap;
1167 /// Mark predicate values currently being processed by isImpliedCond.
1168 SmallPtrSet<Value *, 6> PendingLoopPredicates;
1170 /// Mark SCEVUnknown Phis currently being processed by getRangeRef.
1171 SmallPtrSet<const PHINode *, 6> PendingPhiRanges;
1173 // Mark SCEVUnknown Phis currently being processed by isImpliedViaMerge.
1174 SmallPtrSet<const PHINode *, 6> PendingMerges;
1176 /// Set to true by isLoopBackedgeGuardedByCond when we're walking the set of
1177 /// conditions dominating the backedge of a loop.
1178 bool WalkingBEDominatingConds = false;
1180 /// Set to true by isKnownPredicateViaSplitting when we're trying to prove a
1181 /// predicate by splitting it into a set of independent predicates.
1182 bool ProvingSplitPredicate = false;
1184 /// Memoized values for the GetMinTrailingZeros
1185 DenseMap<const SCEV *, uint32_t> MinTrailingZerosCache;
1187 /// Return the Value set from which the SCEV expr is generated.
1188 SetVector<ValueOffsetPair> *getSCEVValues(const SCEV *S);
1190 /// Private helper method for the GetMinTrailingZeros method
1191 uint32_t GetMinTrailingZerosImpl(const SCEV *S);
1193 /// Information about the number of loop iterations for which a loop exit's
1194 /// branch condition evaluates to the not-taken path. This is a temporary
1195 /// pair of exact and max expressions that are eventually summarized in
1196 /// ExitNotTakenInfo and BackedgeTakenInfo.
1197 struct ExitLimit {
1198 const SCEV *ExactNotTaken; // The exit is not taken exactly this many times
1199 const SCEV *MaxNotTaken; // The exit is not taken at most this many times
1201 // Not taken either exactly MaxNotTaken or zero times
1202 bool MaxOrZero = false;
1204 /// A set of predicate guards for this ExitLimit. The result is only valid
1205 /// if all of the predicates in \c Predicates evaluate to 'true' at
1206 /// run-time.
1207 SmallPtrSet<const SCEVPredicate *, 4> Predicates;
1209 void addPredicate(const SCEVPredicate *P) {
1210 assert(!isa<SCEVUnionPredicate>(P) && "Only add leaf predicates here!");
1211 Predicates.insert(P);
1214 /*implicit*/ ExitLimit(const SCEV *E);
1216 ExitLimit(
1217 const SCEV *E, const SCEV *M, bool MaxOrZero,
1218 ArrayRef<const SmallPtrSetImpl<const SCEVPredicate *> *> PredSetList);
1220 ExitLimit(const SCEV *E, const SCEV *M, bool MaxOrZero,
1221 const SmallPtrSetImpl<const SCEVPredicate *> &PredSet);
1223 ExitLimit(const SCEV *E, const SCEV *M, bool MaxOrZero);
1225 /// Test whether this ExitLimit contains any computed information, or
1226 /// whether it's all SCEVCouldNotCompute values.
1227 bool hasAnyInfo() const {
1228 return !isa<SCEVCouldNotCompute>(ExactNotTaken) ||
1229 !isa<SCEVCouldNotCompute>(MaxNotTaken);
1232 bool hasOperand(const SCEV *S) const;
1234 /// Test whether this ExitLimit contains all information.
1235 bool hasFullInfo() const {
1236 return !isa<SCEVCouldNotCompute>(ExactNotTaken);
1240 /// Information about the number of times a particular loop exit may be
1241 /// reached before exiting the loop.
1242 struct ExitNotTakenInfo {
1243 PoisoningVH<BasicBlock> ExitingBlock;
1244 const SCEV *ExactNotTaken;
1245 std::unique_ptr<SCEVUnionPredicate> Predicate;
1247 explicit ExitNotTakenInfo(PoisoningVH<BasicBlock> ExitingBlock,
1248 const SCEV *ExactNotTaken,
1249 std::unique_ptr<SCEVUnionPredicate> Predicate)
1250 : ExitingBlock(ExitingBlock), ExactNotTaken(ExactNotTaken),
1251 Predicate(std::move(Predicate)) {}
1253 bool hasAlwaysTruePredicate() const {
1254 return !Predicate || Predicate->isAlwaysTrue();
1258 /// Information about the backedge-taken count of a loop. This currently
1259 /// includes an exact count and a maximum count.
1261 class BackedgeTakenInfo {
1262 /// A list of computable exits and their not-taken counts. Loops almost
1263 /// never have more than one computable exit.
1264 SmallVector<ExitNotTakenInfo, 1> ExitNotTaken;
1266 /// The pointer part of \c MaxAndComplete is an expression indicating the
1267 /// least maximum backedge-taken count of the loop that is known, or a
1268 /// SCEVCouldNotCompute. This expression is only valid if the predicates
1269 /// associated with all loop exits are true.
1271 /// The integer part of \c MaxAndComplete is a boolean indicating if \c
1272 /// ExitNotTaken has an element for every exiting block in the loop.
1273 PointerIntPair<const SCEV *, 1> MaxAndComplete;
1275 /// True iff the backedge is taken either exactly Max or zero times.
1276 bool MaxOrZero = false;
1278 /// \name Helper projection functions on \c MaxAndComplete.
1279 /// @{
1280 bool isComplete() const { return MaxAndComplete.getInt(); }
1281 const SCEV *getMax() const { return MaxAndComplete.getPointer(); }
1282 /// @}
1284 public:
1285 BackedgeTakenInfo() : MaxAndComplete(nullptr, 0) {}
1286 BackedgeTakenInfo(BackedgeTakenInfo &&) = default;
1287 BackedgeTakenInfo &operator=(BackedgeTakenInfo &&) = default;
1289 using EdgeExitInfo = std::pair<BasicBlock *, ExitLimit>;
1291 /// Initialize BackedgeTakenInfo from a list of exact exit counts.
1292 BackedgeTakenInfo(ArrayRef<EdgeExitInfo> ExitCounts, bool Complete,
1293 const SCEV *MaxCount, bool MaxOrZero);
1295 /// Test whether this BackedgeTakenInfo contains any computed information,
1296 /// or whether it's all SCEVCouldNotCompute values.
1297 bool hasAnyInfo() const {
1298 return !ExitNotTaken.empty() || !isa<SCEVCouldNotCompute>(getMax());
1301 /// Test whether this BackedgeTakenInfo contains complete information.
1302 bool hasFullInfo() const { return isComplete(); }
1304 /// Return an expression indicating the exact *backedge-taken*
1305 /// count of the loop if it is known or SCEVCouldNotCompute
1306 /// otherwise. If execution makes it to the backedge on every
1307 /// iteration (i.e. there are no abnormal exists like exception
1308 /// throws and thread exits) then this is the number of times the
1309 /// loop header will execute minus one.
1311 /// If the SCEV predicate associated with the answer can be different
1312 /// from AlwaysTrue, we must add a (non null) Predicates argument.
1313 /// The SCEV predicate associated with the answer will be added to
1314 /// Predicates. A run-time check needs to be emitted for the SCEV
1315 /// predicate in order for the answer to be valid.
1317 /// Note that we should always know if we need to pass a predicate
1318 /// argument or not from the way the ExitCounts vector was computed.
1319 /// If we allowed SCEV predicates to be generated when populating this
1320 /// vector, this information can contain them and therefore a
1321 /// SCEVPredicate argument should be added to getExact.
1322 const SCEV *getExact(const Loop *L, ScalarEvolution *SE,
1323 SCEVUnionPredicate *Predicates = nullptr) const;
1325 /// Return the number of times this loop exit may fall through to the back
1326 /// edge, or SCEVCouldNotCompute. The loop is guaranteed not to exit via
1327 /// this block before this number of iterations, but may exit via another
1328 /// block.
1329 const SCEV *getExact(BasicBlock *ExitingBlock, ScalarEvolution *SE) const;
1331 /// Get the max backedge taken count for the loop.
1332 const SCEV *getMax(ScalarEvolution *SE) const;
1334 /// Return true if the number of times this backedge is taken is either the
1335 /// value returned by getMax or zero.
1336 bool isMaxOrZero(ScalarEvolution *SE) const;
1338 /// Return true if any backedge taken count expressions refer to the given
1339 /// subexpression.
1340 bool hasOperand(const SCEV *S, ScalarEvolution *SE) const;
1342 /// Invalidate this result and free associated memory.
1343 void clear();
1346 /// Cache the backedge-taken count of the loops for this function as they
1347 /// are computed.
1348 DenseMap<const Loop *, BackedgeTakenInfo> BackedgeTakenCounts;
1350 /// Cache the predicated backedge-taken count of the loops for this
1351 /// function as they are computed.
1352 DenseMap<const Loop *, BackedgeTakenInfo> PredicatedBackedgeTakenCounts;
1354 /// This map contains entries for all of the PHI instructions that we
1355 /// attempt to compute constant evolutions for. This allows us to avoid
1356 /// potentially expensive recomputation of these properties. An instruction
1357 /// maps to null if we are unable to compute its exit value.
1358 DenseMap<PHINode *, Constant *> ConstantEvolutionLoopExitValue;
1360 /// This map contains entries for all the expressions that we attempt to
1361 /// compute getSCEVAtScope information for, which can be expensive in
1362 /// extreme cases.
1363 DenseMap<const SCEV *, SmallVector<std::pair<const Loop *, const SCEV *>, 2>>
1364 ValuesAtScopes;
1366 /// Memoized computeLoopDisposition results.
1367 DenseMap<const SCEV *,
1368 SmallVector<PointerIntPair<const Loop *, 2, LoopDisposition>, 2>>
1369 LoopDispositions;
1371 struct LoopProperties {
1372 /// Set to true if the loop contains no instruction that can have side
1373 /// effects (i.e. via throwing an exception, volatile or atomic access).
1374 bool HasNoAbnormalExits;
1376 /// Set to true if the loop contains no instruction that can abnormally exit
1377 /// the loop (i.e. via throwing an exception, by terminating the thread
1378 /// cleanly or by infinite looping in a called function). Strictly
1379 /// speaking, the last one is not leaving the loop, but is identical to
1380 /// leaving the loop for reasoning about undefined behavior.
1381 bool HasNoSideEffects;
1384 /// Cache for \c getLoopProperties.
1385 DenseMap<const Loop *, LoopProperties> LoopPropertiesCache;
1387 /// Return a \c LoopProperties instance for \p L, creating one if necessary.
1388 LoopProperties getLoopProperties(const Loop *L);
1390 bool loopHasNoSideEffects(const Loop *L) {
1391 return getLoopProperties(L).HasNoSideEffects;
1394 bool loopHasNoAbnormalExits(const Loop *L) {
1395 return getLoopProperties(L).HasNoAbnormalExits;
1398 /// Compute a LoopDisposition value.
1399 LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L);
1401 /// Memoized computeBlockDisposition results.
1402 DenseMap<
1403 const SCEV *,
1404 SmallVector<PointerIntPair<const BasicBlock *, 2, BlockDisposition>, 2>>
1405 BlockDispositions;
1407 /// Compute a BlockDisposition value.
1408 BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB);
1410 /// Memoized results from getRange
1411 DenseMap<const SCEV *, ConstantRange> UnsignedRanges;
1413 /// Memoized results from getRange
1414 DenseMap<const SCEV *, ConstantRange> SignedRanges;
1416 /// Used to parameterize getRange
1417 enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED };
1419 /// Set the memoized range for the given SCEV.
1420 const ConstantRange &setRange(const SCEV *S, RangeSignHint Hint,
1421 ConstantRange CR) {
1422 DenseMap<const SCEV *, ConstantRange> &Cache =
1423 Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges;
1425 auto Pair = Cache.try_emplace(S, std::move(CR));
1426 if (!Pair.second)
1427 Pair.first->second = std::move(CR);
1428 return Pair.first->second;
1431 /// Determine the range for a particular SCEV.
1432 /// NOTE: This returns a reference to an entry in a cache. It must be
1433 /// copied if its needed for longer.
1434 const ConstantRange &getRangeRef(const SCEV *S, RangeSignHint Hint);
1436 /// Determines the range for the affine SCEVAddRecExpr {\p Start,+,\p Stop}.
1437 /// Helper for \c getRange.
1438 ConstantRange getRangeForAffineAR(const SCEV *Start, const SCEV *Stop,
1439 const SCEV *MaxBECount, unsigned BitWidth);
1441 /// Try to compute a range for the affine SCEVAddRecExpr {\p Start,+,\p
1442 /// Stop} by "factoring out" a ternary expression from the add recurrence.
1443 /// Helper called by \c getRange.
1444 ConstantRange getRangeViaFactoring(const SCEV *Start, const SCEV *Stop,
1445 const SCEV *MaxBECount, unsigned BitWidth);
1447 /// We know that there is no SCEV for the specified value. Analyze the
1448 /// expression.
1449 const SCEV *createSCEV(Value *V);
1451 /// Provide the special handling we need to analyze PHI SCEVs.
1452 const SCEV *createNodeForPHI(PHINode *PN);
1454 /// Helper function called from createNodeForPHI.
1455 const SCEV *createAddRecFromPHI(PHINode *PN);
1457 /// A helper function for createAddRecFromPHI to handle simple cases.
1458 const SCEV *createSimpleAffineAddRec(PHINode *PN, Value *BEValueV,
1459 Value *StartValueV);
1461 /// Helper function called from createNodeForPHI.
1462 const SCEV *createNodeFromSelectLikePHI(PHINode *PN);
1464 /// Provide special handling for a select-like instruction (currently this
1465 /// is either a select instruction or a phi node). \p I is the instruction
1466 /// being processed, and it is assumed equivalent to "Cond ? TrueVal :
1467 /// FalseVal".
1468 const SCEV *createNodeForSelectOrPHI(Instruction *I, Value *Cond,
1469 Value *TrueVal, Value *FalseVal);
1471 /// Provide the special handling we need to analyze GEP SCEVs.
1472 const SCEV *createNodeForGEP(GEPOperator *GEP);
1474 /// Implementation code for getSCEVAtScope; called at most once for each
1475 /// SCEV+Loop pair.
1476 const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
1478 /// This looks up computed SCEV values for all instructions that depend on
1479 /// the given instruction and removes them from the ValueExprMap map if they
1480 /// reference SymName. This is used during PHI resolution.
1481 void forgetSymbolicName(Instruction *I, const SCEV *SymName);
1483 /// Return the BackedgeTakenInfo for the given loop, lazily computing new
1484 /// values if the loop hasn't been analyzed yet. The returned result is
1485 /// guaranteed not to be predicated.
1486 const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
1488 /// Similar to getBackedgeTakenInfo, but will add predicates as required
1489 /// with the purpose of returning complete information.
1490 const BackedgeTakenInfo &getPredicatedBackedgeTakenInfo(const Loop *L);
1492 /// Compute the number of times the specified loop will iterate.
1493 /// If AllowPredicates is set, we will create new SCEV predicates as
1494 /// necessary in order to return an exact answer.
1495 BackedgeTakenInfo computeBackedgeTakenCount(const Loop *L,
1496 bool AllowPredicates = false);
1498 /// Compute the number of times the backedge of the specified loop will
1499 /// execute if it exits via the specified block. If AllowPredicates is set,
1500 /// this call will try to use a minimal set of SCEV predicates in order to
1501 /// return an exact answer.
1502 ExitLimit computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
1503 bool AllowPredicates = false);
1505 /// Compute the number of times the backedge of the specified loop will
1506 /// execute if its exit condition were a conditional branch of ExitCond.
1508 /// \p ControlsExit is true if ExitCond directly controls the exit
1509 /// branch. In this case, we can assume that the loop exits only if the
1510 /// condition is true and can infer that failing to meet the condition prior
1511 /// to integer wraparound results in undefined behavior.
1513 /// If \p AllowPredicates is set, this call will try to use a minimal set of
1514 /// SCEV predicates in order to return an exact answer.
1515 ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond,
1516 bool ExitIfTrue, bool ControlsExit,
1517 bool AllowPredicates = false);
1519 // Helper functions for computeExitLimitFromCond to avoid exponential time
1520 // complexity.
1522 class ExitLimitCache {
1523 // It may look like we need key on the whole (L, ExitIfTrue, ControlsExit,
1524 // AllowPredicates) tuple, but recursive calls to
1525 // computeExitLimitFromCondCached from computeExitLimitFromCondImpl only
1526 // vary the in \c ExitCond and \c ControlsExit parameters. We remember the
1527 // initial values of the other values to assert our assumption.
1528 SmallDenseMap<PointerIntPair<Value *, 1>, ExitLimit> TripCountMap;
1530 const Loop *L;
1531 bool ExitIfTrue;
1532 bool AllowPredicates;
1534 public:
1535 ExitLimitCache(const Loop *L, bool ExitIfTrue, bool AllowPredicates)
1536 : L(L), ExitIfTrue(ExitIfTrue), AllowPredicates(AllowPredicates) {}
1538 Optional<ExitLimit> find(const Loop *L, Value *ExitCond, bool ExitIfTrue,
1539 bool ControlsExit, bool AllowPredicates);
1541 void insert(const Loop *L, Value *ExitCond, bool ExitIfTrue,
1542 bool ControlsExit, bool AllowPredicates, const ExitLimit &EL);
1545 using ExitLimitCacheTy = ExitLimitCache;
1547 ExitLimit computeExitLimitFromCondCached(ExitLimitCacheTy &Cache,
1548 const Loop *L, Value *ExitCond,
1549 bool ExitIfTrue,
1550 bool ControlsExit,
1551 bool AllowPredicates);
1552 ExitLimit computeExitLimitFromCondImpl(ExitLimitCacheTy &Cache, const Loop *L,
1553 Value *ExitCond, bool ExitIfTrue,
1554 bool ControlsExit,
1555 bool AllowPredicates);
1557 /// Compute the number of times the backedge of the specified loop will
1558 /// execute if its exit condition were a conditional branch of the ICmpInst
1559 /// ExitCond and ExitIfTrue. If AllowPredicates is set, this call will try
1560 /// to use a minimal set of SCEV predicates in order to return an exact
1561 /// answer.
1562 ExitLimit computeExitLimitFromICmp(const Loop *L, ICmpInst *ExitCond,
1563 bool ExitIfTrue,
1564 bool IsSubExpr,
1565 bool AllowPredicates = false);
1567 /// Compute the number of times the backedge of the specified loop will
1568 /// execute if its exit condition were a switch with a single exiting case
1569 /// to ExitingBB.
1570 ExitLimit computeExitLimitFromSingleExitSwitch(const Loop *L,
1571 SwitchInst *Switch,
1572 BasicBlock *ExitingBB,
1573 bool IsSubExpr);
1575 /// Given an exit condition of 'icmp op load X, cst', try to see if we can
1576 /// compute the backedge-taken count.
1577 ExitLimit computeLoadConstantCompareExitLimit(LoadInst *LI, Constant *RHS,
1578 const Loop *L,
1579 ICmpInst::Predicate p);
1581 /// Compute the exit limit of a loop that is controlled by a
1582 /// "(IV >> 1) != 0" type comparison. We cannot compute the exact trip
1583 /// count in these cases (since SCEV has no way of expressing them), but we
1584 /// can still sometimes compute an upper bound.
1586 /// Return an ExitLimit for a loop whose backedge is guarded by `LHS Pred
1587 /// RHS`.
1588 ExitLimit computeShiftCompareExitLimit(Value *LHS, Value *RHS, const Loop *L,
1589 ICmpInst::Predicate Pred);
1591 /// If the loop is known to execute a constant number of times (the
1592 /// condition evolves only from constants), try to evaluate a few iterations
1593 /// of the loop until we get the exit condition gets a value of ExitWhen
1594 /// (true or false). If we cannot evaluate the exit count of the loop,
1595 /// return CouldNotCompute.
1596 const SCEV *computeExitCountExhaustively(const Loop *L, Value *Cond,
1597 bool ExitWhen);
1599 /// Return the number of times an exit condition comparing the specified
1600 /// value to zero will execute. If not computable, return CouldNotCompute.
1601 /// If AllowPredicates is set, this call will try to use a minimal set of
1602 /// SCEV predicates in order to return an exact answer.
1603 ExitLimit howFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr,
1604 bool AllowPredicates = false);
1606 /// Return the number of times an exit condition checking the specified
1607 /// value for nonzero will execute. If not computable, return
1608 /// CouldNotCompute.
1609 ExitLimit howFarToNonZero(const SCEV *V, const Loop *L);
1611 /// Return the number of times an exit condition containing the specified
1612 /// less-than comparison will execute. If not computable, return
1613 /// CouldNotCompute.
1615 /// \p isSigned specifies whether the less-than is signed.
1617 /// \p ControlsExit is true when the LHS < RHS condition directly controls
1618 /// the branch (loops exits only if condition is true). In this case, we can
1619 /// use NoWrapFlags to skip overflow checks.
1621 /// If \p AllowPredicates is set, this call will try to use a minimal set of
1622 /// SCEV predicates in order to return an exact answer.
1623 ExitLimit howManyLessThans(const SCEV *LHS, const SCEV *RHS, const Loop *L,
1624 bool isSigned, bool ControlsExit,
1625 bool AllowPredicates = false);
1627 ExitLimit howManyGreaterThans(const SCEV *LHS, const SCEV *RHS, const Loop *L,
1628 bool isSigned, bool IsSubExpr,
1629 bool AllowPredicates = false);
1631 /// Return a predecessor of BB (which may not be an immediate predecessor)
1632 /// which has exactly one successor from which BB is reachable, or null if
1633 /// no such block is found.
1634 std::pair<BasicBlock *, BasicBlock *>
1635 getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
1637 /// Test whether the condition described by Pred, LHS, and RHS is true
1638 /// whenever the given FoundCondValue value evaluates to true.
1639 bool isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
1640 Value *FoundCondValue, bool Inverse);
1642 /// Test whether the condition described by Pred, LHS, and RHS is true
1643 /// whenever the condition described by FoundPred, FoundLHS, FoundRHS is
1644 /// true.
1645 bool isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
1646 ICmpInst::Predicate FoundPred, const SCEV *FoundLHS,
1647 const SCEV *FoundRHS);
1649 /// Test whether the condition described by Pred, LHS, and RHS is true
1650 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1651 /// true.
1652 bool isImpliedCondOperands(ICmpInst::Predicate Pred, const SCEV *LHS,
1653 const SCEV *RHS, const SCEV *FoundLHS,
1654 const SCEV *FoundRHS);
1656 /// Test whether the condition described by Pred, LHS, and RHS is true
1657 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1658 /// true. Here LHS is an operation that includes FoundLHS as one of its
1659 /// arguments.
1660 bool isImpliedViaOperations(ICmpInst::Predicate Pred,
1661 const SCEV *LHS, const SCEV *RHS,
1662 const SCEV *FoundLHS, const SCEV *FoundRHS,
1663 unsigned Depth = 0);
1665 /// Test whether the condition described by Pred, LHS, and RHS is true.
1666 /// Use only simple non-recursive types of checks, such as range analysis etc.
1667 bool isKnownViaNonRecursiveReasoning(ICmpInst::Predicate Pred,
1668 const SCEV *LHS, const SCEV *RHS);
1670 /// Test whether the condition described by Pred, LHS, and RHS is true
1671 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1672 /// true.
1673 bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, const SCEV *LHS,
1674 const SCEV *RHS, const SCEV *FoundLHS,
1675 const SCEV *FoundRHS);
1677 /// Test whether the condition described by Pred, LHS, and RHS is true
1678 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1679 /// true. Utility function used by isImpliedCondOperands. Tries to get
1680 /// cases like "X `sgt` 0 => X - 1 `sgt` -1".
1681 bool isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred, const SCEV *LHS,
1682 const SCEV *RHS, const SCEV *FoundLHS,
1683 const SCEV *FoundRHS);
1685 /// Return true if the condition denoted by \p LHS \p Pred \p RHS is implied
1686 /// by a call to \c @llvm.experimental.guard in \p BB.
1687 bool isImpliedViaGuard(BasicBlock *BB, ICmpInst::Predicate Pred,
1688 const SCEV *LHS, const SCEV *RHS);
1690 /// Test whether the condition described by Pred, LHS, and RHS is true
1691 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1692 /// true.
1694 /// This routine tries to rule out certain kinds of integer overflow, and
1695 /// then tries to reason about arithmetic properties of the predicates.
1696 bool isImpliedCondOperandsViaNoOverflow(ICmpInst::Predicate Pred,
1697 const SCEV *LHS, const SCEV *RHS,
1698 const SCEV *FoundLHS,
1699 const SCEV *FoundRHS);
1701 /// Test whether the condition described by Pred, LHS, and RHS is true
1702 /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
1703 /// true.
1705 /// This routine tries to figure out predicate for Phis which are SCEVUnknown
1706 /// if it is true for every possible incoming value from their respective
1707 /// basic blocks.
1708 bool isImpliedViaMerge(ICmpInst::Predicate Pred,
1709 const SCEV *LHS, const SCEV *RHS,
1710 const SCEV *FoundLHS, const SCEV *FoundRHS,
1711 unsigned Depth);
1713 /// If we know that the specified Phi is in the header of its containing
1714 /// loop, we know the loop executes a constant number of times, and the PHI
1715 /// node is just a recurrence involving constants, fold it.
1716 Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt &BEs,
1717 const Loop *L);
1719 /// Test if the given expression is known to satisfy the condition described
1720 /// by Pred and the known constant ranges of LHS and RHS.
1721 bool isKnownPredicateViaConstantRanges(ICmpInst::Predicate Pred,
1722 const SCEV *LHS, const SCEV *RHS);
1724 /// Try to prove the condition described by "LHS Pred RHS" by ruling out
1725 /// integer overflow.
1727 /// For instance, this will return true for "A s< (A + C)<nsw>" if C is
1728 /// positive.
1729 bool isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred, const SCEV *LHS,
1730 const SCEV *RHS);
1732 /// Try to split Pred LHS RHS into logical conjunctions (and's) and try to
1733 /// prove them individually.
1734 bool isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, const SCEV *LHS,
1735 const SCEV *RHS);
1737 /// Try to match the Expr as "(L + R)<Flags>".
1738 bool splitBinaryAdd(const SCEV *Expr, const SCEV *&L, const SCEV *&R,
1739 SCEV::NoWrapFlags &Flags);
1741 /// Compute \p LHS - \p RHS and returns the result as an APInt if it is a
1742 /// constant, and None if it isn't.
1744 /// This is intended to be a cheaper version of getMinusSCEV. We can be
1745 /// frugal here since we just bail out of actually constructing and
1746 /// canonicalizing an expression in the cases where the result isn't going
1747 /// to be a constant.
1748 Optional<APInt> computeConstantDifference(const SCEV *LHS, const SCEV *RHS);
1750 /// Drop memoized information computed for S.
1751 void forgetMemoizedResults(const SCEV *S);
1753 /// Return an existing SCEV for V if there is one, otherwise return nullptr.
1754 const SCEV *getExistingSCEV(Value *V);
1756 /// Return false iff given SCEV contains a SCEVUnknown with NULL value-
1757 /// pointer.
1758 bool checkValidity(const SCEV *S) const;
1760 /// Return true if `ExtendOpTy`({`Start`,+,`Step`}) can be proved to be
1761 /// equal to {`ExtendOpTy`(`Start`),+,`ExtendOpTy`(`Step`)}. This is
1762 /// equivalent to proving no signed (resp. unsigned) wrap in
1763 /// {`Start`,+,`Step`} if `ExtendOpTy` is `SCEVSignExtendExpr`
1764 /// (resp. `SCEVZeroExtendExpr`).
1765 template <typename ExtendOpTy>
1766 bool proveNoWrapByVaryingStart(const SCEV *Start, const SCEV *Step,
1767 const Loop *L);
1769 /// Try to prove NSW or NUW on \p AR relying on ConstantRange manipulation.
1770 SCEV::NoWrapFlags proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR);
1772 bool isMonotonicPredicateImpl(const SCEVAddRecExpr *LHS,
1773 ICmpInst::Predicate Pred, bool &Increasing);
1775 /// Return SCEV no-wrap flags that can be proven based on reasoning about
1776 /// how poison produced from no-wrap flags on this value (e.g. a nuw add)
1777 /// would trigger undefined behavior on overflow.
1778 SCEV::NoWrapFlags getNoWrapFlagsFromUB(const Value *V);
1780 /// Return true if the SCEV corresponding to \p I is never poison. Proving
1781 /// this is more complex than proving that just \p I is never poison, since
1782 /// SCEV commons expressions across control flow, and you can have cases
1783 /// like:
1785 /// idx0 = a + b;
1786 /// ptr[idx0] = 100;
1787 /// if (<condition>) {
1788 /// idx1 = a +nsw b;
1789 /// ptr[idx1] = 200;
1790 /// }
1792 /// where the SCEV expression (+ a b) is guaranteed to not be poison (and
1793 /// hence not sign-overflow) only if "<condition>" is true. Since both
1794 /// `idx0` and `idx1` will be mapped to the same SCEV expression, (+ a b),
1795 /// it is not okay to annotate (+ a b) with <nsw> in the above example.
1796 bool isSCEVExprNeverPoison(const Instruction *I);
1798 /// This is like \c isSCEVExprNeverPoison but it specifically works for
1799 /// instructions that will get mapped to SCEV add recurrences. Return true
1800 /// if \p I will never generate poison under the assumption that \p I is an
1801 /// add recurrence on the loop \p L.
1802 bool isAddRecNeverPoison(const Instruction *I, const Loop *L);
1804 /// Similar to createAddRecFromPHI, but with the additional flexibility of
1805 /// suggesting runtime overflow checks in case casts are encountered.
1806 /// If successful, the analysis records that for this loop, \p SymbolicPHI,
1807 /// which is the UnknownSCEV currently representing the PHI, can be rewritten
1808 /// into an AddRec, assuming some predicates; The function then returns the
1809 /// AddRec and the predicates as a pair, and caches this pair in
1810 /// PredicatedSCEVRewrites.
1811 /// If the analysis is not successful, a mapping from the \p SymbolicPHI to
1812 /// itself (with no predicates) is recorded, and a nullptr with an empty
1813 /// predicates vector is returned as a pair.
1814 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
1815 createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI);
1817 /// Compute the backedge taken count knowing the interval difference, the
1818 /// stride and presence of the equality in the comparison.
1819 const SCEV *computeBECount(const SCEV *Delta, const SCEV *Stride,
1820 bool Equality);
1822 /// Compute the maximum backedge count based on the range of values
1823 /// permitted by Start, End, and Stride. This is for loops of the form
1824 /// {Start, +, Stride} LT End.
1826 /// Precondition: the induction variable is known to be positive. We *don't*
1827 /// assert these preconditions so please be careful.
1828 const SCEV *computeMaxBECountForLT(const SCEV *Start, const SCEV *Stride,
1829 const SCEV *End, unsigned BitWidth,
1830 bool IsSigned);
1832 /// Verify if an linear IV with positive stride can overflow when in a
1833 /// less-than comparison, knowing the invariant term of the comparison,
1834 /// the stride and the knowledge of NSW/NUW flags on the recurrence.
1835 bool doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, bool IsSigned,
1836 bool NoWrap);
1838 /// Verify if an linear IV with negative stride can overflow when in a
1839 /// greater-than comparison, knowing the invariant term of the comparison,
1840 /// the stride and the knowledge of NSW/NUW flags on the recurrence.
1841 bool doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, bool IsSigned,
1842 bool NoWrap);
1844 /// Get add expr already created or create a new one.
1845 const SCEV *getOrCreateAddExpr(ArrayRef<const SCEV *> Ops,
1846 SCEV::NoWrapFlags Flags);
1848 /// Get mul expr already created or create a new one.
1849 const SCEV *getOrCreateMulExpr(ArrayRef<const SCEV *> Ops,
1850 SCEV::NoWrapFlags Flags);
1852 // Get addrec expr already created or create a new one.
1853 const SCEV *getOrCreateAddRecExpr(ArrayRef<const SCEV *> Ops,
1854 const Loop *L, SCEV::NoWrapFlags Flags);
1856 /// Return x if \p Val is f(x) where f is a 1-1 function.
1857 const SCEV *stripInjectiveFunctions(const SCEV *Val) const;
1859 /// Find all of the loops transitively used in \p S, and fill \p LoopsUsed.
1860 /// A loop is considered "used" by an expression if it contains
1861 /// an add rec on said loop.
1862 void getUsedLoops(const SCEV *S, SmallPtrSetImpl<const Loop *> &LoopsUsed);
1864 /// Find all of the loops transitively used in \p S, and update \c LoopUsers
1865 /// accordingly.
1866 void addToLoopUseLists(const SCEV *S);
1868 /// Try to match the pattern generated by getURemExpr(A, B). If successful,
1869 /// Assign A and B to LHS and RHS, respectively.
1870 bool matchURem(const SCEV *Expr, const SCEV *&LHS, const SCEV *&RHS);
1872 FoldingSet<SCEV> UniqueSCEVs;
1873 FoldingSet<SCEVPredicate> UniquePreds;
1874 BumpPtrAllocator SCEVAllocator;
1876 /// This maps loops to a list of SCEV expressions that (transitively) use said
1877 /// loop.
1878 DenseMap<const Loop *, SmallVector<const SCEV *, 4>> LoopUsers;
1880 /// Cache tentative mappings from UnknownSCEVs in a Loop, to a SCEV expression
1881 /// they can be rewritten into under certain predicates.
1882 DenseMap<std::pair<const SCEVUnknown *, const Loop *>,
1883 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
1884 PredicatedSCEVRewrites;
1886 /// The head of a linked list of all SCEVUnknown values that have been
1887 /// allocated. This is used by releaseMemory to locate them all and call
1888 /// their destructors.
1889 SCEVUnknown *FirstUnknown = nullptr;
1892 /// Analysis pass that exposes the \c ScalarEvolution for a function.
1893 class ScalarEvolutionAnalysis
1894 : public AnalysisInfoMixin<ScalarEvolutionAnalysis> {
1895 friend AnalysisInfoMixin<ScalarEvolutionAnalysis>;
1897 static AnalysisKey Key;
1899 public:
1900 using Result = ScalarEvolution;
1902 ScalarEvolution run(Function &F, FunctionAnalysisManager &AM);
1905 /// Printer pass for the \c ScalarEvolutionAnalysis results.
1906 class ScalarEvolutionPrinterPass
1907 : public PassInfoMixin<ScalarEvolutionPrinterPass> {
1908 raw_ostream &OS;
1910 public:
1911 explicit ScalarEvolutionPrinterPass(raw_ostream &OS) : OS(OS) {}
1913 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
1916 class ScalarEvolutionWrapperPass : public FunctionPass {
1917 std::unique_ptr<ScalarEvolution> SE;
1919 public:
1920 static char ID;
1922 ScalarEvolutionWrapperPass();
1924 ScalarEvolution &getSE() { return *SE; }
1925 const ScalarEvolution &getSE() const { return *SE; }
1927 bool runOnFunction(Function &F) override;
1928 void releaseMemory() override;
1929 void getAnalysisUsage(AnalysisUsage &AU) const override;
1930 void print(raw_ostream &OS, const Module * = nullptr) const override;
1931 void verifyAnalysis() const override;
1934 /// An interface layer with SCEV used to manage how we see SCEV expressions
1935 /// for values in the context of existing predicates. We can add new
1936 /// predicates, but we cannot remove them.
1938 /// This layer has multiple purposes:
1939 /// - provides a simple interface for SCEV versioning.
1940 /// - guarantees that the order of transformations applied on a SCEV
1941 /// expression for a single Value is consistent across two different
1942 /// getSCEV calls. This means that, for example, once we've obtained
1943 /// an AddRec expression for a certain value through expression
1944 /// rewriting, we will continue to get an AddRec expression for that
1945 /// Value.
1946 /// - lowers the number of expression rewrites.
1947 class PredicatedScalarEvolution {
1948 public:
1949 PredicatedScalarEvolution(ScalarEvolution &SE, Loop &L);
1951 const SCEVUnionPredicate &getUnionPredicate() const;
1953 /// Returns the SCEV expression of V, in the context of the current SCEV
1954 /// predicate. The order of transformations applied on the expression of V
1955 /// returned by ScalarEvolution is guaranteed to be preserved, even when
1956 /// adding new predicates.
1957 const SCEV *getSCEV(Value *V);
1959 /// Get the (predicated) backedge count for the analyzed loop.
1960 const SCEV *getBackedgeTakenCount();
1962 /// Adds a new predicate.
1963 void addPredicate(const SCEVPredicate &Pred);
1965 /// Attempts to produce an AddRecExpr for V by adding additional SCEV
1966 /// predicates. If we can't transform the expression into an AddRecExpr we
1967 /// return nullptr and not add additional SCEV predicates to the current
1968 /// context.
1969 const SCEVAddRecExpr *getAsAddRec(Value *V);
1971 /// Proves that V doesn't overflow by adding SCEV predicate.
1972 void setNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags);
1974 /// Returns true if we've proved that V doesn't wrap by means of a SCEV
1975 /// predicate.
1976 bool hasNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags);
1978 /// Returns the ScalarEvolution analysis used.
1979 ScalarEvolution *getSE() const { return &SE; }
1981 /// We need to explicitly define the copy constructor because of FlagsMap.
1982 PredicatedScalarEvolution(const PredicatedScalarEvolution &);
1984 /// Print the SCEV mappings done by the Predicated Scalar Evolution.
1985 /// The printed text is indented by \p Depth.
1986 void print(raw_ostream &OS, unsigned Depth) const;
1988 /// Check if \p AR1 and \p AR2 are equal, while taking into account
1989 /// Equal predicates in Preds.
1990 bool areAddRecsEqualWithPreds(const SCEVAddRecExpr *AR1,
1991 const SCEVAddRecExpr *AR2) const;
1993 private:
1994 /// Increments the version number of the predicate. This needs to be called
1995 /// every time the SCEV predicate changes.
1996 void updateGeneration();
1998 /// Holds a SCEV and the version number of the SCEV predicate used to
1999 /// perform the rewrite of the expression.
2000 using RewriteEntry = std::pair<unsigned, const SCEV *>;
2002 /// Maps a SCEV to the rewrite result of that SCEV at a certain version
2003 /// number. If this number doesn't match the current Generation, we will
2004 /// need to do a rewrite. To preserve the transformation order of previous
2005 /// rewrites, we will rewrite the previous result instead of the original
2006 /// SCEV.
2007 DenseMap<const SCEV *, RewriteEntry> RewriteMap;
2009 /// Records what NoWrap flags we've added to a Value *.
2010 ValueMap<Value *, SCEVWrapPredicate::IncrementWrapFlags> FlagsMap;
2012 /// The ScalarEvolution analysis.
2013 ScalarEvolution &SE;
2015 /// The analyzed Loop.
2016 const Loop &L;
2018 /// The SCEVPredicate that forms our context. We will rewrite all
2019 /// expressions assuming that this predicate true.
2020 SCEVUnionPredicate Preds;
2022 /// Marks the version of the SCEV predicate used. When rewriting a SCEV
2023 /// expression we mark it with the version of the predicate. We use this to
2024 /// figure out if the predicate has changed from the last rewrite of the
2025 /// SCEV. If so, we need to perform a new rewrite.
2026 unsigned Generation = 0;
2028 /// The backedge taken count.
2029 const SCEV *BackedgeCount = nullptr;
2032 } // end namespace llvm
2034 #endif // LLVM_ANALYSIS_SCALAREVOLUTION_H