DependenceAnalysis.h 39 KB

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