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