DependenceAnalysis.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Analysis/DependenceAnalysis.h -------------------- -*- 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. // DependenceAnalysis is an LLVM pass that analyses dependences between memory
  15. // accesses. Currently, it is an implementation of the approach described in
  16. //
  17. // Practical Dependence Testing
  18. // Goff, Kennedy, Tseng
  19. // PLDI 1991
  20. //
  21. // There's a single entry point that analyzes the dependence between a pair
  22. // of memory references in a function, returning either NULL, for no dependence,
  23. // or a more-or-less detailed description of the dependence between them.
  24. //
  25. // This pass exists to support the DependenceGraph pass. There are two separate
  26. // passes because there's a useful separation of concerns. A dependence exists
  27. // if two conditions are met:
  28. //
  29. // 1) Two instructions reference the same memory location, and
  30. // 2) There is a flow of control leading from one instruction to the other.
  31. //
  32. // DependenceAnalysis attacks the first condition; DependenceGraph will attack
  33. // the second (it's not yet ready).
  34. //
  35. // Please note that this is work in progress and the interface is subject to
  36. // change.
  37. //
  38. // Plausible changes:
  39. // Return a set of more precise dependences instead of just one dependence
  40. // summarizing all.
  41. //
  42. //===----------------------------------------------------------------------===//
  43. #ifndef LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
  44. #define LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
  45. #include "llvm/ADT/SmallBitVector.h"
  46. #include "llvm/IR/Instructions.h"
  47. #include "llvm/IR/PassManager.h"
  48. #include "llvm/Pass.h"
  49. namespace llvm {
  50. class AAResults;
  51. template <typename T> class ArrayRef;
  52. class Loop;
  53. class LoopInfo;
  54. class ScalarEvolution;
  55. class SCEV;
  56. class SCEVConstant;
  57. class raw_ostream;
  58. /// Dependence - This class represents a dependence between two memory
  59. /// memory references in a function. It contains minimal information and
  60. /// is used in the very common situation where the compiler is unable to
  61. /// determine anything beyond the existence of a dependence; that is, it
  62. /// represents a confused dependence (see also FullDependence). In most
  63. /// cases (for output, flow, and anti dependences), the dependence implies
  64. /// an ordering, where the source must precede the destination; in contrast,
  65. /// input dependences are unordered.
  66. ///
  67. /// When a dependence graph is built, each Dependence will be a member of
  68. /// the set of predecessor edges for its destination instruction and a set
  69. /// if successor edges for its source instruction. These sets are represented
  70. /// as singly-linked lists, with the "next" fields stored in the dependence
  71. /// itelf.
  72. class Dependence {
  73. protected:
  74. Dependence(Dependence &&) = default;
  75. Dependence &operator=(Dependence &&) = default;
  76. public:
  77. Dependence(Instruction *Source,
  78. Instruction *Destination) :
  79. Src(Source),
  80. Dst(Destination),
  81. NextPredecessor(nullptr),
  82. NextSuccessor(nullptr) {}
  83. virtual ~Dependence() {}
  84. /// Dependence::DVEntry - Each level in the distance/direction vector
  85. /// has a direction (or perhaps a union of several directions), and
  86. /// perhaps a distance.
  87. struct DVEntry {
  88. enum { NONE = 0,
  89. LT = 1,
  90. EQ = 2,
  91. LE = 3,
  92. GT = 4,
  93. NE = 5,
  94. GE = 6,
  95. ALL = 7 };
  96. unsigned char Direction : 3; // Init to ALL, then refine.
  97. bool Scalar : 1; // Init to true.
  98. bool PeelFirst : 1; // Peeling the first iteration will break dependence.
  99. bool PeelLast : 1; // Peeling the last iteration will break the dependence.
  100. bool Splitable : 1; // Splitting the loop will break dependence.
  101. const SCEV *Distance; // NULL implies no distance available.
  102. DVEntry() : Direction(ALL), Scalar(true), PeelFirst(false),
  103. PeelLast(false), Splitable(false), Distance(nullptr) { }
  104. };
  105. /// getSrc - Returns the source instruction for this dependence.
  106. ///
  107. Instruction *getSrc() const { return Src; }
  108. /// getDst - Returns the destination instruction for this dependence.
  109. ///
  110. Instruction *getDst() const { return Dst; }
  111. /// isInput - Returns true if this is an input dependence.
  112. ///
  113. bool isInput() const;
  114. /// isOutput - Returns true if this is an output dependence.
  115. ///
  116. bool isOutput() const;
  117. /// isFlow - Returns true if this is a flow (aka true) dependence.
  118. ///
  119. bool isFlow() const;
  120. /// isAnti - Returns true if this is an anti dependence.
  121. ///
  122. bool isAnti() const;
  123. /// isOrdered - Returns true if dependence is Output, Flow, or Anti
  124. ///
  125. bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
  126. /// isUnordered - Returns true if dependence is Input
  127. ///
  128. bool isUnordered() const { return isInput(); }
  129. /// isLoopIndependent - Returns true if this is a loop-independent
  130. /// dependence.
  131. virtual bool isLoopIndependent() const { return true; }
  132. /// isConfused - Returns true if this dependence is confused
  133. /// (the compiler understands nothing and makes worst-case
  134. /// assumptions).
  135. virtual bool isConfused() const { return true; }
  136. /// isConsistent - Returns true if this dependence is consistent
  137. /// (occurs every time the source and destination are executed).
  138. virtual bool isConsistent() const { return false; }
  139. /// getLevels - Returns the number of common loops surrounding the
  140. /// source and destination of the dependence.
  141. virtual unsigned getLevels() const { return 0; }
  142. /// getDirection - Returns the direction associated with a particular
  143. /// level.
  144. virtual unsigned getDirection(unsigned Level) const { return DVEntry::ALL; }
  145. /// getDistance - Returns the distance (or NULL) associated with a
  146. /// particular level.
  147. virtual const SCEV *getDistance(unsigned Level) const { return nullptr; }
  148. /// isPeelFirst - Returns true if peeling the first iteration from
  149. /// this loop will break this dependence.
  150. virtual bool isPeelFirst(unsigned Level) const { return false; }
  151. /// isPeelLast - Returns true if peeling the last iteration from
  152. /// this loop will break this dependence.
  153. virtual bool isPeelLast(unsigned Level) const { return false; }
  154. /// isSplitable - Returns true if splitting this loop will break
  155. /// the dependence.
  156. virtual bool isSplitable(unsigned Level) const { return false; }
  157. /// isScalar - Returns true if a particular level is scalar; that is,
  158. /// if no subscript in the source or destination mention the induction
  159. /// variable associated with the loop at this level.
  160. virtual bool isScalar(unsigned Level) const;
  161. /// getNextPredecessor - Returns the value of the NextPredecessor
  162. /// field.
  163. const Dependence *getNextPredecessor() const { return NextPredecessor; }
  164. /// getNextSuccessor - Returns the value of the NextSuccessor
  165. /// field.
  166. const Dependence *getNextSuccessor() const { return NextSuccessor; }
  167. /// setNextPredecessor - Sets the value of the NextPredecessor
  168. /// field.
  169. void setNextPredecessor(const Dependence *pred) { NextPredecessor = pred; }
  170. /// setNextSuccessor - Sets the value of the NextSuccessor
  171. /// field.
  172. void setNextSuccessor(const Dependence *succ) { NextSuccessor = succ; }
  173. /// dump - For debugging purposes, dumps a dependence to OS.
  174. ///
  175. void dump(raw_ostream &OS) const;
  176. private:
  177. Instruction *Src, *Dst;
  178. const Dependence *NextPredecessor, *NextSuccessor;
  179. friend class DependenceInfo;
  180. };
  181. /// FullDependence - This class represents a dependence between two memory
  182. /// references in a function. It contains detailed information about the
  183. /// dependence (direction vectors, etc.) and is used when the compiler is
  184. /// able to accurately analyze the interaction of the references; that is,
  185. /// it is not a confused dependence (see Dependence). In most cases
  186. /// (for output, flow, and anti dependences), the dependence implies an
  187. /// ordering, where the source must precede the destination; in contrast,
  188. /// input dependences are unordered.
  189. class FullDependence final : public Dependence {
  190. public:
  191. FullDependence(Instruction *Src, Instruction *Dst, bool LoopIndependent,
  192. unsigned Levels);
  193. /// isLoopIndependent - Returns true if this is a loop-independent
  194. /// dependence.
  195. bool isLoopIndependent() const override { return LoopIndependent; }
  196. /// isConfused - Returns true if this dependence is confused
  197. /// (the compiler understands nothing and makes worst-case
  198. /// assumptions).
  199. bool isConfused() const override { return false; }
  200. /// isConsistent - Returns true if this dependence is consistent
  201. /// (occurs every time the source and destination are executed).
  202. bool isConsistent() const override { return Consistent; }
  203. /// getLevels - Returns the number of common loops surrounding the
  204. /// source and destination of the dependence.
  205. unsigned getLevels() const override { return Levels; }
  206. /// getDirection - Returns the direction associated with a particular
  207. /// level.
  208. unsigned getDirection(unsigned Level) const override;
  209. /// getDistance - Returns the distance (or NULL) associated with a
  210. /// particular level.
  211. const SCEV *getDistance(unsigned Level) const override;
  212. /// isPeelFirst - Returns true if peeling the first iteration from
  213. /// this loop will break this dependence.
  214. bool isPeelFirst(unsigned Level) const override;
  215. /// isPeelLast - Returns true if peeling the last iteration from
  216. /// this loop will break this dependence.
  217. bool isPeelLast(unsigned Level) const override;
  218. /// isSplitable - Returns true if splitting the loop will break
  219. /// the dependence.
  220. bool isSplitable(unsigned Level) const override;
  221. /// isScalar - Returns true if a particular level is scalar; that is,
  222. /// if no subscript in the source or destination mention the induction
  223. /// variable associated with the loop at this level.
  224. bool isScalar(unsigned Level) const override;
  225. private:
  226. unsigned short Levels;
  227. bool LoopIndependent;
  228. bool Consistent; // Init to true, then refine.
  229. std::unique_ptr<DVEntry[]> DV;
  230. friend class DependenceInfo;
  231. };
  232. /// DependenceInfo - This class is the main dependence-analysis driver.
  233. ///
  234. class DependenceInfo {
  235. public:
  236. DependenceInfo(Function *F, AAResults *AA, ScalarEvolution *SE,
  237. LoopInfo *LI)
  238. : AA(AA), SE(SE), LI(LI), F(F) {}
  239. /// Handle transitive invalidation when the cached analysis results go away.
  240. bool invalidate(Function &F, const PreservedAnalyses &PA,
  241. FunctionAnalysisManager::Invalidator &Inv);
  242. /// depends - Tests for a dependence between the Src and Dst instructions.
  243. /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
  244. /// FullDependence) with as much information as can be gleaned.
  245. /// The flag PossiblyLoopIndependent should be set by the caller
  246. /// if it appears that control flow can reach from Src to Dst
  247. /// without traversing a loop back edge.
  248. std::unique_ptr<Dependence> depends(Instruction *Src,
  249. Instruction *Dst,
  250. bool PossiblyLoopIndependent);
  251. /// getSplitIteration - Give a dependence that's splittable at some
  252. /// particular level, return the iteration that should be used to split
  253. /// the loop.
  254. ///
  255. /// Generally, the dependence analyzer will be used to build
  256. /// a dependence graph for a function (basically a map from instructions
  257. /// to dependences). Looking for cycles in the graph shows us loops
  258. /// that cannot be trivially vectorized/parallelized.
  259. ///
  260. /// We can try to improve the situation by examining all the dependences
  261. /// that make up the cycle, looking for ones we can break.
  262. /// Sometimes, peeling the first or last iteration of a loop will break
  263. /// dependences, and there are flags for those possibilities.
  264. /// Sometimes, splitting a loop at some other iteration will do the trick,
  265. /// and we've got a flag for that case. Rather than waste the space to
  266. /// record the exact iteration (since we rarely know), we provide
  267. /// a method that calculates the iteration. It's a drag that it must work
  268. /// from scratch, but wonderful in that it's possible.
  269. ///
  270. /// Here's an example:
  271. ///
  272. /// for (i = 0; i < 10; i++)
  273. /// A[i] = ...
  274. /// ... = A[11 - i]
  275. ///
  276. /// There's a loop-carried flow dependence from the store to the load,
  277. /// found by the weak-crossing SIV test. The dependence will have a flag,
  278. /// indicating that the dependence can be broken by splitting the loop.
  279. /// Calling getSplitIteration will return 5.
  280. /// Splitting the loop breaks the dependence, like so:
  281. ///
  282. /// for (i = 0; i <= 5; i++)
  283. /// A[i] = ...
  284. /// ... = A[11 - i]
  285. /// for (i = 6; i < 10; i++)
  286. /// A[i] = ...
  287. /// ... = A[11 - i]
  288. ///
  289. /// breaks the dependence and allows us to vectorize/parallelize
  290. /// both loops.
  291. const SCEV *getSplitIteration(const Dependence &Dep, unsigned Level);
  292. Function *getFunction() const { return F; }
  293. private:
  294. AAResults *AA;
  295. ScalarEvolution *SE;
  296. LoopInfo *LI;
  297. Function *F;
  298. /// Subscript - This private struct represents a pair of subscripts from
  299. /// a pair of potentially multi-dimensional array references. We use a
  300. /// vector of them to guide subscript partitioning.
  301. struct Subscript {
  302. const SCEV *Src;
  303. const SCEV *Dst;
  304. enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
  305. SmallBitVector Loops;
  306. SmallBitVector GroupLoops;
  307. SmallBitVector Group;
  308. };
  309. struct CoefficientInfo {
  310. const SCEV *Coeff;
  311. const SCEV *PosPart;
  312. const SCEV *NegPart;
  313. const SCEV *Iterations;
  314. };
  315. struct BoundInfo {
  316. const SCEV *Iterations;
  317. const SCEV *Upper[8];
  318. const SCEV *Lower[8];
  319. unsigned char Direction;
  320. unsigned char DirSet;
  321. };
  322. /// Constraint - This private class represents a constraint, as defined
  323. /// in the paper
  324. ///
  325. /// Practical Dependence Testing
  326. /// Goff, Kennedy, Tseng
  327. /// PLDI 1991
  328. ///
  329. /// There are 5 kinds of constraint, in a hierarchy.
  330. /// 1) Any - indicates no constraint, any dependence is possible.
  331. /// 2) Line - A line ax + by = c, where a, b, and c are parameters,
  332. /// representing the dependence equation.
  333. /// 3) Distance - The value d of the dependence distance;
  334. /// 4) Point - A point <x, y> representing the dependence from
  335. /// iteration x to iteration y.
  336. /// 5) Empty - No dependence is possible.
  337. class Constraint {
  338. private:
  339. enum ConstraintKind { Empty, Point, Distance, Line, Any } Kind;
  340. ScalarEvolution *SE;
  341. const SCEV *A;
  342. const SCEV *B;
  343. const SCEV *C;
  344. const Loop *AssociatedLoop;
  345. public:
  346. /// isEmpty - Return true if the constraint is of kind Empty.
  347. bool isEmpty() const { return Kind == Empty; }
  348. /// isPoint - Return true if the constraint is of kind Point.
  349. bool isPoint() const { return Kind == Point; }
  350. /// isDistance - Return true if the constraint is of kind Distance.
  351. bool isDistance() const { return Kind == Distance; }
  352. /// isLine - Return true if the constraint is of kind Line.
  353. /// Since Distance's can also be represented as Lines, we also return
  354. /// true if the constraint is of kind Distance.
  355. bool isLine() const { return Kind == Line || Kind == Distance; }
  356. /// isAny - Return true if the constraint is of kind Any;
  357. bool isAny() const { return Kind == Any; }
  358. /// getX - If constraint is a point <X, Y>, returns X.
  359. /// Otherwise assert.
  360. const SCEV *getX() const;
  361. /// getY - If constraint is a point <X, Y>, returns Y.
  362. /// Otherwise assert.
  363. const SCEV *getY() const;
  364. /// getA - If constraint is a line AX + BY = C, returns A.
  365. /// Otherwise assert.
  366. const SCEV *getA() const;
  367. /// getB - If constraint is a line AX + BY = C, returns B.
  368. /// Otherwise assert.
  369. const SCEV *getB() const;
  370. /// getC - If constraint is a line AX + BY = C, returns C.
  371. /// Otherwise assert.
  372. const SCEV *getC() const;
  373. /// getD - If constraint is a distance, returns D.
  374. /// Otherwise assert.
  375. const SCEV *getD() const;
  376. /// getAssociatedLoop - Returns the loop associated with this constraint.
  377. const Loop *getAssociatedLoop() const;
  378. /// setPoint - Change a constraint to Point.
  379. void setPoint(const SCEV *X, const SCEV *Y, const Loop *CurrentLoop);
  380. /// setLine - Change a constraint to Line.
  381. void setLine(const SCEV *A, const SCEV *B,
  382. const SCEV *C, const Loop *CurrentLoop);
  383. /// setDistance - Change a constraint to Distance.
  384. void setDistance(const SCEV *D, const Loop *CurrentLoop);
  385. /// setEmpty - Change a constraint to Empty.
  386. void setEmpty();
  387. /// setAny - Change a constraint to Any.
  388. void setAny(ScalarEvolution *SE);
  389. /// dump - For debugging purposes. Dumps the constraint
  390. /// out to OS.
  391. void dump(raw_ostream &OS) const;
  392. };
  393. /// establishNestingLevels - Examines the loop nesting of the Src and Dst
  394. /// instructions and establishes their shared loops. Sets the variables
  395. /// CommonLevels, SrcLevels, and MaxLevels.
  396. /// The source and destination instructions needn't be contained in the same
  397. /// loop. The routine establishNestingLevels finds the level of most deeply
  398. /// nested loop that contains them both, CommonLevels. An instruction that's
  399. /// not contained in a loop is at level = 0. MaxLevels is equal to the level
  400. /// of the source plus the level of the destination, minus CommonLevels.
  401. /// This lets us allocate vectors MaxLevels in length, with room for every
  402. /// distinct loop referenced in both the source and destination subscripts.
  403. /// The variable SrcLevels is the nesting depth of the source instruction.
  404. /// It's used to help calculate distinct loops referenced by the destination.
  405. /// Here's the map from loops to levels:
  406. /// 0 - unused
  407. /// 1 - outermost common loop
  408. /// ... - other common loops
  409. /// CommonLevels - innermost common loop
  410. /// ... - loops containing Src but not Dst
  411. /// SrcLevels - innermost loop containing Src but not Dst
  412. /// ... - loops containing Dst but not Src
  413. /// MaxLevels - innermost loop containing Dst but not Src
  414. /// Consider the follow code fragment:
  415. /// for (a = ...) {
  416. /// for (b = ...) {
  417. /// for (c = ...) {
  418. /// for (d = ...) {
  419. /// A[] = ...;
  420. /// }
  421. /// }
  422. /// for (e = ...) {
  423. /// for (f = ...) {
  424. /// for (g = ...) {
  425. /// ... = A[];
  426. /// }
  427. /// }
  428. /// }
  429. /// }
  430. /// }
  431. /// If we're looking at the possibility of a dependence between the store
  432. /// to A (the Src) and the load from A (the Dst), we'll note that they
  433. /// have 2 loops in common, so CommonLevels will equal 2 and the direction
  434. /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
  435. /// A map from loop names to level indices would look like
  436. /// a - 1
  437. /// b - 2 = CommonLevels
  438. /// c - 3
  439. /// d - 4 = SrcLevels
  440. /// e - 5
  441. /// f - 6
  442. /// g - 7 = MaxLevels
  443. void establishNestingLevels(const Instruction *Src,
  444. const Instruction *Dst);
  445. unsigned CommonLevels, SrcLevels, MaxLevels;
  446. /// mapSrcLoop - Given one of the loops containing the source, return
  447. /// its level index in our numbering scheme.
  448. unsigned mapSrcLoop(const Loop *SrcLoop) const;
  449. /// mapDstLoop - Given one of the loops containing the destination,
  450. /// return its level index in our numbering scheme.
  451. unsigned mapDstLoop(const Loop *DstLoop) const;
  452. /// isLoopInvariant - Returns true if Expression is loop invariant
  453. /// in LoopNest.
  454. bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
  455. /// Makes sure all subscript pairs share the same integer type by
  456. /// sign-extending as necessary.
  457. /// Sign-extending a subscript is safe because getelementptr assumes the
  458. /// array subscripts are signed.
  459. void unifySubscriptType(ArrayRef<Subscript *> Pairs);
  460. /// removeMatchingExtensions - Examines a subscript pair.
  461. /// If the source and destination are identically sign (or zero)
  462. /// extended, it strips off the extension in an effort to
  463. /// simplify the actual analysis.
  464. void removeMatchingExtensions(Subscript *Pair);
  465. /// collectCommonLoops - Finds the set of loops from the LoopNest that
  466. /// have a level <= CommonLevels and are referred to by the SCEV Expression.
  467. void collectCommonLoops(const SCEV *Expression,
  468. const Loop *LoopNest,
  469. SmallBitVector &Loops) const;
  470. /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
  471. /// linear. Collect the set of loops mentioned by Src.
  472. bool checkSrcSubscript(const SCEV *Src,
  473. const Loop *LoopNest,
  474. SmallBitVector &Loops);
  475. /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
  476. /// linear. Collect the set of loops mentioned by Dst.
  477. bool checkDstSubscript(const SCEV *Dst,
  478. const Loop *LoopNest,
  479. SmallBitVector &Loops);
  480. /// isKnownPredicate - Compare X and Y using the predicate Pred.
  481. /// Basically a wrapper for SCEV::isKnownPredicate,
  482. /// but tries harder, especially in the presence of sign and zero
  483. /// extensions and symbolics.
  484. bool isKnownPredicate(ICmpInst::Predicate Pred,
  485. const SCEV *X,
  486. const SCEV *Y) const;
  487. /// isKnownLessThan - Compare to see if S is less than Size
  488. /// Another wrapper for isKnownNegative(S - max(Size, 1)) with some extra
  489. /// checking if S is an AddRec and we can prove lessthan using the loop
  490. /// bounds.
  491. bool isKnownLessThan(const SCEV *S, const SCEV *Size) const;
  492. /// isKnownNonNegative - Compare to see if S is known not to be negative
  493. /// Uses the fact that S comes from Ptr, which may be an inbound GEP,
  494. /// Proving there is no wrapping going on.
  495. bool isKnownNonNegative(const SCEV *S, const Value *Ptr) const;
  496. /// collectUpperBound - All subscripts are the same type (on my machine,
  497. /// an i64). The loop bound may be a smaller type. collectUpperBound
  498. /// find the bound, if available, and zero extends it to the Type T.
  499. /// (I zero extend since the bound should always be >= 0.)
  500. /// If no upper bound is available, return NULL.
  501. const SCEV *collectUpperBound(const Loop *l, Type *T) const;
  502. /// collectConstantUpperBound - Calls collectUpperBound(), then
  503. /// attempts to cast it to SCEVConstant. If the cast fails,
  504. /// returns NULL.
  505. const SCEVConstant *collectConstantUpperBound(const Loop *l, Type *T) const;
  506. /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
  507. /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
  508. /// Collects the associated loops in a set.
  509. Subscript::ClassificationKind classifyPair(const SCEV *Src,
  510. const Loop *SrcLoopNest,
  511. const SCEV *Dst,
  512. const Loop *DstLoopNest,
  513. SmallBitVector &Loops);
  514. /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
  515. /// Returns true if any possible dependence is disproved.
  516. /// If there might be a dependence, returns false.
  517. /// If the dependence isn't proven to exist,
  518. /// marks the Result as inconsistent.
  519. bool testZIV(const SCEV *Src,
  520. const SCEV *Dst,
  521. FullDependence &Result) const;
  522. /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
  523. /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
  524. /// i and j are induction variables, c1 and c2 are loop invariant,
  525. /// and a1 and a2 are constant.
  526. /// Returns true if any possible dependence is disproved.
  527. /// If there might be a dependence, returns false.
  528. /// Sets appropriate direction vector entry and, when possible,
  529. /// the distance vector entry.
  530. /// If the dependence isn't proven to exist,
  531. /// marks the Result as inconsistent.
  532. bool testSIV(const SCEV *Src,
  533. const SCEV *Dst,
  534. unsigned &Level,
  535. FullDependence &Result,
  536. Constraint &NewConstraint,
  537. const SCEV *&SplitIter) const;
  538. /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
  539. /// Things of the form [c1 + a1*i] and [c2 + a2*j]
  540. /// where i and j are induction variables, c1 and c2 are loop invariant,
  541. /// and a1 and a2 are constant.
  542. /// With minor algebra, this test can also be used for things like
  543. /// [c1 + a1*i + a2*j][c2].
  544. /// Returns true if any possible dependence is disproved.
  545. /// If there might be a dependence, returns false.
  546. /// Marks the Result as inconsistent.
  547. bool testRDIV(const SCEV *Src,
  548. const SCEV *Dst,
  549. FullDependence &Result) const;
  550. /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
  551. /// Returns true if dependence disproved.
  552. /// Can sometimes refine direction vectors.
  553. bool testMIV(const SCEV *Src,
  554. const SCEV *Dst,
  555. const SmallBitVector &Loops,
  556. FullDependence &Result) const;
  557. /// strongSIVtest - Tests the strong SIV subscript pair (Src and Dst)
  558. /// for dependence.
  559. /// Things of the form [c1 + a*i] and [c2 + a*i],
  560. /// where i is an induction variable, c1 and c2 are loop invariant,
  561. /// and a is a constant
  562. /// Returns true if any possible dependence is disproved.
  563. /// If there might be a dependence, returns false.
  564. /// Sets appropriate direction and distance.
  565. bool strongSIVtest(const SCEV *Coeff,
  566. const SCEV *SrcConst,
  567. const SCEV *DstConst,
  568. const Loop *CurrentLoop,
  569. unsigned Level,
  570. FullDependence &Result,
  571. Constraint &NewConstraint) const;
  572. /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
  573. /// (Src and Dst) for dependence.
  574. /// Things of the form [c1 + a*i] and [c2 - a*i],
  575. /// where i is an induction variable, c1 and c2 are loop invariant,
  576. /// and a is a constant.
  577. /// Returns true if any possible dependence is disproved.
  578. /// If there might be a dependence, returns false.
  579. /// Sets appropriate direction entry.
  580. /// Set consistent to false.
  581. /// Marks the dependence as splitable.
  582. bool weakCrossingSIVtest(const SCEV *SrcCoeff,
  583. const SCEV *SrcConst,
  584. const SCEV *DstConst,
  585. const Loop *CurrentLoop,
  586. unsigned Level,
  587. FullDependence &Result,
  588. Constraint &NewConstraint,
  589. const SCEV *&SplitIter) const;
  590. /// ExactSIVtest - Tests the SIV subscript pair
  591. /// (Src and Dst) for dependence.
  592. /// Things of the form [c1 + a1*i] and [c2 + a2*i],
  593. /// where i is an induction variable, c1 and c2 are loop invariant,
  594. /// and a1 and a2 are constant.
  595. /// Returns true if any possible dependence is disproved.
  596. /// If there might be a dependence, returns false.
  597. /// Sets appropriate direction entry.
  598. /// Set consistent to false.
  599. bool exactSIVtest(const SCEV *SrcCoeff,
  600. const SCEV *DstCoeff,
  601. const SCEV *SrcConst,
  602. const SCEV *DstConst,
  603. const Loop *CurrentLoop,
  604. unsigned Level,
  605. FullDependence &Result,
  606. Constraint &NewConstraint) const;
  607. /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
  608. /// (Src and Dst) for dependence.
  609. /// Things of the form [c1] and [c2 + a*i],
  610. /// where i is an induction variable, c1 and c2 are loop invariant,
  611. /// and a is a constant. See also weakZeroDstSIVtest.
  612. /// Returns true if any possible dependence is disproved.
  613. /// If there might be a dependence, returns false.
  614. /// Sets appropriate direction entry.
  615. /// Set consistent to false.
  616. /// If loop peeling will break the dependence, mark appropriately.
  617. bool weakZeroSrcSIVtest(const SCEV *DstCoeff,
  618. const SCEV *SrcConst,
  619. const SCEV *DstConst,
  620. const Loop *CurrentLoop,
  621. unsigned Level,
  622. FullDependence &Result,
  623. Constraint &NewConstraint) const;
  624. /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
  625. /// (Src and Dst) for dependence.
  626. /// Things of the form [c1 + a*i] and [c2],
  627. /// where i is an induction variable, c1 and c2 are loop invariant,
  628. /// and a is a constant. See also weakZeroSrcSIVtest.
  629. /// Returns true if any possible dependence is disproved.
  630. /// If there might be a dependence, returns false.
  631. /// Sets appropriate direction entry.
  632. /// Set consistent to false.
  633. /// If loop peeling will break the dependence, mark appropriately.
  634. bool weakZeroDstSIVtest(const SCEV *SrcCoeff,
  635. const SCEV *SrcConst,
  636. const SCEV *DstConst,
  637. const Loop *CurrentLoop,
  638. unsigned Level,
  639. FullDependence &Result,
  640. Constraint &NewConstraint) const;
  641. /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
  642. /// Things of the form [c1 + a*i] and [c2 + b*j],
  643. /// where i and j are induction variable, c1 and c2 are loop invariant,
  644. /// and a and b are constants.
  645. /// Returns true if any possible dependence is disproved.
  646. /// Marks the result as inconsistent.
  647. /// Works in some cases that symbolicRDIVtest doesn't,
  648. /// and vice versa.
  649. bool exactRDIVtest(const SCEV *SrcCoeff,
  650. const SCEV *DstCoeff,
  651. const SCEV *SrcConst,
  652. const SCEV *DstConst,
  653. const Loop *SrcLoop,
  654. const Loop *DstLoop,
  655. FullDependence &Result) const;
  656. /// symbolicRDIVtest - Tests the RDIV subscript pair for dependence.
  657. /// Things of the form [c1 + a*i] and [c2 + b*j],
  658. /// where i and j are induction variable, c1 and c2 are loop invariant,
  659. /// and a and b are constants.
  660. /// Returns true if any possible dependence is disproved.
  661. /// Marks the result as inconsistent.
  662. /// Works in some cases that exactRDIVtest doesn't,
  663. /// and vice versa. Can also be used as a backup for
  664. /// ordinary SIV tests.
  665. bool symbolicRDIVtest(const SCEV *SrcCoeff,
  666. const SCEV *DstCoeff,
  667. const SCEV *SrcConst,
  668. const SCEV *DstConst,
  669. const Loop *SrcLoop,
  670. const Loop *DstLoop) const;
  671. /// gcdMIVtest - Tests an MIV subscript pair for dependence.
  672. /// Returns true if any possible dependence is disproved.
  673. /// Marks the result as inconsistent.
  674. /// Can sometimes disprove the equal direction for 1 or more loops.
  675. // Can handle some symbolics that even the SIV tests don't get,
  676. /// so we use it as a backup for everything.
  677. bool gcdMIVtest(const SCEV *Src,
  678. const SCEV *Dst,
  679. FullDependence &Result) const;
  680. /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
  681. /// Returns true if any possible dependence is disproved.
  682. /// Marks the result as inconsistent.
  683. /// Computes directions.
  684. bool banerjeeMIVtest(const SCEV *Src,
  685. const SCEV *Dst,
  686. const SmallBitVector &Loops,
  687. FullDependence &Result) const;
  688. /// collectCoefficientInfo - Walks through the subscript,
  689. /// collecting each coefficient, the associated loop bounds,
  690. /// and recording its positive and negative parts for later use.
  691. CoefficientInfo *collectCoeffInfo(const SCEV *Subscript,
  692. bool SrcFlag,
  693. const SCEV *&Constant) const;
  694. /// getPositivePart - X^+ = max(X, 0).
  695. ///
  696. const SCEV *getPositivePart(const SCEV *X) const;
  697. /// getNegativePart - X^- = min(X, 0).
  698. ///
  699. const SCEV *getNegativePart(const SCEV *X) const;
  700. /// getLowerBound - Looks through all the bounds info and
  701. /// computes the lower bound given the current direction settings
  702. /// at each level.
  703. const SCEV *getLowerBound(BoundInfo *Bound) const;
  704. /// getUpperBound - Looks through all the bounds info and
  705. /// computes the upper bound given the current direction settings
  706. /// at each level.
  707. const SCEV *getUpperBound(BoundInfo *Bound) const;
  708. /// exploreDirections - Hierarchically expands the direction vector
  709. /// search space, combining the directions of discovered dependences
  710. /// in the DirSet field of Bound. Returns the number of distinct
  711. /// dependences discovered. If the dependence is disproved,
  712. /// it will return 0.
  713. unsigned exploreDirections(unsigned Level,
  714. CoefficientInfo *A,
  715. CoefficientInfo *B,
  716. BoundInfo *Bound,
  717. const SmallBitVector &Loops,
  718. unsigned &DepthExpanded,
  719. const SCEV *Delta) const;
  720. /// testBounds - Returns true iff the current bounds are plausible.
  721. bool testBounds(unsigned char DirKind,
  722. unsigned Level,
  723. BoundInfo *Bound,
  724. const SCEV *Delta) const;
  725. /// findBoundsALL - Computes the upper and lower bounds for level K
  726. /// using the * direction. Records them in Bound.
  727. void findBoundsALL(CoefficientInfo *A,
  728. CoefficientInfo *B,
  729. BoundInfo *Bound,
  730. unsigned K) const;
  731. /// findBoundsLT - Computes the upper and lower bounds for level K
  732. /// using the < direction. Records them in Bound.
  733. void findBoundsLT(CoefficientInfo *A,
  734. CoefficientInfo *B,
  735. BoundInfo *Bound,
  736. unsigned K) const;
  737. /// findBoundsGT - Computes the upper and lower bounds for level K
  738. /// using the > direction. Records them in Bound.
  739. void findBoundsGT(CoefficientInfo *A,
  740. CoefficientInfo *B,
  741. BoundInfo *Bound,
  742. unsigned K) const;
  743. /// findBoundsEQ - Computes the upper and lower bounds for level K
  744. /// using the = direction. Records them in Bound.
  745. void findBoundsEQ(CoefficientInfo *A,
  746. CoefficientInfo *B,
  747. BoundInfo *Bound,
  748. unsigned K) const;
  749. /// intersectConstraints - Updates X with the intersection
  750. /// of the Constraints X and Y. Returns true if X has changed.
  751. bool intersectConstraints(Constraint *X,
  752. const Constraint *Y);
  753. /// propagate - Review the constraints, looking for opportunities
  754. /// to simplify a subscript pair (Src and Dst).
  755. /// Return true if some simplification occurs.
  756. /// If the simplification isn't exact (that is, if it is conservative
  757. /// in terms of dependence), set consistent to false.
  758. bool propagate(const SCEV *&Src,
  759. const SCEV *&Dst,
  760. SmallBitVector &Loops,
  761. SmallVectorImpl<Constraint> &Constraints,
  762. bool &Consistent);
  763. /// propagateDistance - Attempt to propagate a distance
  764. /// constraint into a subscript pair (Src and Dst).
  765. /// Return true if some simplification occurs.
  766. /// If the simplification isn't exact (that is, if it is conservative
  767. /// in terms of dependence), set consistent to false.
  768. bool propagateDistance(const SCEV *&Src,
  769. const SCEV *&Dst,
  770. Constraint &CurConstraint,
  771. bool &Consistent);
  772. /// propagatePoint - Attempt to propagate a point
  773. /// constraint into a subscript pair (Src and Dst).
  774. /// Return true if some simplification occurs.
  775. bool propagatePoint(const SCEV *&Src,
  776. const SCEV *&Dst,
  777. Constraint &CurConstraint);
  778. /// propagateLine - Attempt to propagate a line
  779. /// constraint into a subscript pair (Src and Dst).
  780. /// Return true if some simplification occurs.
  781. /// If the simplification isn't exact (that is, if it is conservative
  782. /// in terms of dependence), set consistent to false.
  783. bool propagateLine(const SCEV *&Src,
  784. const SCEV *&Dst,
  785. Constraint &CurConstraint,
  786. bool &Consistent);
  787. /// findCoefficient - Given a linear SCEV,
  788. /// return the coefficient corresponding to specified loop.
  789. /// If there isn't one, return the SCEV constant 0.
  790. /// For example, given a*i + b*j + c*k, returning the coefficient
  791. /// corresponding to the j loop would yield b.
  792. const SCEV *findCoefficient(const SCEV *Expr,
  793. const Loop *TargetLoop) const;
  794. /// zeroCoefficient - Given a linear SCEV,
  795. /// return the SCEV given by zeroing out the coefficient
  796. /// corresponding to the specified loop.
  797. /// For example, given a*i + b*j + c*k, zeroing the coefficient
  798. /// corresponding to the j loop would yield a*i + c*k.
  799. const SCEV *zeroCoefficient(const SCEV *Expr,
  800. const Loop *TargetLoop) const;
  801. /// addToCoefficient - Given a linear SCEV Expr,
  802. /// return the SCEV given by adding some Value to the
  803. /// coefficient corresponding to the specified TargetLoop.
  804. /// For example, given a*i + b*j + c*k, adding 1 to the coefficient
  805. /// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
  806. const SCEV *addToCoefficient(const SCEV *Expr,
  807. const Loop *TargetLoop,
  808. const SCEV *Value) const;
  809. /// updateDirection - Update direction vector entry
  810. /// based on the current constraint.
  811. void updateDirection(Dependence::DVEntry &Level,
  812. const Constraint &CurConstraint) const;
  813. /// Given a linear access function, tries to recover subscripts
  814. /// for each dimension of the array element access.
  815. bool tryDelinearize(Instruction *Src, Instruction *Dst,
  816. SmallVectorImpl<Subscript> &Pair);
  817. /// Tries to delinearize access function for a fixed size multi-dimensional
  818. /// array, by deriving subscripts from GEP instructions. Returns true upon
  819. /// success and false otherwise.
  820. bool tryDelinearizeFixedSize(Instruction *Src, Instruction *Dst,
  821. const SCEV *SrcAccessFn,
  822. const SCEV *DstAccessFn,
  823. SmallVectorImpl<const SCEV *> &SrcSubscripts,
  824. SmallVectorImpl<const SCEV *> &DstSubscripts);
  825. /// Tries to delinearize access function for a multi-dimensional array with
  826. /// symbolic runtime sizes.
  827. /// Returns true upon success and false otherwise.
  828. bool tryDelinearizeParametricSize(
  829. Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
  830. const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
  831. SmallVectorImpl<const SCEV *> &DstSubscripts);
  832. /// checkSubscript - Helper function for checkSrcSubscript and
  833. /// checkDstSubscript to avoid duplicate code
  834. bool checkSubscript(const SCEV *Expr, const Loop *LoopNest,
  835. SmallBitVector &Loops, bool IsSrc);
  836. }; // class DependenceInfo
  837. /// AnalysisPass to compute dependence information in a function
  838. class DependenceAnalysis : public AnalysisInfoMixin<DependenceAnalysis> {
  839. public:
  840. typedef DependenceInfo Result;
  841. Result run(Function &F, FunctionAnalysisManager &FAM);
  842. private:
  843. static AnalysisKey Key;
  844. friend struct AnalysisInfoMixin<DependenceAnalysis>;
  845. }; // class DependenceAnalysis
  846. /// Printer pass to dump DA results.
  847. struct DependenceAnalysisPrinterPass
  848. : public PassInfoMixin<DependenceAnalysisPrinterPass> {
  849. DependenceAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
  850. PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
  851. private:
  852. raw_ostream &OS;
  853. }; // class DependenceAnalysisPrinterPass
  854. /// Legacy pass manager pass to access dependence information
  855. class DependenceAnalysisWrapperPass : public FunctionPass {
  856. public:
  857. static char ID; // Class identification, replacement for typeinfo
  858. DependenceAnalysisWrapperPass();
  859. bool runOnFunction(Function &F) override;
  860. void releaseMemory() override;
  861. void getAnalysisUsage(AnalysisUsage &) const override;
  862. void print(raw_ostream &, const Module * = nullptr) const override;
  863. DependenceInfo &getDI() const;
  864. private:
  865. std::unique_ptr<DependenceInfo> info;
  866. }; // class DependenceAnalysisWrapperPass
  867. /// createDependenceAnalysisPass - This creates an instance of the
  868. /// DependenceAnalysis wrapper pass.
  869. FunctionPass *createDependenceAnalysisWrapperPass();
  870. } // namespace llvm
  871. #endif
  872. #ifdef __GNUC__
  873. #pragma GCC diagnostic pop
  874. #endif