ScalarEvolution.h 96 KB

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