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