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