ScalarEvolution.h 105 KB

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