ScalarEvolution.h 100 KB

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