ScalarEvolution.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. //===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // The ScalarEvolution class is an LLVM pass which can be used to analyze and
  11. // categorize scalar expressions in loops. It specializes in recognizing
  12. // general induction variables, representing them with the abstract and opaque
  13. // SCEV class. Given this analysis, trip counts of loops and other important
  14. // properties can be obtained.
  15. //
  16. // This analysis is primarily useful for induction variable substitution and
  17. // strength reduction.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
  21. #define LLVM_ANALYSIS_SCALAREVOLUTION_H
  22. #include "llvm/ADT/DenseSet.h"
  23. #include "llvm/ADT/FoldingSet.h"
  24. #include "llvm/IR/ConstantRange.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/IR/Operator.h"
  28. #include "llvm/IR/ValueHandle.h"
  29. #include "llvm/Pass.h"
  30. #include "llvm/Support/Allocator.h"
  31. #include "llvm/Support/DataTypes.h"
  32. #include <map>
  33. namespace llvm {
  34. class APInt;
  35. class AssumptionCache;
  36. class Constant;
  37. class ConstantInt;
  38. class DominatorTree;
  39. class Type;
  40. class ScalarEvolution;
  41. class DataLayout;
  42. class TargetLibraryInfo;
  43. class LLVMContext;
  44. class Loop;
  45. class LoopInfo;
  46. class Operator;
  47. class SCEVUnknown;
  48. class SCEV;
  49. template<> struct FoldingSetTrait<SCEV>;
  50. /// SCEV - This class represents an analyzed expression in the program. These
  51. /// are opaque objects that the client is not allowed to do much with
  52. /// directly.
  53. ///
  54. class SCEV : public FoldingSetNode {
  55. friend struct FoldingSetTrait<SCEV>;
  56. /// FastID - A reference to an Interned FoldingSetNodeID for this node.
  57. /// The ScalarEvolution's BumpPtrAllocator holds the data.
  58. FoldingSetNodeIDRef FastID;
  59. // The SCEV baseclass this node corresponds to
  60. const unsigned short SCEVType;
  61. protected:
  62. /// SubclassData - This field is initialized to zero and may be used in
  63. /// subclasses to store miscellaneous information.
  64. unsigned short SubclassData;
  65. private:
  66. SCEV(const SCEV &) = delete;
  67. void operator=(const SCEV &) = delete;
  68. public:
  69. /// NoWrapFlags are bitfield indices into SubclassData.
  70. ///
  71. /// Add and Mul expressions may have no-unsigned-wrap <NUW> or
  72. /// no-signed-wrap <NSW> properties, which are derived from the IR
  73. /// operator. NSW is a misnomer that we use to mean no signed overflow or
  74. /// underflow.
  75. ///
  76. /// AddRec expressions may have a no-self-wraparound <NW> property if, in
  77. /// the integer domain, abs(step) * max-iteration(loop) <=
  78. /// unsigned-max(bitwidth). This means that the recurrence will never reach
  79. /// its start value if the step is non-zero. Computing the same value on
  80. /// each iteration is not considered wrapping, and recurrences with step = 0
  81. /// are trivially <NW>. <NW> is independent of the sign of step and the
  82. /// value the add recurrence starts with.
  83. ///
  84. /// Note that NUW and NSW are also valid properties of a recurrence, and
  85. /// either implies NW. For convenience, NW will be set for a recurrence
  86. /// whenever either NUW or NSW are set.
  87. enum NoWrapFlags { FlagAnyWrap = 0, // No guarantee.
  88. FlagNW = (1 << 0), // No self-wrap.
  89. FlagNUW = (1 << 1), // No unsigned wrap.
  90. FlagNSW = (1 << 2), // No signed wrap.
  91. NoWrapMask = (1 << 3) -1 };
  92. explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) :
  93. FastID(ID), SCEVType(SCEVTy), SubclassData(0) {}
  94. unsigned getSCEVType() const { return SCEVType; }
  95. /// getType - Return the LLVM type of this SCEV expression.
  96. ///
  97. Type *getType() const;
  98. /// isZero - Return true if the expression is a constant zero.
  99. ///
  100. bool isZero() const;
  101. /// isOne - Return true if the expression is a constant one.
  102. ///
  103. bool isOne() const;
  104. /// isAllOnesValue - Return true if the expression is a constant
  105. /// all-ones value.
  106. ///
  107. bool isAllOnesValue() const;
  108. /// isNonConstantNegative - Return true if the specified scev is negated,
  109. /// but not a constant.
  110. bool isNonConstantNegative() const;
  111. /// print - Print out the internal representation of this scalar to the
  112. /// specified stream. This should really only be used for debugging
  113. /// purposes.
  114. void print(raw_ostream &OS) const;
  115. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  116. /// dump - This method is used for debugging.
  117. ///
  118. void dump() const;
  119. #endif
  120. };
  121. // Specialize FoldingSetTrait for SCEV to avoid needing to compute
  122. // temporary FoldingSetNodeID values.
  123. template<> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> {
  124. static void Profile(const SCEV &X, FoldingSetNodeID& ID) {
  125. ID = X.FastID;
  126. }
  127. static bool Equals(const SCEV &X, const FoldingSetNodeID &ID,
  128. unsigned IDHash, FoldingSetNodeID &TempID) {
  129. return ID == X.FastID;
  130. }
  131. static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) {
  132. return X.FastID.ComputeHash();
  133. }
  134. };
  135. inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
  136. S.print(OS);
  137. return OS;
  138. }
  139. /// SCEVCouldNotCompute - An object of this class is returned by queries that
  140. /// could not be answered. For example, if you ask for the number of
  141. /// iterations of a linked-list traversal loop, you will get one of these.
  142. /// None of the standard SCEV operations are valid on this class, it is just a
  143. /// marker.
  144. struct SCEVCouldNotCompute : public SCEV {
  145. SCEVCouldNotCompute();
  146. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  147. static bool classof(const SCEV *S);
  148. };
  149. /// ScalarEvolution - This class is the main scalar evolution driver. Because
  150. /// client code (intentionally) can't do much with the SCEV objects directly,
  151. /// they must ask this class for services.
  152. ///
  153. class ScalarEvolution : public FunctionPass {
  154. public:
  155. /// LoopDisposition - An enum describing the relationship between a
  156. /// SCEV and a loop.
  157. enum LoopDisposition {
  158. LoopVariant, ///< The SCEV is loop-variant (unknown).
  159. LoopInvariant, ///< The SCEV is loop-invariant.
  160. LoopComputable ///< The SCEV varies predictably with the loop.
  161. };
  162. /// BlockDisposition - An enum describing the relationship between a
  163. /// SCEV and a basic block.
  164. enum BlockDisposition {
  165. DoesNotDominateBlock, ///< The SCEV does not dominate the block.
  166. DominatesBlock, ///< The SCEV dominates the block.
  167. ProperlyDominatesBlock ///< The SCEV properly dominates the block.
  168. };
  169. /// Convenient NoWrapFlags manipulation that hides enum casts and is
  170. /// visible in the ScalarEvolution name space.
  171. static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
  172. maskFlags(SCEV::NoWrapFlags Flags, int Mask) {
  173. return (SCEV::NoWrapFlags)(Flags & Mask);
  174. }
  175. static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
  176. setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags) {
  177. return (SCEV::NoWrapFlags)(Flags | OnFlags);
  178. }
  179. static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT
  180. clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags) {
  181. return (SCEV::NoWrapFlags)(Flags & ~OffFlags);
  182. }
  183. private:
  184. /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be
  185. /// notified whenever a Value is deleted.
  186. class SCEVCallbackVH : public CallbackVH {
  187. ScalarEvolution *SE;
  188. void deleted() override;
  189. void allUsesReplacedWith(Value *New) override;
  190. public:
  191. SCEVCallbackVH(Value *V, ScalarEvolution *SE = nullptr);
  192. };
  193. friend class SCEVCallbackVH;
  194. friend class SCEVExpander;
  195. friend class SCEVUnknown;
  196. /// F - The function we are analyzing.
  197. ///
  198. Function *F;
  199. /// The tracker for @llvm.assume intrinsics in this function.
  200. AssumptionCache *AC;
  201. /// LI - The loop information for the function we are currently analyzing.
  202. ///
  203. LoopInfo *LI;
  204. /// TLI - The target library information for the target we are targeting.
  205. ///
  206. TargetLibraryInfo *TLI;
  207. /// DT - The dominator tree.
  208. ///
  209. DominatorTree *DT;
  210. /// CouldNotCompute - This SCEV is used to represent unknown trip
  211. /// counts and things.
  212. SCEVCouldNotCompute CouldNotCompute;
  213. /// ValueExprMapType - The typedef for ValueExprMap.
  214. ///
  215. typedef DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *> >
  216. ValueExprMapType;
  217. /// ValueExprMap - This is a cache of the values we have analyzed so far.
  218. ///
  219. ValueExprMapType ValueExprMap;
  220. /// Mark predicate values currently being processed by isImpliedCond.
  221. DenseSet<Value*> PendingLoopPredicates;
  222. /// Set to true by isLoopBackedgeGuardedByCond when we're walking the set of
  223. /// conditions dominating the backedge of a loop.
  224. bool WalkingBEDominatingConds;
  225. /// ExitLimit - Information about the number of loop iterations for which a
  226. /// loop exit's branch condition evaluates to the not-taken path. This is a
  227. /// temporary pair of exact and max expressions that are eventually
  228. /// summarized in ExitNotTakenInfo and BackedgeTakenInfo.
  229. struct ExitLimit {
  230. const SCEV *Exact;
  231. const SCEV *Max;
  232. /*implicit*/ ExitLimit(const SCEV *E) : Exact(E), Max(E) {}
  233. ExitLimit(const SCEV *E, const SCEV *M) : Exact(E), Max(M) {}
  234. /// hasAnyInfo - Test whether this ExitLimit contains any computed
  235. /// information, or whether it's all SCEVCouldNotCompute values.
  236. bool hasAnyInfo() const {
  237. return !isa<SCEVCouldNotCompute>(Exact) ||
  238. !isa<SCEVCouldNotCompute>(Max);
  239. }
  240. };
  241. /// ExitNotTakenInfo - Information about the number of times a particular
  242. /// loop exit may be reached before exiting the loop.
  243. struct ExitNotTakenInfo {
  244. AssertingVH<BasicBlock> ExitingBlock;
  245. const SCEV *ExactNotTaken;
  246. PointerIntPair<ExitNotTakenInfo*, 1> NextExit;
  247. ExitNotTakenInfo() : ExitingBlock(nullptr), ExactNotTaken(nullptr) {}
  248. /// isCompleteList - Return true if all loop exits are computable.
  249. bool isCompleteList() const {
  250. return NextExit.getInt() == 0;
  251. }
  252. void setIncomplete() { NextExit.setInt(1); }
  253. /// getNextExit - Return a pointer to the next exit's not-taken info.
  254. ExitNotTakenInfo *getNextExit() const {
  255. return NextExit.getPointer();
  256. }
  257. void setNextExit(ExitNotTakenInfo *ENT) { NextExit.setPointer(ENT); }
  258. };
  259. /// BackedgeTakenInfo - Information about the backedge-taken count
  260. /// of a loop. This currently includes an exact count and a maximum count.
  261. ///
  262. class BackedgeTakenInfo {
  263. /// ExitNotTaken - A list of computable exits and their not-taken counts.
  264. /// Loops almost never have more than one computable exit.
  265. ExitNotTakenInfo ExitNotTaken;
  266. /// Max - An expression indicating the least maximum backedge-taken
  267. /// count of the loop that is known, or a SCEVCouldNotCompute.
  268. const SCEV *Max;
  269. public:
  270. BackedgeTakenInfo() : Max(nullptr) {}
  271. /// Initialize BackedgeTakenInfo from a list of exact exit counts.
  272. BackedgeTakenInfo(
  273. SmallVectorImpl< std::pair<BasicBlock *, const SCEV *> > &ExitCounts,
  274. bool Complete, const SCEV *MaxCount);
  275. /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any
  276. /// computed information, or whether it's all SCEVCouldNotCompute
  277. /// values.
  278. bool hasAnyInfo() const {
  279. return ExitNotTaken.ExitingBlock || !isa<SCEVCouldNotCompute>(Max);
  280. }
  281. /// getExact - Return an expression indicating the exact backedge-taken
  282. /// count of the loop if it is known, or SCEVCouldNotCompute
  283. /// otherwise. This is the number of times the loop header can be
  284. /// guaranteed to execute, minus one.
  285. const SCEV *getExact(ScalarEvolution *SE) const;
  286. /// getExact - Return the number of times this loop exit may fall through
  287. /// to the back edge, or SCEVCouldNotCompute. The loop is guaranteed not
  288. /// to exit via this block before this number of iterations, but may exit
  289. /// via another block.
  290. const SCEV *getExact(BasicBlock *ExitingBlock, ScalarEvolution *SE) const;
  291. /// getMax - Get the max backedge taken count for the loop.
  292. const SCEV *getMax(ScalarEvolution *SE) const;
  293. /// Return true if any backedge taken count expressions refer to the given
  294. /// subexpression.
  295. bool hasOperand(const SCEV *S, ScalarEvolution *SE) const;
  296. /// clear - Invalidate this result and free associated memory.
  297. void clear();
  298. };
  299. /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for
  300. /// this function as they are computed.
  301. DenseMap<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
  302. /// ConstantEvolutionLoopExitValue - This map contains entries for all of
  303. /// the PHI instructions that we attempt to compute constant evolutions for.
  304. /// This allows us to avoid potentially expensive recomputation of these
  305. /// properties. An instruction maps to null if we are unable to compute its
  306. /// exit value.
  307. DenseMap<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
  308. /// ValuesAtScopes - This map contains entries for all the expressions
  309. /// that we attempt to compute getSCEVAtScope information for, which can
  310. /// be expensive in extreme cases.
  311. DenseMap<const SCEV *,
  312. SmallVector<std::pair<const Loop *, const SCEV *>, 2> > ValuesAtScopes;
  313. /// LoopDispositions - Memoized computeLoopDisposition results.
  314. DenseMap<const SCEV *,
  315. SmallVector<PointerIntPair<const Loop *, 2, LoopDisposition>, 2>>
  316. LoopDispositions;
  317. /// computeLoopDisposition - Compute a LoopDisposition value.
  318. LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L);
  319. /// BlockDispositions - Memoized computeBlockDisposition results.
  320. DenseMap<
  321. const SCEV *,
  322. SmallVector<PointerIntPair<const BasicBlock *, 2, BlockDisposition>, 2>>
  323. BlockDispositions;
  324. /// computeBlockDisposition - Compute a BlockDisposition value.
  325. BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB);
  326. /// UnsignedRanges - Memoized results from getRange
  327. DenseMap<const SCEV *, ConstantRange> UnsignedRanges;
  328. /// SignedRanges - Memoized results from getRange
  329. DenseMap<const SCEV *, ConstantRange> SignedRanges;
  330. /// RangeSignHint - Used to parameterize getRange
  331. enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED };
  332. /// setRange - Set the memoized range for the given SCEV.
  333. const ConstantRange &setRange(const SCEV *S, RangeSignHint Hint,
  334. const ConstantRange &CR) {
  335. DenseMap<const SCEV *, ConstantRange> &Cache =
  336. Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges;
  337. std::pair<DenseMap<const SCEV *, ConstantRange>::iterator, bool> Pair =
  338. Cache.insert(std::make_pair(S, CR));
  339. if (!Pair.second)
  340. Pair.first->second = CR;
  341. return Pair.first->second;
  342. }
  343. /// getRange - Determine the range for a particular SCEV.
  344. ConstantRange getRange(const SCEV *S, RangeSignHint Hint);
  345. /// createSCEV - We know that there is no SCEV for the specified value.
  346. /// Analyze the expression.
  347. const SCEV *createSCEV(Value *V);
  348. /// createNodeForPHI - Provide the special handling we need to analyze PHI
  349. /// SCEVs.
  350. const SCEV *createNodeForPHI(PHINode *PN);
  351. /// createNodeForGEP - Provide the special handling we need to analyze GEP
  352. /// SCEVs.
  353. const SCEV *createNodeForGEP(GEPOperator *GEP);
  354. /// computeSCEVAtScope - Implementation code for getSCEVAtScope; called
  355. /// at most once for each SCEV+Loop pair.
  356. ///
  357. const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
  358. /// ForgetSymbolicValue - This looks up computed SCEV values for all
  359. /// instructions that depend on the given instruction and removes them from
  360. /// the ValueExprMap map if they reference SymName. This is used during PHI
  361. /// resolution.
  362. void ForgetSymbolicName(Instruction *I, const SCEV *SymName);
  363. /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given
  364. /// loop, lazily computing new values if the loop hasn't been analyzed
  365. /// yet.
  366. const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
  367. /// ComputeBackedgeTakenCount - Compute the number of times the specified
  368. /// loop will iterate.
  369. BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L);
  370. /// ComputeExitLimit - Compute the number of times the backedge of the
  371. /// specified loop will execute if it exits via the specified block.
  372. ExitLimit ComputeExitLimit(const Loop *L, BasicBlock *ExitingBlock);
  373. /// ComputeExitLimitFromCond - Compute the number of times the backedge of
  374. /// the specified loop will execute if its exit condition were a conditional
  375. /// branch of ExitCond, TBB, and FBB.
  376. ExitLimit ComputeExitLimitFromCond(const Loop *L,
  377. Value *ExitCond,
  378. BasicBlock *TBB,
  379. BasicBlock *FBB,
  380. bool IsSubExpr);
  381. /// ComputeExitLimitFromICmp - Compute the number of times the backedge of
  382. /// the specified loop will execute if its exit condition were a conditional
  383. /// branch of the ICmpInst ExitCond, TBB, and FBB.
  384. ExitLimit ComputeExitLimitFromICmp(const Loop *L,
  385. ICmpInst *ExitCond,
  386. BasicBlock *TBB,
  387. BasicBlock *FBB,
  388. bool IsSubExpr);
  389. /// ComputeExitLimitFromSingleExitSwitch - Compute the number of times the
  390. /// backedge of the specified loop will execute if its exit condition were a
  391. /// switch with a single exiting case to ExitingBB.
  392. ExitLimit
  393. ComputeExitLimitFromSingleExitSwitch(const Loop *L, SwitchInst *Switch,
  394. BasicBlock *ExitingBB, bool IsSubExpr);
  395. /// ComputeLoadConstantCompareExitLimit - Given an exit condition
  396. /// of 'icmp op load X, cst', try to see if we can compute the
  397. /// backedge-taken count.
  398. ExitLimit ComputeLoadConstantCompareExitLimit(LoadInst *LI,
  399. Constant *RHS,
  400. const Loop *L,
  401. ICmpInst::Predicate p);
  402. /// ComputeExitCountExhaustively - If the loop is known to execute a
  403. /// constant number of times (the condition evolves only from constants),
  404. /// try to evaluate a few iterations of the loop until we get the exit
  405. /// condition gets a value of ExitWhen (true or false). If we cannot
  406. /// evaluate the exit count of the loop, return CouldNotCompute.
  407. const SCEV *ComputeExitCountExhaustively(const Loop *L,
  408. Value *Cond,
  409. bool ExitWhen);
  410. /// HowFarToZero - Return the number of times an exit condition comparing
  411. /// the specified value to zero will execute. If not computable, return
  412. /// CouldNotCompute.
  413. ExitLimit HowFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr);
  414. /// HowFarToNonZero - Return the number of times an exit condition checking
  415. /// the specified value for nonzero will execute. If not computable, return
  416. /// CouldNotCompute.
  417. ExitLimit HowFarToNonZero(const SCEV *V, const Loop *L);
  418. /// HowManyLessThans - Return the number of times an exit condition
  419. /// containing the specified less-than comparison will execute. If not
  420. /// computable, return CouldNotCompute. isSigned specifies whether the
  421. /// less-than is signed.
  422. ExitLimit HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
  423. const Loop *L, bool isSigned, bool IsSubExpr);
  424. ExitLimit HowManyGreaterThans(const SCEV *LHS, const SCEV *RHS,
  425. const Loop *L, bool isSigned, bool IsSubExpr);
  426. /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
  427. /// (which may not be an immediate predecessor) which has exactly one
  428. /// successor from which BB is reachable, or null if no such block is
  429. /// found.
  430. std::pair<BasicBlock *, BasicBlock *>
  431. getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
  432. /// isImpliedCond - Test whether the condition described by Pred, LHS, and
  433. /// RHS is true whenever the given FoundCondValue value evaluates to true.
  434. bool isImpliedCond(ICmpInst::Predicate Pred,
  435. const SCEV *LHS, const SCEV *RHS,
  436. Value *FoundCondValue,
  437. bool Inverse);
  438. /// isImpliedCondOperands - Test whether the condition described by Pred,
  439. /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS,
  440. /// and FoundRHS is true.
  441. bool isImpliedCondOperands(ICmpInst::Predicate Pred,
  442. const SCEV *LHS, const SCEV *RHS,
  443. const SCEV *FoundLHS, const SCEV *FoundRHS);
  444. /// isImpliedCondOperandsHelper - Test whether the condition described by
  445. /// Pred, LHS, and RHS is true whenever the condition described by Pred,
  446. /// FoundLHS, and FoundRHS is true.
  447. bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
  448. const SCEV *LHS, const SCEV *RHS,
  449. const SCEV *FoundLHS,
  450. const SCEV *FoundRHS);
  451. /// isImpliedCondOperandsViaRanges - Test whether the condition described by
  452. /// Pred, LHS, and RHS is true whenever the condition described by Pred,
  453. /// FoundLHS, and FoundRHS is true. Utility function used by
  454. /// isImpliedCondOperands.
  455. bool isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred,
  456. const SCEV *LHS, const SCEV *RHS,
  457. const SCEV *FoundLHS,
  458. const SCEV *FoundRHS);
  459. /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
  460. /// in the header of its containing loop, we know the loop executes a
  461. /// constant number of times, and the PHI node is just a recurrence
  462. /// involving constants, fold it.
  463. Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
  464. const Loop *L);
  465. /// isKnownPredicateWithRanges - Test if the given expression is known to
  466. /// satisfy the condition described by Pred and the known constant ranges
  467. /// of LHS and RHS.
  468. ///
  469. bool isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
  470. const SCEV *LHS, const SCEV *RHS);
  471. /// forgetMemoizedResults - Drop memoized information computed for S.
  472. void forgetMemoizedResults(const SCEV *S);
  473. /// Return false iff given SCEV contains a SCEVUnknown with NULL value-
  474. /// pointer.
  475. bool checkValidity(const SCEV *S) const;
  476. // Return true if `ExtendOpTy`({`Start`,+,`Step`}) can be proved to be equal
  477. // to {`ExtendOpTy`(`Start`),+,`ExtendOpTy`(`Step`)}. This is equivalent to
  478. // proving no signed (resp. unsigned) wrap in {`Start`,+,`Step`} if
  479. // `ExtendOpTy` is `SCEVSignExtendExpr` (resp. `SCEVZeroExtendExpr`).
  480. //
  481. template<typename ExtendOpTy>
  482. bool proveNoWrapByVaryingStart(const SCEV *Start, const SCEV *Step,
  483. const Loop *L);
  484. public:
  485. static char ID; // Pass identification, replacement for typeid
  486. ScalarEvolution();
  487. LLVMContext &getContext() const { return F->getContext(); }
  488. /// isSCEVable - Test if values of the given type are analyzable within
  489. /// the SCEV framework. This primarily includes integer types, and it
  490. /// can optionally include pointer types if the ScalarEvolution class
  491. /// has access to target-specific information.
  492. bool isSCEVable(Type *Ty) const;
  493. /// getTypeSizeInBits - Return the size in bits of the specified type,
  494. /// for which isSCEVable must return true.
  495. uint64_t getTypeSizeInBits(Type *Ty) const;
  496. /// getEffectiveSCEVType - Return a type with the same bitwidth as
  497. /// the given type and which represents how SCEV will treat the given
  498. /// type, for which isSCEVable must return true. For pointer types,
  499. /// this is the pointer-sized integer type.
  500. Type *getEffectiveSCEVType(Type *Ty) const;
  501. /// getSCEV - Return a SCEV expression for the full generality of the
  502. /// specified expression.
  503. const SCEV *getSCEV(Value *V);
  504. const SCEV *getConstant(ConstantInt *V);
  505. const SCEV *getConstant(const APInt& Val);
  506. const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false);
  507. const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty);
  508. const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty);
  509. const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty);
  510. const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty);
  511. const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
  512. SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
  513. const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
  514. SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
  515. SmallVector<const SCEV *, 2> Ops;
  516. Ops.push_back(LHS);
  517. Ops.push_back(RHS);
  518. return getAddExpr(Ops, Flags);
  519. }
  520. const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
  521. SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
  522. SmallVector<const SCEV *, 3> Ops;
  523. Ops.push_back(Op0);
  524. Ops.push_back(Op1);
  525. Ops.push_back(Op2);
  526. return getAddExpr(Ops, Flags);
  527. }
  528. const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
  529. SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
  530. const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
  531. SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap)
  532. {
  533. SmallVector<const SCEV *, 2> Ops;
  534. Ops.push_back(LHS);
  535. Ops.push_back(RHS);
  536. return getMulExpr(Ops, Flags);
  537. }
  538. const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
  539. SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
  540. SmallVector<const SCEV *, 3> Ops;
  541. Ops.push_back(Op0);
  542. Ops.push_back(Op1);
  543. Ops.push_back(Op2);
  544. return getMulExpr(Ops, Flags);
  545. }
  546. const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
  547. const SCEV *getUDivExactExpr(const SCEV *LHS, const SCEV *RHS);
  548. const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
  549. const Loop *L, SCEV::NoWrapFlags Flags);
  550. const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
  551. const Loop *L, SCEV::NoWrapFlags Flags);
  552. const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
  553. const Loop *L, SCEV::NoWrapFlags Flags) {
  554. SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
  555. return getAddRecExpr(NewOp, L, Flags);
  556. }
  557. /// \brief Returns an expression for a GEP
  558. ///
  559. /// \p PointeeType The type used as the basis for the pointer arithmetics
  560. /// \p BaseExpr The expression for the pointer operand.
  561. /// \p IndexExprs The expressions for the indices.
  562. /// \p InBounds Whether the GEP is in bounds.
  563. const SCEV *getGEPExpr(Type *PointeeType, const SCEV *BaseExpr,
  564. const SmallVectorImpl<const SCEV *> &IndexExprs,
  565. bool InBounds = false);
  566. const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
  567. const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
  568. const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
  569. const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
  570. const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
  571. const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
  572. const SCEV *getUnknown(Value *V);
  573. const SCEV *getCouldNotCompute();
  574. /// getSizeOfExpr - Return an expression for sizeof AllocTy that is type
  575. /// IntTy
  576. ///
  577. const SCEV *getSizeOfExpr(Type *IntTy, Type *AllocTy);
  578. /// getOffsetOfExpr - Return an expression for offsetof on the given field
  579. /// with type IntTy
  580. ///
  581. const SCEV *getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo);
  582. /// getNegativeSCEV - Return the SCEV object corresponding to -V.
  583. ///
  584. const SCEV *getNegativeSCEV(const SCEV *V);
  585. /// getNotSCEV - Return the SCEV object corresponding to ~V.
  586. ///
  587. const SCEV *getNotSCEV(const SCEV *V);
  588. /// getMinusSCEV - Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
  589. const SCEV *getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
  590. SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
  591. /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
  592. /// of the input value to the specified type. If the type must be
  593. /// extended, it is zero extended.
  594. const SCEV *getTruncateOrZeroExtend(const SCEV *V, Type *Ty);
  595. /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
  596. /// of the input value to the specified type. If the type must be
  597. /// extended, it is sign extended.
  598. const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty);
  599. /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of
  600. /// the input value to the specified type. If the type must be extended,
  601. /// it is zero extended. The conversion must not be narrowing.
  602. const SCEV *getNoopOrZeroExtend(const SCEV *V, Type *Ty);
  603. /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of
  604. /// the input value to the specified type. If the type must be extended,
  605. /// it is sign extended. The conversion must not be narrowing.
  606. const SCEV *getNoopOrSignExtend(const SCEV *V, Type *Ty);
  607. /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
  608. /// the input value to the specified type. If the type must be extended,
  609. /// it is extended with unspecified bits. The conversion must not be
  610. /// narrowing.
  611. const SCEV *getNoopOrAnyExtend(const SCEV *V, Type *Ty);
  612. /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
  613. /// input value to the specified type. The conversion must not be
  614. /// widening.
  615. const SCEV *getTruncateOrNoop(const SCEV *V, Type *Ty);
  616. /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
  617. /// the types using zero-extension, and then perform a umax operation
  618. /// with them.
  619. const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS,
  620. const SCEV *RHS);
  621. /// getUMinFromMismatchedTypes - Promote the operands to the wider of
  622. /// the types using zero-extension, and then perform a umin operation
  623. /// with them.
  624. const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS,
  625. const SCEV *RHS);
  626. /// getPointerBase - Transitively follow the chain of pointer-type operands
  627. /// until reaching a SCEV that does not have a single pointer operand. This
  628. /// returns a SCEVUnknown pointer for well-formed pointer-type expressions,
  629. /// but corner cases do exist.
  630. const SCEV *getPointerBase(const SCEV *V);
  631. /// getSCEVAtScope - Return a SCEV expression for the specified value
  632. /// at the specified scope in the program. The L value specifies a loop
  633. /// nest to evaluate the expression at, where null is the top-level or a
  634. /// specified loop is immediately inside of the loop.
  635. ///
  636. /// This method can be used to compute the exit value for a variable defined
  637. /// in a loop by querying what the value will hold in the parent loop.
  638. ///
  639. /// In the case that a relevant loop exit value cannot be computed, the
  640. /// original value V is returned.
  641. const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
  642. /// getSCEVAtScope - This is a convenience function which does
  643. /// getSCEVAtScope(getSCEV(V), L).
  644. const SCEV *getSCEVAtScope(Value *V, const Loop *L);
  645. /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected
  646. /// by a conditional between LHS and RHS. This is used to help avoid max
  647. /// expressions in loop trip counts, and to eliminate casts.
  648. bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
  649. const SCEV *LHS, const SCEV *RHS);
  650. /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
  651. /// protected by a conditional between LHS and RHS. This is used to
  652. /// to eliminate casts.
  653. bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
  654. const SCEV *LHS, const SCEV *RHS);
  655. /// \brief Returns the maximum trip count of the loop if it is a single-exit
  656. /// loop and we can compute a small maximum for that loop.
  657. ///
  658. /// Implemented in terms of the \c getSmallConstantTripCount overload with
  659. /// the single exiting block passed to it. See that routine for details.
  660. unsigned getSmallConstantTripCount(Loop *L);
  661. /// getSmallConstantTripCount - Returns the maximum trip count of this loop
  662. /// as a normal unsigned value. Returns 0 if the trip count is unknown or
  663. /// not constant. This "trip count" assumes that control exits via
  664. /// ExitingBlock. More precisely, it is the number of times that control may
  665. /// reach ExitingBlock before taking the branch. For loops with multiple
  666. /// exits, it may not be the number times that the loop header executes if
  667. /// the loop exits prematurely via another branch.
  668. unsigned getSmallConstantTripCount(Loop *L, BasicBlock *ExitingBlock);
  669. /// \brief Returns the largest constant divisor of the trip count of the
  670. /// loop if it is a single-exit loop and we can compute a small maximum for
  671. /// that loop.
  672. ///
  673. /// Implemented in terms of the \c getSmallConstantTripMultiple overload with
  674. /// the single exiting block passed to it. See that routine for details.
  675. unsigned getSmallConstantTripMultiple(Loop *L);
  676. /// getSmallConstantTripMultiple - Returns the largest constant divisor of
  677. /// the trip count of this loop as a normal unsigned value, if
  678. /// possible. This means that the actual trip count is always a multiple of
  679. /// the returned value (don't forget the trip count could very well be zero
  680. /// as well!). As explained in the comments for getSmallConstantTripCount,
  681. /// this assumes that control exits the loop via ExitingBlock.
  682. unsigned getSmallConstantTripMultiple(Loop *L, BasicBlock *ExitingBlock);
  683. // getExitCount - Get the expression for the number of loop iterations for
  684. // which this loop is guaranteed not to exit via ExitingBlock. Otherwise
  685. // return SCEVCouldNotCompute.
  686. const SCEV *getExitCount(Loop *L, BasicBlock *ExitingBlock);
  687. /// getBackedgeTakenCount - If the specified loop has a predictable
  688. /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
  689. /// object. The backedge-taken count is the number of times the loop header
  690. /// will be branched to from within the loop. This is one less than the
  691. /// trip count of the loop, since it doesn't count the first iteration,
  692. /// when the header is branched to from outside the loop.
  693. ///
  694. /// Note that it is not valid to call this method on a loop without a
  695. /// loop-invariant backedge-taken count (see
  696. /// hasLoopInvariantBackedgeTakenCount).
  697. ///
  698. const SCEV *getBackedgeTakenCount(const Loop *L);
  699. /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
  700. /// return the least SCEV value that is known never to be less than the
  701. /// actual backedge taken count.
  702. const SCEV *getMaxBackedgeTakenCount(const Loop *L);
  703. /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
  704. /// has an analyzable loop-invariant backedge-taken count.
  705. bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
  706. /// forgetLoop - This method should be called by the client when it has
  707. /// changed a loop in a way that may effect ScalarEvolution's ability to
  708. /// compute a trip count, or if the loop is deleted. This call is
  709. /// potentially expensive for large loop bodies.
  710. void forgetLoop(const Loop *L);
  711. /// forgetValue - This method should be called by the client when it has
  712. /// changed a value in a way that may effect its value, or which may
  713. /// disconnect it from a def-use chain linking it to a loop.
  714. void forgetValue(Value *V);
  715. /// \brief Called when the client has changed the disposition of values in
  716. /// this loop.
  717. ///
  718. /// We don't have a way to invalidate per-loop dispositions. Clear and
  719. /// recompute is simpler.
  720. void forgetLoopDispositions(const Loop *L) { LoopDispositions.clear(); }
  721. /// GetMinTrailingZeros - Determine the minimum number of zero bits that S
  722. /// is guaranteed to end in (at every loop iteration). It is, at the same
  723. /// time, the minimum number of times S is divisible by 2. For example,
  724. /// given {4,+,8} it returns 2. If S is guaranteed to be 0, it returns the
  725. /// bitwidth of S.
  726. uint32_t GetMinTrailingZeros(const SCEV *S);
  727. /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
  728. ///
  729. ConstantRange getUnsignedRange(const SCEV *S) {
  730. return getRange(S, HINT_RANGE_UNSIGNED);
  731. }
  732. /// getSignedRange - Determine the signed range for a particular SCEV.
  733. ///
  734. ConstantRange getSignedRange(const SCEV *S) {
  735. return getRange(S, HINT_RANGE_SIGNED);
  736. }
  737. /// isKnownNegative - Test if the given expression is known to be negative.
  738. ///
  739. bool isKnownNegative(const SCEV *S);
  740. /// isKnownPositive - Test if the given expression is known to be positive.
  741. ///
  742. bool isKnownPositive(const SCEV *S);
  743. /// isKnownNonNegative - Test if the given expression is known to be
  744. /// non-negative.
  745. ///
  746. bool isKnownNonNegative(const SCEV *S);
  747. /// isKnownNonPositive - Test if the given expression is known to be
  748. /// non-positive.
  749. ///
  750. bool isKnownNonPositive(const SCEV *S);
  751. /// isKnownNonZero - Test if the given expression is known to be
  752. /// non-zero.
  753. ///
  754. bool isKnownNonZero(const SCEV *S);
  755. /// isKnownPredicate - Test if the given expression is known to satisfy
  756. /// the condition described by Pred, LHS, and RHS.
  757. ///
  758. bool isKnownPredicate(ICmpInst::Predicate Pred,
  759. const SCEV *LHS, const SCEV *RHS);
  760. /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with
  761. /// predicate Pred. Return true iff any changes were made. If the
  762. /// operands are provably equal or unequal, LHS and RHS are set to
  763. /// the same value and Pred is set to either ICMP_EQ or ICMP_NE.
  764. ///
  765. bool SimplifyICmpOperands(ICmpInst::Predicate &Pred,
  766. const SCEV *&LHS,
  767. const SCEV *&RHS,
  768. unsigned Depth = 0);
  769. /// getLoopDisposition - Return the "disposition" of the given SCEV with
  770. /// respect to the given loop.
  771. LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L);
  772. /// isLoopInvariant - Return true if the value of the given SCEV is
  773. /// unchanging in the specified loop.
  774. bool isLoopInvariant(const SCEV *S, const Loop *L);
  775. /// hasComputableLoopEvolution - Return true if the given SCEV changes value
  776. /// in a known way in the specified loop. This property being true implies
  777. /// that the value is variant in the loop AND that we can emit an expression
  778. /// to compute the value of the expression at any particular loop iteration.
  779. bool hasComputableLoopEvolution(const SCEV *S, const Loop *L);
  780. /// getLoopDisposition - Return the "disposition" of the given SCEV with
  781. /// respect to the given block.
  782. BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB);
  783. /// dominates - Return true if elements that makes up the given SCEV
  784. /// dominate the specified basic block.
  785. bool dominates(const SCEV *S, const BasicBlock *BB);
  786. /// properlyDominates - Return true if elements that makes up the given SCEV
  787. /// properly dominate the specified basic block.
  788. bool properlyDominates(const SCEV *S, const BasicBlock *BB);
  789. /// hasOperand - Test whether the given SCEV has Op as a direct or
  790. /// indirect operand.
  791. bool hasOperand(const SCEV *S, const SCEV *Op) const;
  792. /// Return the size of an element read or written by Inst.
  793. const SCEV *getElementSize(Instruction *Inst);
  794. /// Compute the array dimensions Sizes from the set of Terms extracted from
  795. /// the memory access function of this SCEVAddRecExpr.
  796. void findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
  797. SmallVectorImpl<const SCEV *> &Sizes,
  798. const SCEV *ElementSize) const;
  799. bool runOnFunction(Function &F) override;
  800. void releaseMemory() override;
  801. void getAnalysisUsage(AnalysisUsage &AU) const override;
  802. void print(raw_ostream &OS, const Module* = nullptr) const override;
  803. void verifyAnalysis() const override;
  804. /// Collect parametric terms occurring in step expressions.
  805. void collectParametricTerms(const SCEV *Expr,
  806. SmallVectorImpl<const SCEV *> &Terms);
  807. /// Return in Subscripts the access functions for each dimension in Sizes.
  808. void computeAccessFunctions(const SCEV *Expr,
  809. SmallVectorImpl<const SCEV *> &Subscripts,
  810. SmallVectorImpl<const SCEV *> &Sizes);
  811. /// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
  812. /// subscripts and sizes of an array access.
  813. ///
  814. /// The delinearization is a 3 step process: the first two steps compute the
  815. /// sizes of each subscript and the third step computes the access functions
  816. /// for the delinearized array:
  817. ///
  818. /// 1. Find the terms in the step functions
  819. /// 2. Compute the array size
  820. /// 3. Compute the access function: divide the SCEV by the array size
  821. /// starting with the innermost dimensions found in step 2. The Quotient
  822. /// is the SCEV to be divided in the next step of the recursion. The
  823. /// Remainder is the subscript of the innermost dimension. Loop over all
  824. /// array dimensions computed in step 2.
  825. ///
  826. /// To compute a uniform array size for several memory accesses to the same
  827. /// object, one can collect in step 1 all the step terms for all the memory
  828. /// accesses, and compute in step 2 a unique array shape. This guarantees
  829. /// that the array shape will be the same across all memory accesses.
  830. ///
  831. /// FIXME: We could derive the result of steps 1 and 2 from a description of
  832. /// the array shape given in metadata.
  833. ///
  834. /// Example:
  835. ///
  836. /// A[][n][m]
  837. ///
  838. /// for i
  839. /// for j
  840. /// for k
  841. /// A[j+k][2i][5i] =
  842. ///
  843. /// The initial SCEV:
  844. ///
  845. /// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k]
  846. ///
  847. /// 1. Find the different terms in the step functions:
  848. /// -> [2*m, 5, n*m, n*m]
  849. ///
  850. /// 2. Compute the array size: sort and unique them
  851. /// -> [n*m, 2*m, 5]
  852. /// find the GCD of all the terms = 1
  853. /// divide by the GCD and erase constant terms
  854. /// -> [n*m, 2*m]
  855. /// GCD = m
  856. /// divide by GCD -> [n, 2]
  857. /// remove constant terms
  858. /// -> [n]
  859. /// size of the array is A[unknown][n][m]
  860. ///
  861. /// 3. Compute the access function
  862. /// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m
  863. /// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k
  864. /// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k
  865. /// The remainder is the subscript of the innermost array dimension: [5i].
  866. ///
  867. /// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n
  868. /// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k
  869. /// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k
  870. /// The Remainder is the subscript of the next array dimension: [2i].
  871. ///
  872. /// The subscript of the outermost dimension is the Quotient: [j+k].
  873. ///
  874. /// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
  875. void delinearize(const SCEV *Expr,
  876. SmallVectorImpl<const SCEV *> &Subscripts,
  877. SmallVectorImpl<const SCEV *> &Sizes,
  878. const SCEV *ElementSize);
  879. private:
  880. /// Compute the backedge taken count knowing the interval difference, the
  881. /// stride and presence of the equality in the comparison.
  882. const SCEV *computeBECount(const SCEV *Delta, const SCEV *Stride,
  883. bool Equality);
  884. /// Verify if an linear IV with positive stride can overflow when in a
  885. /// less-than comparison, knowing the invariant term of the comparison,
  886. /// the stride and the knowledge of NSW/NUW flags on the recurrence.
  887. bool doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
  888. bool IsSigned, bool NoWrap);
  889. /// Verify if an linear IV with negative stride can overflow when in a
  890. /// greater-than comparison, knowing the invariant term of the comparison,
  891. /// the stride and the knowledge of NSW/NUW flags on the recurrence.
  892. bool doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
  893. bool IsSigned, bool NoWrap);
  894. private:
  895. FoldingSet<SCEV> UniqueSCEVs;
  896. BumpPtrAllocator SCEVAllocator;
  897. /// FirstUnknown - The head of a linked list of all SCEVUnknown
  898. /// values that have been allocated. This is used by releaseMemory
  899. /// to locate them all and call their destructors.
  900. SCEVUnknown *FirstUnknown;
  901. };
  902. }
  903. #endif