LLVM API Documentation

DependenceAnalysis.h
Go to the documentation of this file.
00001 //===-- llvm/Analysis/DependenceAnalysis.h -------------------- -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // DependenceAnalysis is an LLVM pass that analyses dependences between memory
00011 // accesses. Currently, it is an implementation of the approach described in
00012 //
00013 //            Practical Dependence Testing
00014 //            Goff, Kennedy, Tseng
00015 //            PLDI 1991
00016 //
00017 // There's a single entry point that analyzes the dependence between a pair
00018 // of memory references in a function, returning either NULL, for no dependence,
00019 // or a more-or-less detailed description of the dependence between them.
00020 //
00021 // This pass exists to support the DependenceGraph pass. There are two separate
00022 // passes because there's a useful separation of concerns. A dependence exists
00023 // if two conditions are met:
00024 //
00025 //    1) Two instructions reference the same memory location, and
00026 //    2) There is a flow of control leading from one instruction to the other.
00027 //
00028 // DependenceAnalysis attacks the first condition; DependenceGraph will attack
00029 // the second (it's not yet ready).
00030 //
00031 // Please note that this is work in progress and the interface is subject to
00032 // change.
00033 //
00034 // Plausible changes:
00035 //    Return a set of more precise dependences instead of just one dependence
00036 //    summarizing all.
00037 //
00038 //===----------------------------------------------------------------------===//
00039 
00040 #ifndef LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
00041 #define LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
00042 
00043 #include "llvm/ADT/SmallBitVector.h"
00044 #include "llvm/IR/Instructions.h"
00045 #include "llvm/Pass.h"
00046 
00047 namespace llvm {
00048   class AliasAnalysis;
00049   class Loop;
00050   class LoopInfo;
00051   class ScalarEvolution;
00052   class SCEV;
00053   class SCEVConstant;
00054   class raw_ostream;
00055 
00056   /// Dependence - This class represents a dependence between two memory
00057   /// memory references in a function. It contains minimal information and
00058   /// is used in the very common situation where the compiler is unable to
00059   /// determine anything beyond the existence of a dependence; that is, it
00060   /// represents a confused dependence (see also FullDependence). In most
00061   /// cases (for output, flow, and anti dependences), the dependence implies
00062   /// an ordering, where the source must precede the destination; in contrast,
00063   /// input dependences are unordered.
00064   ///
00065   /// When a dependence graph is built, each Dependence will be a member of
00066   /// the set of predecessor edges for its destination instruction and a set
00067   /// if successor edges for its source instruction. These sets are represented
00068   /// as singly-linked lists, with the "next" fields stored in the dependence
00069   /// itelf.
00070   class Dependence {
00071   public:
00072     Dependence(Instruction *Source,
00073                Instruction *Destination) :
00074       Src(Source),
00075       Dst(Destination),
00076       NextPredecessor(nullptr),
00077       NextSuccessor(nullptr) {}
00078     virtual ~Dependence() {}
00079 
00080     /// Dependence::DVEntry - Each level in the distance/direction vector
00081     /// has a direction (or perhaps a union of several directions), and
00082     /// perhaps a distance.
00083     struct DVEntry {
00084       enum { NONE = 0,
00085              LT = 1,
00086              EQ = 2,
00087              LE = 3,
00088              GT = 4,
00089              NE = 5,
00090              GE = 6,
00091              ALL = 7 };
00092       unsigned char Direction : 3; // Init to ALL, then refine.
00093       bool Scalar    : 1; // Init to true.
00094       bool PeelFirst : 1; // Peeling the first iteration will break dependence.
00095       bool PeelLast  : 1; // Peeling the last iteration will break the dependence.
00096       bool Splitable : 1; // Splitting the loop will break dependence.
00097       const SCEV *Distance; // NULL implies no distance available.
00098       DVEntry() : Direction(ALL), Scalar(true), PeelFirst(false),
00099                   PeelLast(false), Splitable(false), Distance(nullptr) { }
00100     };
00101 
00102     /// getSrc - Returns the source instruction for this dependence.
00103     ///
00104     Instruction *getSrc() const { return Src; }
00105 
00106     /// getDst - Returns the destination instruction for this dependence.
00107     ///
00108     Instruction *getDst() const { return Dst; }
00109 
00110     /// isInput - Returns true if this is an input dependence.
00111     ///
00112     bool isInput() const;
00113 
00114     /// isOutput - Returns true if this is an output dependence.
00115     ///
00116     bool isOutput() const;
00117 
00118     /// isFlow - Returns true if this is a flow (aka true) dependence.
00119     ///
00120     bool isFlow() const;
00121 
00122     /// isAnti - Returns true if this is an anti dependence.
00123     ///
00124     bool isAnti() const;
00125 
00126     /// isOrdered - Returns true if dependence is Output, Flow, or Anti
00127     ///
00128     bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
00129 
00130     /// isUnordered - Returns true if dependence is Input
00131     ///
00132     bool isUnordered() const { return isInput(); }
00133 
00134     /// isLoopIndependent - Returns true if this is a loop-independent
00135     /// dependence.
00136     virtual bool isLoopIndependent() const { return true; }
00137 
00138     /// isConfused - Returns true if this dependence is confused
00139     /// (the compiler understands nothing and makes worst-case
00140     /// assumptions).
00141     virtual bool isConfused() const { return true; }
00142 
00143     /// isConsistent - Returns true if this dependence is consistent
00144     /// (occurs every time the source and destination are executed).
00145     virtual bool isConsistent() const { return false; }
00146 
00147     /// getLevels - Returns the number of common loops surrounding the
00148     /// source and destination of the dependence.
00149     virtual unsigned getLevels() const { return 0; }
00150 
00151     /// getDirection - Returns the direction associated with a particular
00152     /// level.
00153     virtual unsigned getDirection(unsigned Level) const { return DVEntry::ALL; }
00154 
00155     /// getDistance - Returns the distance (or NULL) associated with a
00156     /// particular level.
00157     virtual const SCEV *getDistance(unsigned Level) const { return nullptr; }
00158 
00159     /// isPeelFirst - Returns true if peeling the first iteration from
00160     /// this loop will break this dependence.
00161     virtual bool isPeelFirst(unsigned Level) const { return false; }
00162 
00163     /// isPeelLast - Returns true if peeling the last iteration from
00164     /// this loop will break this dependence.
00165     virtual bool isPeelLast(unsigned Level) const { return false; }
00166 
00167     /// isSplitable - Returns true if splitting this loop will break
00168     /// the dependence.
00169     virtual bool isSplitable(unsigned Level) const { return false; }
00170 
00171     /// isScalar - Returns true if a particular level is scalar; that is,
00172     /// if no subscript in the source or destination mention the induction
00173     /// variable associated with the loop at this level.
00174     virtual bool isScalar(unsigned Level) const;
00175 
00176     /// getNextPredecessor - Returns the value of the NextPredecessor
00177     /// field.
00178     const Dependence *getNextPredecessor() const {
00179       return NextPredecessor;
00180     }
00181     
00182     /// getNextSuccessor - Returns the value of the NextSuccessor
00183     /// field.
00184     const Dependence *getNextSuccessor() const {
00185       return NextSuccessor;
00186     }
00187     
00188     /// setNextPredecessor - Sets the value of the NextPredecessor
00189     /// field.
00190     void setNextPredecessor(const Dependence *pred) {
00191       NextPredecessor = pred;
00192     }
00193     
00194     /// setNextSuccessor - Sets the value of the NextSuccessor
00195     /// field.
00196     void setNextSuccessor(const Dependence *succ) {
00197       NextSuccessor = succ;
00198     }
00199     
00200     /// dump - For debugging purposes, dumps a dependence to OS.
00201     ///
00202     void dump(raw_ostream &OS) const;
00203   private:
00204     Instruction *Src, *Dst;
00205     const Dependence *NextPredecessor, *NextSuccessor;
00206     friend class DependenceAnalysis;
00207   };
00208 
00209 
00210   /// FullDependence - This class represents a dependence between two memory
00211   /// references in a function. It contains detailed information about the
00212   /// dependence (direction vectors, etc.) and is used when the compiler is
00213   /// able to accurately analyze the interaction of the references; that is,
00214   /// it is not a confused dependence (see Dependence). In most cases
00215   /// (for output, flow, and anti dependences), the dependence implies an
00216   /// ordering, where the source must precede the destination; in contrast,
00217   /// input dependences are unordered.
00218   class FullDependence : public Dependence {
00219   public:
00220     FullDependence(Instruction *Src,
00221                    Instruction *Dst,
00222                    bool LoopIndependent,
00223                    unsigned Levels);
00224     ~FullDependence() {
00225       delete[] DV;
00226     }
00227 
00228     /// isLoopIndependent - Returns true if this is a loop-independent
00229     /// dependence.
00230     bool isLoopIndependent() const override { return LoopIndependent; }
00231 
00232     /// isConfused - Returns true if this dependence is confused
00233     /// (the compiler understands nothing and makes worst-case
00234     /// assumptions).
00235     bool isConfused() const override { return false; }
00236 
00237     /// isConsistent - Returns true if this dependence is consistent
00238     /// (occurs every time the source and destination are executed).
00239     bool isConsistent() const override { return Consistent; }
00240 
00241     /// getLevels - Returns the number of common loops surrounding the
00242     /// source and destination of the dependence.
00243     unsigned getLevels() const override { return Levels; }
00244 
00245     /// getDirection - Returns the direction associated with a particular
00246     /// level.
00247     unsigned getDirection(unsigned Level) const override;
00248 
00249     /// getDistance - Returns the distance (or NULL) associated with a
00250     /// particular level.
00251     const SCEV *getDistance(unsigned Level) const override;
00252 
00253     /// isPeelFirst - Returns true if peeling the first iteration from
00254     /// this loop will break this dependence.
00255     bool isPeelFirst(unsigned Level) const override;
00256 
00257     /// isPeelLast - Returns true if peeling the last iteration from
00258     /// this loop will break this dependence.
00259     bool isPeelLast(unsigned Level) const override;
00260 
00261     /// isSplitable - Returns true if splitting the loop will break
00262     /// the dependence.
00263     bool isSplitable(unsigned Level) const override;
00264 
00265     /// isScalar - Returns true if a particular level is scalar; that is,
00266     /// if no subscript in the source or destination mention the induction
00267     /// variable associated with the loop at this level.
00268     bool isScalar(unsigned Level) const override;
00269   private:
00270     unsigned short Levels;
00271     bool LoopIndependent;
00272     bool Consistent; // Init to true, then refine.
00273     DVEntry *DV;
00274     friend class DependenceAnalysis;
00275   };
00276 
00277 
00278   /// DependenceAnalysis - This class is the main dependence-analysis driver.
00279   ///
00280   class DependenceAnalysis : public FunctionPass {
00281     void operator=(const DependenceAnalysis &) LLVM_DELETED_FUNCTION;
00282     DependenceAnalysis(const DependenceAnalysis &) LLVM_DELETED_FUNCTION;
00283   public:
00284     /// depends - Tests for a dependence between the Src and Dst instructions.
00285     /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
00286     /// FullDependence) with as much information as can be gleaned.
00287     /// The flag PossiblyLoopIndependent should be set by the caller
00288     /// if it appears that control flow can reach from Src to Dst
00289     /// without traversing a loop back edge.
00290     std::unique_ptr<Dependence> depends(Instruction *Src,
00291                                         Instruction *Dst,
00292                                         bool PossiblyLoopIndependent);
00293 
00294     /// getSplitIteration - Give a dependence that's splittable at some
00295     /// particular level, return the iteration that should be used to split
00296     /// the loop.
00297     ///
00298     /// Generally, the dependence analyzer will be used to build
00299     /// a dependence graph for a function (basically a map from instructions
00300     /// to dependences). Looking for cycles in the graph shows us loops
00301     /// that cannot be trivially vectorized/parallelized.
00302     ///
00303     /// We can try to improve the situation by examining all the dependences
00304     /// that make up the cycle, looking for ones we can break.
00305     /// Sometimes, peeling the first or last iteration of a loop will break
00306     /// dependences, and there are flags for those possibilities.
00307     /// Sometimes, splitting a loop at some other iteration will do the trick,
00308     /// and we've got a flag for that case. Rather than waste the space to
00309     /// record the exact iteration (since we rarely know), we provide
00310     /// a method that calculates the iteration. It's a drag that it must work
00311     /// from scratch, but wonderful in that it's possible.
00312     ///
00313     /// Here's an example:
00314     ///
00315     ///    for (i = 0; i < 10; i++)
00316     ///        A[i] = ...
00317     ///        ... = A[11 - i]
00318     ///
00319     /// There's a loop-carried flow dependence from the store to the load,
00320     /// found by the weak-crossing SIV test. The dependence will have a flag,
00321     /// indicating that the dependence can be broken by splitting the loop.
00322     /// Calling getSplitIteration will return 5.
00323     /// Splitting the loop breaks the dependence, like so:
00324     ///
00325     ///    for (i = 0; i <= 5; i++)
00326     ///        A[i] = ...
00327     ///        ... = A[11 - i]
00328     ///    for (i = 6; i < 10; i++)
00329     ///        A[i] = ...
00330     ///        ... = A[11 - i]
00331     ///
00332     /// breaks the dependence and allows us to vectorize/parallelize
00333     /// both loops.
00334     const SCEV *getSplitIteration(const Dependence &Dep, unsigned Level);
00335 
00336   private:
00337     AliasAnalysis *AA;
00338     ScalarEvolution *SE;
00339     LoopInfo *LI;
00340     Function *F;
00341 
00342     /// Subscript - This private struct represents a pair of subscripts from
00343     /// a pair of potentially multi-dimensional array references. We use a
00344     /// vector of them to guide subscript partitioning.
00345     struct Subscript {
00346       const SCEV *Src;
00347       const SCEV *Dst;
00348       enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
00349       SmallBitVector Loops;
00350       SmallBitVector GroupLoops;
00351       SmallBitVector Group;
00352     };
00353 
00354     struct CoefficientInfo {
00355       const SCEV *Coeff;
00356       const SCEV *PosPart;
00357       const SCEV *NegPart;
00358       const SCEV *Iterations;
00359     };
00360 
00361     struct BoundInfo {
00362       const SCEV *Iterations;
00363       const SCEV *Upper[8];
00364       const SCEV *Lower[8];
00365       unsigned char Direction;
00366       unsigned char DirSet;
00367     };
00368 
00369     /// Constraint - This private class represents a constraint, as defined
00370     /// in the paper
00371     ///
00372     ///           Practical Dependence Testing
00373     ///           Goff, Kennedy, Tseng
00374     ///           PLDI 1991
00375     ///
00376     /// There are 5 kinds of constraint, in a hierarchy.
00377     ///   1) Any - indicates no constraint, any dependence is possible.
00378     ///   2) Line - A line ax + by = c, where a, b, and c are parameters,
00379     ///             representing the dependence equation.
00380     ///   3) Distance - The value d of the dependence distance;
00381     ///   4) Point - A point <x, y> representing the dependence from
00382     ///              iteration x to iteration y.
00383     ///   5) Empty - No dependence is possible.
00384     class Constraint {
00385     private:
00386       enum ConstraintKind { Empty, Point, Distance, Line, Any } Kind;
00387       ScalarEvolution *SE;
00388       const SCEV *A;
00389       const SCEV *B;
00390       const SCEV *C;
00391       const Loop *AssociatedLoop;
00392     public:
00393       /// isEmpty - Return true if the constraint is of kind Empty.
00394       bool isEmpty() const { return Kind == Empty; }
00395 
00396       /// isPoint - Return true if the constraint is of kind Point.
00397       bool isPoint() const { return Kind == Point; }
00398 
00399       /// isDistance - Return true if the constraint is of kind Distance.
00400       bool isDistance() const { return Kind == Distance; }
00401 
00402       /// isLine - Return true if the constraint is of kind Line.
00403       /// Since Distance's can also be represented as Lines, we also return
00404       /// true if the constraint is of kind Distance.
00405       bool isLine() const { return Kind == Line || Kind == Distance; }
00406 
00407       /// isAny - Return true if the constraint is of kind Any;
00408       bool isAny() const { return Kind == Any; }
00409 
00410       /// getX - If constraint is a point <X, Y>, returns X.
00411       /// Otherwise assert.
00412       const SCEV *getX() const;
00413 
00414       /// getY - If constraint is a point <X, Y>, returns Y.
00415       /// Otherwise assert.
00416       const SCEV *getY() const;
00417 
00418       /// getA - If constraint is a line AX + BY = C, returns A.
00419       /// Otherwise assert.
00420       const SCEV *getA() const;
00421 
00422       /// getB - If constraint is a line AX + BY = C, returns B.
00423       /// Otherwise assert.
00424       const SCEV *getB() const;
00425 
00426       /// getC - If constraint is a line AX + BY = C, returns C.
00427       /// Otherwise assert.
00428       const SCEV *getC() const;
00429 
00430       /// getD - If constraint is a distance, returns D.
00431       /// Otherwise assert.
00432       const SCEV *getD() const;
00433 
00434       /// getAssociatedLoop - Returns the loop associated with this constraint.
00435       const Loop *getAssociatedLoop() const;
00436 
00437       /// setPoint - Change a constraint to Point.
00438       void setPoint(const SCEV *X, const SCEV *Y, const Loop *CurrentLoop);
00439 
00440       /// setLine - Change a constraint to Line.
00441       void setLine(const SCEV *A, const SCEV *B,
00442                    const SCEV *C, const Loop *CurrentLoop);
00443 
00444       /// setDistance - Change a constraint to Distance.
00445       void setDistance(const SCEV *D, const Loop *CurrentLoop);
00446 
00447       /// setEmpty - Change a constraint to Empty.
00448       void setEmpty();
00449 
00450       /// setAny - Change a constraint to Any.
00451       void setAny(ScalarEvolution *SE);
00452 
00453       /// dump - For debugging purposes. Dumps the constraint
00454       /// out to OS.
00455       void dump(raw_ostream &OS) const;
00456     };
00457 
00458 
00459     /// establishNestingLevels - Examines the loop nesting of the Src and Dst
00460     /// instructions and establishes their shared loops. Sets the variables
00461     /// CommonLevels, SrcLevels, and MaxLevels.
00462     /// The source and destination instructions needn't be contained in the same
00463     /// loop. The routine establishNestingLevels finds the level of most deeply
00464     /// nested loop that contains them both, CommonLevels. An instruction that's
00465     /// not contained in a loop is at level = 0. MaxLevels is equal to the level
00466     /// of the source plus the level of the destination, minus CommonLevels.
00467     /// This lets us allocate vectors MaxLevels in length, with room for every
00468     /// distinct loop referenced in both the source and destination subscripts.
00469     /// The variable SrcLevels is the nesting depth of the source instruction.
00470     /// It's used to help calculate distinct loops referenced by the destination.
00471     /// Here's the map from loops to levels:
00472     ///            0 - unused
00473     ///            1 - outermost common loop
00474     ///          ... - other common loops
00475     /// CommonLevels - innermost common loop
00476     ///          ... - loops containing Src but not Dst
00477     ///    SrcLevels - innermost loop containing Src but not Dst
00478     ///          ... - loops containing Dst but not Src
00479     ///    MaxLevels - innermost loop containing Dst but not Src
00480     /// Consider the follow code fragment:
00481     ///    for (a = ...) {
00482     ///      for (b = ...) {
00483     ///        for (c = ...) {
00484     ///          for (d = ...) {
00485     ///            A[] = ...;
00486     ///          }
00487     ///        }
00488     ///        for (e = ...) {
00489     ///          for (f = ...) {
00490     ///            for (g = ...) {
00491     ///              ... = A[];
00492     ///            }
00493     ///          }
00494     ///        }
00495     ///      }
00496     ///    }
00497     /// If we're looking at the possibility of a dependence between the store
00498     /// to A (the Src) and the load from A (the Dst), we'll note that they
00499     /// have 2 loops in common, so CommonLevels will equal 2 and the direction
00500     /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
00501     /// A map from loop names to level indices would look like
00502     ///     a - 1
00503     ///     b - 2 = CommonLevels
00504     ///     c - 3
00505     ///     d - 4 = SrcLevels
00506     ///     e - 5
00507     ///     f - 6
00508     ///     g - 7 = MaxLevels
00509     void establishNestingLevels(const Instruction *Src,
00510                                 const Instruction *Dst);
00511 
00512     unsigned CommonLevels, SrcLevels, MaxLevels;
00513 
00514     /// mapSrcLoop - Given one of the loops containing the source, return
00515     /// its level index in our numbering scheme.
00516     unsigned mapSrcLoop(const Loop *SrcLoop) const;
00517 
00518     /// mapDstLoop - Given one of the loops containing the destination,
00519     /// return its level index in our numbering scheme.
00520     unsigned mapDstLoop(const Loop *DstLoop) const;
00521 
00522     /// isLoopInvariant - Returns true if Expression is loop invariant
00523     /// in LoopNest.
00524     bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
00525 
00526     /// removeMatchingExtensions - Examines a subscript pair.
00527     /// If the source and destination are identically sign (or zero)
00528     /// extended, it strips off the extension in an effort to
00529     /// simplify the actual analysis.
00530     void removeMatchingExtensions(Subscript *Pair);
00531 
00532     /// collectCommonLoops - Finds the set of loops from the LoopNest that
00533     /// have a level <= CommonLevels and are referred to by the SCEV Expression.
00534     void collectCommonLoops(const SCEV *Expression,
00535                             const Loop *LoopNest,
00536                             SmallBitVector &Loops) const;
00537 
00538     /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
00539     /// linear. Collect the set of loops mentioned by Src.
00540     bool checkSrcSubscript(const SCEV *Src,
00541                            const Loop *LoopNest,
00542                            SmallBitVector &Loops);
00543 
00544     /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
00545     /// linear. Collect the set of loops mentioned by Dst.
00546     bool checkDstSubscript(const SCEV *Dst,
00547                            const Loop *LoopNest,
00548                            SmallBitVector &Loops);
00549 
00550     /// isKnownPredicate - Compare X and Y using the predicate Pred.
00551     /// Basically a wrapper for SCEV::isKnownPredicate,
00552     /// but tries harder, especially in the presence of sign and zero
00553     /// extensions and symbolics.
00554     bool isKnownPredicate(ICmpInst::Predicate Pred,
00555                           const SCEV *X,
00556                           const SCEV *Y) const;
00557 
00558     /// collectUpperBound - All subscripts are the same type (on my machine,
00559     /// an i64). The loop bound may be a smaller type. collectUpperBound
00560     /// find the bound, if available, and zero extends it to the Type T.
00561     /// (I zero extend since the bound should always be >= 0.)
00562     /// If no upper bound is available, return NULL.
00563     const SCEV *collectUpperBound(const Loop *l, Type *T) const;
00564 
00565     /// collectConstantUpperBound - Calls collectUpperBound(), then
00566     /// attempts to cast it to SCEVConstant. If the cast fails,
00567     /// returns NULL.
00568     const SCEVConstant *collectConstantUpperBound(const Loop *l, Type *T) const;
00569 
00570     /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
00571     /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
00572     /// Collects the associated loops in a set.
00573     Subscript::ClassificationKind classifyPair(const SCEV *Src,
00574                                            const Loop *SrcLoopNest,
00575                                            const SCEV *Dst,
00576                                            const Loop *DstLoopNest,
00577                                            SmallBitVector &Loops);
00578 
00579     /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
00580     /// Returns true if any possible dependence is disproved.
00581     /// If there might be a dependence, returns false.
00582     /// If the dependence isn't proven to exist,
00583     /// marks the Result as inconsistent.
00584     bool testZIV(const SCEV *Src,
00585                  const SCEV *Dst,
00586                  FullDependence &Result) const;
00587 
00588     /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
00589     /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
00590     /// i and j are induction variables, c1 and c2 are loop invariant,
00591     /// and a1 and a2 are constant.
00592     /// Returns true if any possible dependence is disproved.
00593     /// If there might be a dependence, returns false.
00594     /// Sets appropriate direction vector entry and, when possible,
00595     /// the distance vector entry.
00596     /// If the dependence isn't proven to exist,
00597     /// marks the Result as inconsistent.
00598     bool testSIV(const SCEV *Src,
00599                  const SCEV *Dst,
00600                  unsigned &Level,
00601                  FullDependence &Result,
00602                  Constraint &NewConstraint,
00603                  const SCEV *&SplitIter) const;
00604 
00605     /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
00606     /// Things of the form [c1 + a1*i] and [c2 + a2*j]
00607     /// where i and j are induction variables, c1 and c2 are loop invariant,
00608     /// and a1 and a2 are constant.
00609     /// With minor algebra, this test can also be used for things like
00610     /// [c1 + a1*i + a2*j][c2].
00611     /// Returns true if any possible dependence is disproved.
00612     /// If there might be a dependence, returns false.
00613     /// Marks the Result as inconsistent.
00614     bool testRDIV(const SCEV *Src,
00615                   const SCEV *Dst,
00616                   FullDependence &Result) const;
00617 
00618     /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
00619     /// Returns true if dependence disproved.
00620     /// Can sometimes refine direction vectors.
00621     bool testMIV(const SCEV *Src,
00622                  const SCEV *Dst,
00623                  const SmallBitVector &Loops,
00624                  FullDependence &Result) const;
00625 
00626     /// strongSIVtest - Tests the strong SIV subscript pair (Src and Dst)
00627     /// for dependence.
00628     /// Things of the form [c1 + a*i] and [c2 + a*i],
00629     /// where i is an induction variable, c1 and c2 are loop invariant,
00630     /// and a is a constant
00631     /// Returns true if any possible dependence is disproved.
00632     /// If there might be a dependence, returns false.
00633     /// Sets appropriate direction and distance.
00634     bool strongSIVtest(const SCEV *Coeff,
00635                        const SCEV *SrcConst,
00636                        const SCEV *DstConst,
00637                        const Loop *CurrentLoop,
00638                        unsigned Level,
00639                        FullDependence &Result,
00640                        Constraint &NewConstraint) const;
00641 
00642     /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
00643     /// (Src and Dst) for dependence.
00644     /// Things of the form [c1 + a*i] and [c2 - a*i],
00645     /// where i is an induction variable, c1 and c2 are loop invariant,
00646     /// and a is a constant.
00647     /// Returns true if any possible dependence is disproved.
00648     /// If there might be a dependence, returns false.
00649     /// Sets appropriate direction entry.
00650     /// Set consistent to false.
00651     /// Marks the dependence as splitable.
00652     bool weakCrossingSIVtest(const SCEV *SrcCoeff,
00653                              const SCEV *SrcConst,
00654                              const SCEV *DstConst,
00655                              const Loop *CurrentLoop,
00656                              unsigned Level,
00657                              FullDependence &Result,
00658                              Constraint &NewConstraint,
00659                              const SCEV *&SplitIter) const;
00660 
00661     /// ExactSIVtest - Tests the SIV subscript pair
00662     /// (Src and Dst) for dependence.
00663     /// Things of the form [c1 + a1*i] and [c2 + a2*i],
00664     /// where i is an induction variable, c1 and c2 are loop invariant,
00665     /// and a1 and a2 are constant.
00666     /// Returns true if any possible dependence is disproved.
00667     /// If there might be a dependence, returns false.
00668     /// Sets appropriate direction entry.
00669     /// Set consistent to false.
00670     bool exactSIVtest(const SCEV *SrcCoeff,
00671                       const SCEV *DstCoeff,
00672                       const SCEV *SrcConst,
00673                       const SCEV *DstConst,
00674                       const Loop *CurrentLoop,
00675                       unsigned Level,
00676                       FullDependence &Result,
00677                       Constraint &NewConstraint) const;
00678 
00679     /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
00680     /// (Src and Dst) for dependence.
00681     /// Things of the form [c1] and [c2 + a*i],
00682     /// where i is an induction variable, c1 and c2 are loop invariant,
00683     /// and a is a constant. See also weakZeroDstSIVtest.
00684     /// Returns true if any possible dependence is disproved.
00685     /// If there might be a dependence, returns false.
00686     /// Sets appropriate direction entry.
00687     /// Set consistent to false.
00688     /// If loop peeling will break the dependence, mark appropriately.
00689     bool weakZeroSrcSIVtest(const SCEV *DstCoeff,
00690                             const SCEV *SrcConst,
00691                             const SCEV *DstConst,
00692                             const Loop *CurrentLoop,
00693                             unsigned Level,
00694                             FullDependence &Result,
00695                             Constraint &NewConstraint) const;
00696 
00697     /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
00698     /// (Src and Dst) for dependence.
00699     /// Things of the form [c1 + a*i] and [c2],
00700     /// where i is an induction variable, c1 and c2 are loop invariant,
00701     /// and a is a constant. See also weakZeroSrcSIVtest.
00702     /// Returns true if any possible dependence is disproved.
00703     /// If there might be a dependence, returns false.
00704     /// Sets appropriate direction entry.
00705     /// Set consistent to false.
00706     /// If loop peeling will break the dependence, mark appropriately.
00707     bool weakZeroDstSIVtest(const SCEV *SrcCoeff,
00708                             const SCEV *SrcConst,
00709                             const SCEV *DstConst,
00710                             const Loop *CurrentLoop,
00711                             unsigned Level,
00712                             FullDependence &Result,
00713                             Constraint &NewConstraint) const;
00714 
00715     /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
00716     /// Things of the form [c1 + a*i] and [c2 + b*j],
00717     /// where i and j are induction variable, c1 and c2 are loop invariant,
00718     /// and a and b are constants.
00719     /// Returns true if any possible dependence is disproved.
00720     /// Marks the result as inconsistent.
00721     /// Works in some cases that symbolicRDIVtest doesn't,
00722     /// and vice versa.
00723     bool exactRDIVtest(const SCEV *SrcCoeff,
00724                        const SCEV *DstCoeff,
00725                        const SCEV *SrcConst,
00726                        const SCEV *DstConst,
00727                        const Loop *SrcLoop,
00728                        const Loop *DstLoop,
00729                        FullDependence &Result) const;
00730 
00731     /// symbolicRDIVtest - Tests the RDIV subscript pair for dependence.
00732     /// Things of the form [c1 + a*i] and [c2 + b*j],
00733     /// where i and j are induction variable, c1 and c2 are loop invariant,
00734     /// and a and b are constants.
00735     /// Returns true if any possible dependence is disproved.
00736     /// Marks the result as inconsistent.
00737     /// Works in some cases that exactRDIVtest doesn't,
00738     /// and vice versa. Can also be used as a backup for
00739     /// ordinary SIV tests.
00740     bool symbolicRDIVtest(const SCEV *SrcCoeff,
00741                           const SCEV *DstCoeff,
00742                           const SCEV *SrcConst,
00743                           const SCEV *DstConst,
00744                           const Loop *SrcLoop,
00745                           const Loop *DstLoop) const;
00746 
00747     /// gcdMIVtest - Tests an MIV subscript pair for dependence.
00748     /// Returns true if any possible dependence is disproved.
00749     /// Marks the result as inconsistent.
00750     /// Can sometimes disprove the equal direction for 1 or more loops.
00751     //  Can handle some symbolics that even the SIV tests don't get,
00752     /// so we use it as a backup for everything.
00753     bool gcdMIVtest(const SCEV *Src,
00754                     const SCEV *Dst,
00755                     FullDependence &Result) const;
00756 
00757     /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
00758     /// Returns true if any possible dependence is disproved.
00759     /// Marks the result as inconsistent.
00760     /// Computes directions.
00761     bool banerjeeMIVtest(const SCEV *Src,
00762                          const SCEV *Dst,
00763                          const SmallBitVector &Loops,
00764                          FullDependence &Result) const;
00765 
00766     /// collectCoefficientInfo - Walks through the subscript,
00767     /// collecting each coefficient, the associated loop bounds,
00768     /// and recording its positive and negative parts for later use.
00769     CoefficientInfo *collectCoeffInfo(const SCEV *Subscript,
00770                                       bool SrcFlag,
00771                                       const SCEV *&Constant) const;
00772 
00773     /// getPositivePart - X^+ = max(X, 0).
00774     ///
00775     const SCEV *getPositivePart(const SCEV *X) const;
00776 
00777     /// getNegativePart - X^- = min(X, 0).
00778     ///
00779     const SCEV *getNegativePart(const SCEV *X) const;
00780 
00781     /// getLowerBound - Looks through all the bounds info and
00782     /// computes the lower bound given the current direction settings
00783     /// at each level.
00784     const SCEV *getLowerBound(BoundInfo *Bound) const;
00785 
00786     /// getUpperBound - Looks through all the bounds info and
00787     /// computes the upper bound given the current direction settings
00788     /// at each level.
00789     const SCEV *getUpperBound(BoundInfo *Bound) const;
00790 
00791     /// exploreDirections - Hierarchically expands the direction vector
00792     /// search space, combining the directions of discovered dependences
00793     /// in the DirSet field of Bound. Returns the number of distinct
00794     /// dependences discovered. If the dependence is disproved,
00795     /// it will return 0.
00796     unsigned exploreDirections(unsigned Level,
00797                                CoefficientInfo *A,
00798                                CoefficientInfo *B,
00799                                BoundInfo *Bound,
00800                                const SmallBitVector &Loops,
00801                                unsigned &DepthExpanded,
00802                                const SCEV *Delta) const;
00803 
00804     /// testBounds - Returns true iff the current bounds are plausible.
00805     ///
00806     bool testBounds(unsigned char DirKind,
00807                     unsigned Level,
00808                     BoundInfo *Bound,
00809                     const SCEV *Delta) const;
00810 
00811     /// findBoundsALL - Computes the upper and lower bounds for level K
00812     /// using the * direction. Records them in Bound.
00813     void findBoundsALL(CoefficientInfo *A,
00814                        CoefficientInfo *B,
00815                        BoundInfo *Bound,
00816                        unsigned K) const;
00817 
00818     /// findBoundsLT - Computes the upper and lower bounds for level K
00819     /// using the < direction. Records them in Bound.
00820     void findBoundsLT(CoefficientInfo *A,
00821                       CoefficientInfo *B,
00822                       BoundInfo *Bound,
00823                       unsigned K) const;
00824 
00825     /// findBoundsGT - Computes the upper and lower bounds for level K
00826     /// using the > direction. Records them in Bound.
00827     void findBoundsGT(CoefficientInfo *A,
00828                       CoefficientInfo *B,
00829                       BoundInfo *Bound,
00830                       unsigned K) const;
00831 
00832     /// findBoundsEQ - Computes the upper and lower bounds for level K
00833     /// using the = direction. Records them in Bound.
00834     void findBoundsEQ(CoefficientInfo *A,
00835                       CoefficientInfo *B,
00836                       BoundInfo *Bound,
00837                       unsigned K) const;
00838 
00839     /// intersectConstraints - Updates X with the intersection
00840     /// of the Constraints X and Y. Returns true if X has changed.
00841     bool intersectConstraints(Constraint *X,
00842                               const Constraint *Y);
00843 
00844     /// propagate - Review the constraints, looking for opportunities
00845     /// to simplify a subscript pair (Src and Dst).
00846     /// Return true if some simplification occurs.
00847     /// If the simplification isn't exact (that is, if it is conservative
00848     /// in terms of dependence), set consistent to false.
00849     bool propagate(const SCEV *&Src,
00850                    const SCEV *&Dst,
00851                    SmallBitVector &Loops,
00852                    SmallVectorImpl<Constraint> &Constraints,
00853                    bool &Consistent);
00854 
00855     /// propagateDistance - Attempt to propagate a distance
00856     /// constraint into a subscript pair (Src and Dst).
00857     /// Return true if some simplification occurs.
00858     /// If the simplification isn't exact (that is, if it is conservative
00859     /// in terms of dependence), set consistent to false.
00860     bool propagateDistance(const SCEV *&Src,
00861                            const SCEV *&Dst,
00862                            Constraint &CurConstraint,
00863                            bool &Consistent);
00864 
00865     /// propagatePoint - Attempt to propagate a point
00866     /// constraint into a subscript pair (Src and Dst).
00867     /// Return true if some simplification occurs.
00868     bool propagatePoint(const SCEV *&Src,
00869                         const SCEV *&Dst,
00870                         Constraint &CurConstraint);
00871 
00872     /// propagateLine - Attempt to propagate a line
00873     /// constraint into a subscript pair (Src and Dst).
00874     /// Return true if some simplification occurs.
00875     /// If the simplification isn't exact (that is, if it is conservative
00876     /// in terms of dependence), set consistent to false.
00877     bool propagateLine(const SCEV *&Src,
00878                        const SCEV *&Dst,
00879                        Constraint &CurConstraint,
00880                        bool &Consistent);
00881 
00882     /// findCoefficient - Given a linear SCEV,
00883     /// return the coefficient corresponding to specified loop.
00884     /// If there isn't one, return the SCEV constant 0.
00885     /// For example, given a*i + b*j + c*k, returning the coefficient
00886     /// corresponding to the j loop would yield b.
00887     const SCEV *findCoefficient(const SCEV *Expr,
00888                                 const Loop *TargetLoop) const;
00889 
00890     /// zeroCoefficient - Given a linear SCEV,
00891     /// return the SCEV given by zeroing out the coefficient
00892     /// corresponding to the specified loop.
00893     /// For example, given a*i + b*j + c*k, zeroing the coefficient
00894     /// corresponding to the j loop would yield a*i + c*k.
00895     const SCEV *zeroCoefficient(const SCEV *Expr,
00896                                 const Loop *TargetLoop) const;
00897 
00898     /// addToCoefficient - Given a linear SCEV Expr,
00899     /// return the SCEV given by adding some Value to the
00900     /// coefficient corresponding to the specified TargetLoop.
00901     /// For example, given a*i + b*j + c*k, adding 1 to the coefficient
00902     /// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
00903     const SCEV *addToCoefficient(const SCEV *Expr,
00904                                  const Loop *TargetLoop,
00905                                  const SCEV *Value)  const;
00906 
00907     /// updateDirection - Update direction vector entry
00908     /// based on the current constraint.
00909     void updateDirection(Dependence::DVEntry &Level,
00910                          const Constraint &CurConstraint) const;
00911 
00912     bool tryDelinearize(const SCEV *SrcSCEV, const SCEV *DstSCEV,
00913                         SmallVectorImpl<Subscript> &Pair,
00914                         const SCEV *ElementSize) const;
00915 
00916   public:
00917     static char ID; // Class identification, replacement for typeinfo
00918     DependenceAnalysis() : FunctionPass(ID) {
00919       initializeDependenceAnalysisPass(*PassRegistry::getPassRegistry());
00920     }
00921 
00922     bool runOnFunction(Function &F) override;
00923     void releaseMemory() override;
00924     void getAnalysisUsage(AnalysisUsage &) const override;
00925     void print(raw_ostream &, const Module * = nullptr) const override;
00926   }; // class DependenceAnalysis
00927 
00928   /// createDependenceAnalysisPass - This creates an instance of the
00929   /// DependenceAnalysis pass.
00930   FunctionPass *createDependenceAnalysisPass();
00931 
00932 } // namespace llvm
00933 
00934 #endif