Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

895 lines
37 KiB

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