Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:12

0001 //===-- llvm/Analysis/DependenceAnalysis.h -------------------- -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // DependenceAnalysis is an LLVM pass that analyses dependences between memory
0010 // accesses. Currently, it is an implementation of the approach described in
0011 //
0012 //            Practical Dependence Testing
0013 //            Goff, Kennedy, Tseng
0014 //            PLDI 1991
0015 //
0016 // There's a single entry point that analyzes the dependence between a pair
0017 // of memory references in a function, returning either NULL, for no dependence,
0018 // or a more-or-less detailed description of the dependence between them.
0019 //
0020 // This pass exists to support the DependenceGraph pass. There are two separate
0021 // passes because there's a useful separation of concerns. A dependence exists
0022 // if two conditions are met:
0023 //
0024 //    1) Two instructions reference the same memory location, and
0025 //    2) There is a flow of control leading from one instruction to the other.
0026 //
0027 // DependenceAnalysis attacks the first condition; DependenceGraph will attack
0028 // the second (it's not yet ready).
0029 //
0030 // Please note that this is work in progress and the interface is subject to
0031 // change.
0032 //
0033 // Plausible changes:
0034 //    Return a set of more precise dependences instead of just one dependence
0035 //    summarizing all.
0036 //
0037 //===----------------------------------------------------------------------===//
0038 
0039 #ifndef LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
0040 #define LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
0041 
0042 #include "llvm/ADT/SmallBitVector.h"
0043 #include "llvm/IR/Instructions.h"
0044 #include "llvm/IR/PassManager.h"
0045 #include "llvm/Pass.h"
0046 
0047 namespace llvm {
0048   class AAResults;
0049   template <typename T> class ArrayRef;
0050   class Loop;
0051   class LoopInfo;
0052   class ScalarEvolution;
0053   class SCEV;
0054   class SCEVConstant;
0055   class raw_ostream;
0056 
0057   /// Dependence - This class represents a dependence between two memory
0058   /// memory references in a function. It contains minimal information and
0059   /// is used in the very common situation where the compiler is unable to
0060   /// determine anything beyond the existence of a dependence; that is, it
0061   /// represents a confused dependence (see also FullDependence). In most
0062   /// cases (for output, flow, and anti dependences), the dependence implies
0063   /// an ordering, where the source must precede the destination; in contrast,
0064   /// input dependences are unordered.
0065   ///
0066   /// When a dependence graph is built, each Dependence will be a member of
0067   /// the set of predecessor edges for its destination instruction and a set
0068   /// if successor edges for its source instruction. These sets are represented
0069   /// as singly-linked lists, with the "next" fields stored in the dependence
0070   /// itelf.
0071   class Dependence {
0072   protected:
0073     Dependence(Dependence &&) = default;
0074     Dependence &operator=(Dependence &&) = default;
0075 
0076   public:
0077     Dependence(Instruction *Source, Instruction *Destination)
0078         : Src(Source), Dst(Destination) {}
0079     virtual ~Dependence() = default;
0080 
0081     /// Dependence::DVEntry - Each level in the distance/direction vector
0082     /// has a direction (or perhaps a union of several directions), and
0083     /// perhaps a distance.
0084     struct DVEntry {
0085       enum : unsigned char {
0086         NONE = 0,
0087         LT = 1,
0088         EQ = 2,
0089         LE = 3,
0090         GT = 4,
0091         NE = 5,
0092         GE = 6,
0093         ALL = 7
0094       };
0095       unsigned char Direction : 3; // Init to ALL, then refine.
0096       bool Scalar    : 1; // Init to true.
0097       bool PeelFirst : 1; // Peeling the first iteration will break dependence.
0098       bool PeelLast  : 1; // Peeling the last iteration will break the dependence.
0099       bool Splitable : 1; // Splitting the loop will break dependence.
0100       const SCEV *Distance = nullptr; // NULL implies no distance available.
0101       DVEntry()
0102           : Direction(ALL), Scalar(true), PeelFirst(false), PeelLast(false),
0103             Splitable(false) {}
0104     };
0105 
0106     /// getSrc - Returns the source instruction for this dependence.
0107     ///
0108     Instruction *getSrc() const { return Src; }
0109 
0110     /// getDst - Returns the destination instruction for this dependence.
0111     ///
0112     Instruction *getDst() const { return Dst; }
0113 
0114     /// isInput - Returns true if this is an input dependence.
0115     ///
0116     bool isInput() const;
0117 
0118     /// isOutput - Returns true if this is an output dependence.
0119     ///
0120     bool isOutput() const;
0121 
0122     /// isFlow - Returns true if this is a flow (aka true) dependence.
0123     ///
0124     bool isFlow() const;
0125 
0126     /// isAnti - Returns true if this is an anti dependence.
0127     ///
0128     bool isAnti() const;
0129 
0130     /// isOrdered - Returns true if dependence is Output, Flow, or Anti
0131     ///
0132     bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
0133 
0134     /// isUnordered - Returns true if dependence is Input
0135     ///
0136     bool isUnordered() const { return isInput(); }
0137 
0138     /// isLoopIndependent - Returns true if this is a loop-independent
0139     /// dependence.
0140     virtual bool isLoopIndependent() const { return true; }
0141 
0142     /// isConfused - Returns true if this dependence is confused
0143     /// (the compiler understands nothing and makes worst-case
0144     /// assumptions).
0145     virtual bool isConfused() const { return true; }
0146 
0147     /// isConsistent - Returns true if this dependence is consistent
0148     /// (occurs every time the source and destination are executed).
0149     virtual bool isConsistent() const { return false; }
0150 
0151     /// getLevels - Returns the number of common loops surrounding the
0152     /// source and destination of the dependence.
0153     virtual unsigned getLevels() const { return 0; }
0154 
0155     /// getDirection - Returns the direction associated with a particular
0156     /// level.
0157     virtual unsigned getDirection(unsigned Level) const { return DVEntry::ALL; }
0158 
0159     /// getDistance - Returns the distance (or NULL) associated with a
0160     /// particular level.
0161     virtual const SCEV *getDistance(unsigned Level) const { return nullptr; }
0162 
0163     /// Check if the direction vector is negative. A negative direction
0164     /// vector means Src and Dst are reversed in the actual program.
0165     virtual bool isDirectionNegative() const { return false; }
0166 
0167     /// If the direction vector is negative, normalize the direction
0168     /// vector to make it non-negative. Normalization is done by reversing
0169     /// Src and Dst, plus reversing the dependence directions and distances
0170     /// in the vector.
0171     virtual bool normalize(ScalarEvolution *SE) { return false; }
0172 
0173     /// isPeelFirst - Returns true if peeling the first iteration from
0174     /// this loop will break this dependence.
0175     virtual bool isPeelFirst(unsigned Level) const { return false; }
0176 
0177     /// isPeelLast - Returns true if peeling the last iteration from
0178     /// this loop will break this dependence.
0179     virtual bool isPeelLast(unsigned Level) const { return false; }
0180 
0181     /// isSplitable - Returns true if splitting this loop will break
0182     /// the dependence.
0183     virtual bool isSplitable(unsigned Level) const { return false; }
0184 
0185     /// isScalar - Returns true if a particular level is scalar; that is,
0186     /// if no subscript in the source or destination mention the induction
0187     /// variable associated with the loop at this level.
0188     virtual bool isScalar(unsigned Level) const;
0189 
0190     /// getNextPredecessor - Returns the value of the NextPredecessor
0191     /// field.
0192     const Dependence *getNextPredecessor() const { return NextPredecessor; }
0193 
0194     /// getNextSuccessor - Returns the value of the NextSuccessor
0195     /// field.
0196     const Dependence *getNextSuccessor() const { return NextSuccessor; }
0197 
0198     /// setNextPredecessor - Sets the value of the NextPredecessor
0199     /// field.
0200     void setNextPredecessor(const Dependence *pred) { NextPredecessor = pred; }
0201 
0202     /// setNextSuccessor - Sets the value of the NextSuccessor
0203     /// field.
0204     void setNextSuccessor(const Dependence *succ) { NextSuccessor = succ; }
0205 
0206     /// dump - For debugging purposes, dumps a dependence to OS.
0207     ///
0208     void dump(raw_ostream &OS) const;
0209 
0210   protected:
0211     Instruction *Src, *Dst;
0212 
0213   private:
0214     const Dependence *NextPredecessor = nullptr, *NextSuccessor = nullptr;
0215     friend class DependenceInfo;
0216   };
0217 
0218   /// FullDependence - This class represents a dependence between two memory
0219   /// references in a function. It contains detailed information about the
0220   /// dependence (direction vectors, etc.) and is used when the compiler is
0221   /// able to accurately analyze the interaction of the references; that is,
0222   /// it is not a confused dependence (see Dependence). In most cases
0223   /// (for output, flow, and anti dependences), the dependence implies an
0224   /// ordering, where the source must precede the destination; in contrast,
0225   /// input dependences are unordered.
0226   class FullDependence final : public Dependence {
0227   public:
0228     FullDependence(Instruction *Src, Instruction *Dst, bool LoopIndependent,
0229                    unsigned Levels);
0230 
0231     /// isLoopIndependent - Returns true if this is a loop-independent
0232     /// dependence.
0233     bool isLoopIndependent() const override { return LoopIndependent; }
0234 
0235     /// isConfused - Returns true if this dependence is confused
0236     /// (the compiler understands nothing and makes worst-case
0237     /// assumptions).
0238     bool isConfused() const override { return false; }
0239 
0240     /// isConsistent - Returns true if this dependence is consistent
0241     /// (occurs every time the source and destination are executed).
0242     bool isConsistent() const override { return Consistent; }
0243 
0244     /// getLevels - Returns the number of common loops surrounding the
0245     /// source and destination of the dependence.
0246     unsigned getLevels() const override { return Levels; }
0247 
0248     /// getDirection - Returns the direction associated with a particular
0249     /// level.
0250     unsigned getDirection(unsigned Level) const override;
0251 
0252     /// getDistance - Returns the distance (or NULL) associated with a
0253     /// particular level.
0254     const SCEV *getDistance(unsigned Level) const override;
0255 
0256     /// Check if the direction vector is negative. A negative direction
0257     /// vector means Src and Dst are reversed in the actual program.
0258     bool isDirectionNegative() const override;
0259 
0260     /// If the direction vector is negative, normalize the direction
0261     /// vector to make it non-negative. Normalization is done by reversing
0262     /// Src and Dst, plus reversing the dependence directions and distances
0263     /// in the vector.
0264     bool normalize(ScalarEvolution *SE) override;
0265 
0266     /// isPeelFirst - Returns true if peeling the first iteration from
0267     /// this loop will break this dependence.
0268     bool isPeelFirst(unsigned Level) const override;
0269 
0270     /// isPeelLast - Returns true if peeling the last iteration from
0271     /// this loop will break this dependence.
0272     bool isPeelLast(unsigned Level) const override;
0273 
0274     /// isSplitable - Returns true if splitting the loop will break
0275     /// the dependence.
0276     bool isSplitable(unsigned Level) const override;
0277 
0278     /// isScalar - Returns true if a particular level is scalar; that is,
0279     /// if no subscript in the source or destination mention the induction
0280     /// variable associated with the loop at this level.
0281     bool isScalar(unsigned Level) const override;
0282 
0283   private:
0284     unsigned short Levels;
0285     bool LoopIndependent;
0286     bool Consistent; // Init to true, then refine.
0287     std::unique_ptr<DVEntry[]> DV;
0288     friend class DependenceInfo;
0289   };
0290 
0291   /// DependenceInfo - This class is the main dependence-analysis driver.
0292   ///
0293   class DependenceInfo {
0294   public:
0295     DependenceInfo(Function *F, AAResults *AA, ScalarEvolution *SE,
0296                    LoopInfo *LI)
0297         : AA(AA), SE(SE), LI(LI), F(F) {}
0298 
0299     /// Handle transitive invalidation when the cached analysis results go away.
0300     bool invalidate(Function &F, const PreservedAnalyses &PA,
0301                     FunctionAnalysisManager::Invalidator &Inv);
0302 
0303     /// depends - Tests for a dependence between the Src and Dst instructions.
0304     /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
0305     /// FullDependence) with as much information as can be gleaned.
0306     /// The flag PossiblyLoopIndependent should be set by the caller
0307     /// if it appears that control flow can reach from Src to Dst
0308     /// without traversing a loop back edge.
0309     std::unique_ptr<Dependence> depends(Instruction *Src,
0310                                         Instruction *Dst,
0311                                         bool PossiblyLoopIndependent);
0312 
0313     /// getSplitIteration - Give a dependence that's splittable at some
0314     /// particular level, return the iteration that should be used to split
0315     /// the loop.
0316     ///
0317     /// Generally, the dependence analyzer will be used to build
0318     /// a dependence graph for a function (basically a map from instructions
0319     /// to dependences). Looking for cycles in the graph shows us loops
0320     /// that cannot be trivially vectorized/parallelized.
0321     ///
0322     /// We can try to improve the situation by examining all the dependences
0323     /// that make up the cycle, looking for ones we can break.
0324     /// Sometimes, peeling the first or last iteration of a loop will break
0325     /// dependences, and there are flags for those possibilities.
0326     /// Sometimes, splitting a loop at some other iteration will do the trick,
0327     /// and we've got a flag for that case. Rather than waste the space to
0328     /// record the exact iteration (since we rarely know), we provide
0329     /// a method that calculates the iteration. It's a drag that it must work
0330     /// from scratch, but wonderful in that it's possible.
0331     ///
0332     /// Here's an example:
0333     ///
0334     ///    for (i = 0; i < 10; i++)
0335     ///        A[i] = ...
0336     ///        ... = A[11 - i]
0337     ///
0338     /// There's a loop-carried flow dependence from the store to the load,
0339     /// found by the weak-crossing SIV test. The dependence will have a flag,
0340     /// indicating that the dependence can be broken by splitting the loop.
0341     /// Calling getSplitIteration will return 5.
0342     /// Splitting the loop breaks the dependence, like so:
0343     ///
0344     ///    for (i = 0; i <= 5; i++)
0345     ///        A[i] = ...
0346     ///        ... = A[11 - i]
0347     ///    for (i = 6; i < 10; i++)
0348     ///        A[i] = ...
0349     ///        ... = A[11 - i]
0350     ///
0351     /// breaks the dependence and allows us to vectorize/parallelize
0352     /// both loops.
0353     const SCEV *getSplitIteration(const Dependence &Dep, unsigned Level);
0354 
0355     Function *getFunction() const { return F; }
0356 
0357   private:
0358     AAResults *AA;
0359     ScalarEvolution *SE;
0360     LoopInfo *LI;
0361     Function *F;
0362 
0363     /// Subscript - This private struct represents a pair of subscripts from
0364     /// a pair of potentially multi-dimensional array references. We use a
0365     /// vector of them to guide subscript partitioning.
0366     struct Subscript {
0367       const SCEV *Src;
0368       const SCEV *Dst;
0369       enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
0370       SmallBitVector Loops;
0371       SmallBitVector GroupLoops;
0372       SmallBitVector Group;
0373     };
0374 
0375     struct CoefficientInfo {
0376       const SCEV *Coeff;
0377       const SCEV *PosPart;
0378       const SCEV *NegPart;
0379       const SCEV *Iterations;
0380     };
0381 
0382     struct BoundInfo {
0383       const SCEV *Iterations;
0384       const SCEV *Upper[8];
0385       const SCEV *Lower[8];
0386       unsigned char Direction;
0387       unsigned char DirSet;
0388     };
0389 
0390     /// Constraint - This private class represents a constraint, as defined
0391     /// in the paper
0392     ///
0393     ///           Practical Dependence Testing
0394     ///           Goff, Kennedy, Tseng
0395     ///           PLDI 1991
0396     ///
0397     /// There are 5 kinds of constraint, in a hierarchy.
0398     ///   1) Any - indicates no constraint, any dependence is possible.
0399     ///   2) Line - A line ax + by = c, where a, b, and c are parameters,
0400     ///             representing the dependence equation.
0401     ///   3) Distance - The value d of the dependence distance;
0402     ///   4) Point - A point <x, y> representing the dependence from
0403     ///              iteration x to iteration y.
0404     ///   5) Empty - No dependence is possible.
0405     class Constraint {
0406     private:
0407       enum ConstraintKind { Empty, Point, Distance, Line, Any } Kind;
0408       ScalarEvolution *SE;
0409       const SCEV *A;
0410       const SCEV *B;
0411       const SCEV *C;
0412       const Loop *AssociatedLoop;
0413 
0414     public:
0415       /// isEmpty - Return true if the constraint is of kind Empty.
0416       bool isEmpty() const { return Kind == Empty; }
0417 
0418       /// isPoint - Return true if the constraint is of kind Point.
0419       bool isPoint() const { return Kind == Point; }
0420 
0421       /// isDistance - Return true if the constraint is of kind Distance.
0422       bool isDistance() const { return Kind == Distance; }
0423 
0424       /// isLine - Return true if the constraint is of kind Line.
0425       /// Since Distance's can also be represented as Lines, we also return
0426       /// true if the constraint is of kind Distance.
0427       bool isLine() const { return Kind == Line || Kind == Distance; }
0428 
0429       /// isAny - Return true if the constraint is of kind Any;
0430       bool isAny() const { return Kind == Any; }
0431 
0432       /// getX - If constraint is a point <X, Y>, returns X.
0433       /// Otherwise assert.
0434       const SCEV *getX() const;
0435 
0436       /// getY - If constraint is a point <X, Y>, returns Y.
0437       /// Otherwise assert.
0438       const SCEV *getY() const;
0439 
0440       /// getA - If constraint is a line AX + BY = C, returns A.
0441       /// Otherwise assert.
0442       const SCEV *getA() const;
0443 
0444       /// getB - If constraint is a line AX + BY = C, returns B.
0445       /// Otherwise assert.
0446       const SCEV *getB() const;
0447 
0448       /// getC - If constraint is a line AX + BY = C, returns C.
0449       /// Otherwise assert.
0450       const SCEV *getC() const;
0451 
0452       /// getD - If constraint is a distance, returns D.
0453       /// Otherwise assert.
0454       const SCEV *getD() const;
0455 
0456       /// getAssociatedLoop - Returns the loop associated with this constraint.
0457       const Loop *getAssociatedLoop() const;
0458 
0459       /// setPoint - Change a constraint to Point.
0460       void setPoint(const SCEV *X, const SCEV *Y, const Loop *CurrentLoop);
0461 
0462       /// setLine - Change a constraint to Line.
0463       void setLine(const SCEV *A, const SCEV *B,
0464                    const SCEV *C, const Loop *CurrentLoop);
0465 
0466       /// setDistance - Change a constraint to Distance.
0467       void setDistance(const SCEV *D, const Loop *CurrentLoop);
0468 
0469       /// setEmpty - Change a constraint to Empty.
0470       void setEmpty();
0471 
0472       /// setAny - Change a constraint to Any.
0473       void setAny(ScalarEvolution *SE);
0474 
0475       /// dump - For debugging purposes. Dumps the constraint
0476       /// out to OS.
0477       void dump(raw_ostream &OS) const;
0478     };
0479 
0480     /// establishNestingLevels - Examines the loop nesting of the Src and Dst
0481     /// instructions and establishes their shared loops. Sets the variables
0482     /// CommonLevels, SrcLevels, and MaxLevels.
0483     /// The source and destination instructions needn't be contained in the same
0484     /// loop. The routine establishNestingLevels finds the level of most deeply
0485     /// nested loop that contains them both, CommonLevels. An instruction that's
0486     /// not contained in a loop is at level = 0. MaxLevels is equal to the level
0487     /// of the source plus the level of the destination, minus CommonLevels.
0488     /// This lets us allocate vectors MaxLevels in length, with room for every
0489     /// distinct loop referenced in both the source and destination subscripts.
0490     /// The variable SrcLevels is the nesting depth of the source instruction.
0491     /// It's used to help calculate distinct loops referenced by the destination.
0492     /// Here's the map from loops to levels:
0493     ///            0 - unused
0494     ///            1 - outermost common loop
0495     ///          ... - other common loops
0496     /// CommonLevels - innermost common loop
0497     ///          ... - loops containing Src but not Dst
0498     ///    SrcLevels - innermost loop containing Src but not Dst
0499     ///          ... - loops containing Dst but not Src
0500     ///    MaxLevels - innermost loop containing Dst but not Src
0501     /// Consider the follow code fragment:
0502     ///    for (a = ...) {
0503     ///      for (b = ...) {
0504     ///        for (c = ...) {
0505     ///          for (d = ...) {
0506     ///            A[] = ...;
0507     ///          }
0508     ///        }
0509     ///        for (e = ...) {
0510     ///          for (f = ...) {
0511     ///            for (g = ...) {
0512     ///              ... = A[];
0513     ///            }
0514     ///          }
0515     ///        }
0516     ///      }
0517     ///    }
0518     /// If we're looking at the possibility of a dependence between the store
0519     /// to A (the Src) and the load from A (the Dst), we'll note that they
0520     /// have 2 loops in common, so CommonLevels will equal 2 and the direction
0521     /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
0522     /// A map from loop names to level indices would look like
0523     ///     a - 1
0524     ///     b - 2 = CommonLevels
0525     ///     c - 3
0526     ///     d - 4 = SrcLevels
0527     ///     e - 5
0528     ///     f - 6
0529     ///     g - 7 = MaxLevels
0530     void establishNestingLevels(const Instruction *Src,
0531                                 const Instruction *Dst);
0532 
0533     unsigned CommonLevels, SrcLevels, MaxLevels;
0534 
0535     /// mapSrcLoop - Given one of the loops containing the source, return
0536     /// its level index in our numbering scheme.
0537     unsigned mapSrcLoop(const Loop *SrcLoop) const;
0538 
0539     /// mapDstLoop - Given one of the loops containing the destination,
0540     /// return its level index in our numbering scheme.
0541     unsigned mapDstLoop(const Loop *DstLoop) const;
0542 
0543     /// isLoopInvariant - Returns true if Expression is loop invariant
0544     /// in LoopNest.
0545     bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
0546 
0547     /// Makes sure all subscript pairs share the same integer type by
0548     /// sign-extending as necessary.
0549     /// Sign-extending a subscript is safe because getelementptr assumes the
0550     /// array subscripts are signed.
0551     void unifySubscriptType(ArrayRef<Subscript *> Pairs);
0552 
0553     /// removeMatchingExtensions - Examines a subscript pair.
0554     /// If the source and destination are identically sign (or zero)
0555     /// extended, it strips off the extension in an effort to
0556     /// simplify the actual analysis.
0557     void removeMatchingExtensions(Subscript *Pair);
0558 
0559     /// collectCommonLoops - Finds the set of loops from the LoopNest that
0560     /// have a level <= CommonLevels and are referred to by the SCEV Expression.
0561     void collectCommonLoops(const SCEV *Expression,
0562                             const Loop *LoopNest,
0563                             SmallBitVector &Loops) const;
0564 
0565     /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
0566     /// linear. Collect the set of loops mentioned by Src.
0567     bool checkSrcSubscript(const SCEV *Src,
0568                            const Loop *LoopNest,
0569                            SmallBitVector &Loops);
0570 
0571     /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
0572     /// linear. Collect the set of loops mentioned by Dst.
0573     bool checkDstSubscript(const SCEV *Dst,
0574                            const Loop *LoopNest,
0575                            SmallBitVector &Loops);
0576 
0577     /// isKnownPredicate - Compare X and Y using the predicate Pred.
0578     /// Basically a wrapper for SCEV::isKnownPredicate,
0579     /// but tries harder, especially in the presence of sign and zero
0580     /// extensions and symbolics.
0581     bool isKnownPredicate(ICmpInst::Predicate Pred,
0582                           const SCEV *X,
0583                           const SCEV *Y) const;
0584 
0585     /// isKnownLessThan - Compare to see if S is less than Size
0586     /// Another wrapper for isKnownNegative(S - max(Size, 1)) with some extra
0587     /// checking if S is an AddRec and we can prove lessthan using the loop
0588     /// bounds.
0589     bool isKnownLessThan(const SCEV *S, const SCEV *Size) const;
0590 
0591     /// isKnownNonNegative - Compare to see if S is known not to be negative
0592     /// Uses the fact that S comes from Ptr, which may be an inbound GEP,
0593     /// Proving there is no wrapping going on.
0594     bool isKnownNonNegative(const SCEV *S, const Value *Ptr) const;
0595 
0596     /// collectUpperBound - All subscripts are the same type (on my machine,
0597     /// an i64). The loop bound may be a smaller type. collectUpperBound
0598     /// find the bound, if available, and zero extends it to the Type T.
0599     /// (I zero extend since the bound should always be >= 0.)
0600     /// If no upper bound is available, return NULL.
0601     const SCEV *collectUpperBound(const Loop *l, Type *T) const;
0602 
0603     /// collectConstantUpperBound - Calls collectUpperBound(), then
0604     /// attempts to cast it to SCEVConstant. If the cast fails,
0605     /// returns NULL.
0606     const SCEVConstant *collectConstantUpperBound(const Loop *l, Type *T) const;
0607 
0608     /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
0609     /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
0610     /// Collects the associated loops in a set.
0611     Subscript::ClassificationKind classifyPair(const SCEV *Src,
0612                                            const Loop *SrcLoopNest,
0613                                            const SCEV *Dst,
0614                                            const Loop *DstLoopNest,
0615                                            SmallBitVector &Loops);
0616 
0617     /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
0618     /// Returns true if any possible dependence is disproved.
0619     /// If there might be a dependence, returns false.
0620     /// If the dependence isn't proven to exist,
0621     /// marks the Result as inconsistent.
0622     bool testZIV(const SCEV *Src,
0623                  const SCEV *Dst,
0624                  FullDependence &Result) const;
0625 
0626     /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
0627     /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
0628     /// i and j are induction variables, c1 and c2 are loop invariant,
0629     /// and a1 and a2 are constant.
0630     /// Returns true if any possible dependence is disproved.
0631     /// If there might be a dependence, returns false.
0632     /// Sets appropriate direction vector entry and, when possible,
0633     /// the distance vector entry.
0634     /// If the dependence isn't proven to exist,
0635     /// marks the Result as inconsistent.
0636     bool testSIV(const SCEV *Src,
0637                  const SCEV *Dst,
0638                  unsigned &Level,
0639                  FullDependence &Result,
0640                  Constraint &NewConstraint,
0641                  const SCEV *&SplitIter) const;
0642 
0643     /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
0644     /// Things of the form [c1 + a1*i] and [c2 + a2*j]
0645     /// where i and j are induction variables, c1 and c2 are loop invariant,
0646     /// and a1 and a2 are constant.
0647     /// With minor algebra, this test can also be used for things like
0648     /// [c1 + a1*i + a2*j][c2].
0649     /// Returns true if any possible dependence is disproved.
0650     /// If there might be a dependence, returns false.
0651     /// Marks the Result as inconsistent.
0652     bool testRDIV(const SCEV *Src,
0653                   const SCEV *Dst,
0654                   FullDependence &Result) const;
0655 
0656     /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
0657     /// Returns true if dependence disproved.
0658     /// Can sometimes refine direction vectors.
0659     bool testMIV(const SCEV *Src,
0660                  const SCEV *Dst,
0661                  const SmallBitVector &Loops,
0662                  FullDependence &Result) const;
0663 
0664     /// strongSIVtest - Tests the strong SIV subscript pair (Src and Dst)
0665     /// for dependence.
0666     /// Things of the form [c1 + a*i] and [c2 + a*i],
0667     /// where i is an induction variable, c1 and c2 are loop invariant,
0668     /// and a is a constant
0669     /// Returns true if any possible dependence is disproved.
0670     /// If there might be a dependence, returns false.
0671     /// Sets appropriate direction and distance.
0672     bool strongSIVtest(const SCEV *Coeff,
0673                        const SCEV *SrcConst,
0674                        const SCEV *DstConst,
0675                        const Loop *CurrentLoop,
0676                        unsigned Level,
0677                        FullDependence &Result,
0678                        Constraint &NewConstraint) const;
0679 
0680     /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
0681     /// (Src and Dst) for dependence.
0682     /// Things of the form [c1 + a*i] and [c2 - a*i],
0683     /// where i is an induction variable, c1 and c2 are loop invariant,
0684     /// and a is a constant.
0685     /// Returns true if any possible dependence is disproved.
0686     /// If there might be a dependence, returns false.
0687     /// Sets appropriate direction entry.
0688     /// Set consistent to false.
0689     /// Marks the dependence as splitable.
0690     bool weakCrossingSIVtest(const SCEV *SrcCoeff,
0691                              const SCEV *SrcConst,
0692                              const SCEV *DstConst,
0693                              const Loop *CurrentLoop,
0694                              unsigned Level,
0695                              FullDependence &Result,
0696                              Constraint &NewConstraint,
0697                              const SCEV *&SplitIter) const;
0698 
0699     /// ExactSIVtest - Tests the SIV subscript pair
0700     /// (Src and Dst) for dependence.
0701     /// Things of the form [c1 + a1*i] and [c2 + a2*i],
0702     /// where i is an induction variable, c1 and c2 are loop invariant,
0703     /// and a1 and a2 are constant.
0704     /// Returns true if any possible dependence is disproved.
0705     /// If there might be a dependence, returns false.
0706     /// Sets appropriate direction entry.
0707     /// Set consistent to false.
0708     bool exactSIVtest(const SCEV *SrcCoeff,
0709                       const SCEV *DstCoeff,
0710                       const SCEV *SrcConst,
0711                       const SCEV *DstConst,
0712                       const Loop *CurrentLoop,
0713                       unsigned Level,
0714                       FullDependence &Result,
0715                       Constraint &NewConstraint) const;
0716 
0717     /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
0718     /// (Src and Dst) for dependence.
0719     /// Things of the form [c1] and [c2 + a*i],
0720     /// where i is an induction variable, c1 and c2 are loop invariant,
0721     /// and a is a constant. See also weakZeroDstSIVtest.
0722     /// Returns true if any possible dependence is disproved.
0723     /// If there might be a dependence, returns false.
0724     /// Sets appropriate direction entry.
0725     /// Set consistent to false.
0726     /// If loop peeling will break the dependence, mark appropriately.
0727     bool weakZeroSrcSIVtest(const SCEV *DstCoeff,
0728                             const SCEV *SrcConst,
0729                             const SCEV *DstConst,
0730                             const Loop *CurrentLoop,
0731                             unsigned Level,
0732                             FullDependence &Result,
0733                             Constraint &NewConstraint) const;
0734 
0735     /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
0736     /// (Src and Dst) for dependence.
0737     /// Things of the form [c1 + a*i] and [c2],
0738     /// where i is an induction variable, c1 and c2 are loop invariant,
0739     /// and a is a constant. See also weakZeroSrcSIVtest.
0740     /// Returns true if any possible dependence is disproved.
0741     /// If there might be a dependence, returns false.
0742     /// Sets appropriate direction entry.
0743     /// Set consistent to false.
0744     /// If loop peeling will break the dependence, mark appropriately.
0745     bool weakZeroDstSIVtest(const SCEV *SrcCoeff,
0746                             const SCEV *SrcConst,
0747                             const SCEV *DstConst,
0748                             const Loop *CurrentLoop,
0749                             unsigned Level,
0750                             FullDependence &Result,
0751                             Constraint &NewConstraint) const;
0752 
0753     /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
0754     /// Things of the form [c1 + a*i] and [c2 + b*j],
0755     /// where i and j are induction variable, c1 and c2 are loop invariant,
0756     /// and a and b are constants.
0757     /// Returns true if any possible dependence is disproved.
0758     /// Marks the result as inconsistent.
0759     /// Works in some cases that symbolicRDIVtest doesn't,
0760     /// and vice versa.
0761     bool exactRDIVtest(const SCEV *SrcCoeff,
0762                        const SCEV *DstCoeff,
0763                        const SCEV *SrcConst,
0764                        const SCEV *DstConst,
0765                        const Loop *SrcLoop,
0766                        const Loop *DstLoop,
0767                        FullDependence &Result) const;
0768 
0769     /// symbolicRDIVtest - Tests the RDIV subscript pair for dependence.
0770     /// Things of the form [c1 + a*i] and [c2 + b*j],
0771     /// where i and j are induction variable, c1 and c2 are loop invariant,
0772     /// and a and b are constants.
0773     /// Returns true if any possible dependence is disproved.
0774     /// Marks the result as inconsistent.
0775     /// Works in some cases that exactRDIVtest doesn't,
0776     /// and vice versa. Can also be used as a backup for
0777     /// ordinary SIV tests.
0778     bool symbolicRDIVtest(const SCEV *SrcCoeff,
0779                           const SCEV *DstCoeff,
0780                           const SCEV *SrcConst,
0781                           const SCEV *DstConst,
0782                           const Loop *SrcLoop,
0783                           const Loop *DstLoop) const;
0784 
0785     /// gcdMIVtest - Tests an MIV subscript pair for dependence.
0786     /// Returns true if any possible dependence is disproved.
0787     /// Marks the result as inconsistent.
0788     /// Can sometimes disprove the equal direction for 1 or more loops.
0789     //  Can handle some symbolics that even the SIV tests don't get,
0790     /// so we use it as a backup for everything.
0791     bool gcdMIVtest(const SCEV *Src,
0792                     const SCEV *Dst,
0793                     FullDependence &Result) const;
0794 
0795     /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
0796     /// Returns true if any possible dependence is disproved.
0797     /// Marks the result as inconsistent.
0798     /// Computes directions.
0799     bool banerjeeMIVtest(const SCEV *Src,
0800                          const SCEV *Dst,
0801                          const SmallBitVector &Loops,
0802                          FullDependence &Result) const;
0803 
0804     /// collectCoefficientInfo - Walks through the subscript,
0805     /// collecting each coefficient, the associated loop bounds,
0806     /// and recording its positive and negative parts for later use.
0807     CoefficientInfo *collectCoeffInfo(const SCEV *Subscript,
0808                                       bool SrcFlag,
0809                                       const SCEV *&Constant) const;
0810 
0811     /// getPositivePart - X^+ = max(X, 0).
0812     ///
0813     const SCEV *getPositivePart(const SCEV *X) const;
0814 
0815     /// getNegativePart - X^- = min(X, 0).
0816     ///
0817     const SCEV *getNegativePart(const SCEV *X) const;
0818 
0819     /// getLowerBound - Looks through all the bounds info and
0820     /// computes the lower bound given the current direction settings
0821     /// at each level.
0822     const SCEV *getLowerBound(BoundInfo *Bound) const;
0823 
0824     /// getUpperBound - Looks through all the bounds info and
0825     /// computes the upper bound given the current direction settings
0826     /// at each level.
0827     const SCEV *getUpperBound(BoundInfo *Bound) const;
0828 
0829     /// exploreDirections - Hierarchically expands the direction vector
0830     /// search space, combining the directions of discovered dependences
0831     /// in the DirSet field of Bound. Returns the number of distinct
0832     /// dependences discovered. If the dependence is disproved,
0833     /// it will return 0.
0834     unsigned exploreDirections(unsigned Level,
0835                                CoefficientInfo *A,
0836                                CoefficientInfo *B,
0837                                BoundInfo *Bound,
0838                                const SmallBitVector &Loops,
0839                                unsigned &DepthExpanded,
0840                                const SCEV *Delta) const;
0841 
0842     /// testBounds - Returns true iff the current bounds are plausible.
0843     bool testBounds(unsigned char DirKind,
0844                     unsigned Level,
0845                     BoundInfo *Bound,
0846                     const SCEV *Delta) const;
0847 
0848     /// findBoundsALL - Computes the upper and lower bounds for level K
0849     /// using the * direction. Records them in Bound.
0850     void findBoundsALL(CoefficientInfo *A,
0851                        CoefficientInfo *B,
0852                        BoundInfo *Bound,
0853                        unsigned K) const;
0854 
0855     /// findBoundsLT - Computes the upper and lower bounds for level K
0856     /// using the < direction. Records them in Bound.
0857     void findBoundsLT(CoefficientInfo *A,
0858                       CoefficientInfo *B,
0859                       BoundInfo *Bound,
0860                       unsigned K) const;
0861 
0862     /// findBoundsGT - Computes the upper and lower bounds for level K
0863     /// using the > direction. Records them in Bound.
0864     void findBoundsGT(CoefficientInfo *A,
0865                       CoefficientInfo *B,
0866                       BoundInfo *Bound,
0867                       unsigned K) const;
0868 
0869     /// findBoundsEQ - Computes the upper and lower bounds for level K
0870     /// using the = direction. Records them in Bound.
0871     void findBoundsEQ(CoefficientInfo *A,
0872                       CoefficientInfo *B,
0873                       BoundInfo *Bound,
0874                       unsigned K) const;
0875 
0876     /// intersectConstraints - Updates X with the intersection
0877     /// of the Constraints X and Y. Returns true if X has changed.
0878     bool intersectConstraints(Constraint *X,
0879                               const Constraint *Y);
0880 
0881     /// propagate - Review the constraints, looking for opportunities
0882     /// to simplify a subscript pair (Src and Dst).
0883     /// Return true if some simplification occurs.
0884     /// If the simplification isn't exact (that is, if it is conservative
0885     /// in terms of dependence), set consistent to false.
0886     bool propagate(const SCEV *&Src,
0887                    const SCEV *&Dst,
0888                    SmallBitVector &Loops,
0889                    SmallVectorImpl<Constraint> &Constraints,
0890                    bool &Consistent);
0891 
0892     /// propagateDistance - Attempt to propagate a distance
0893     /// constraint into a subscript pair (Src and Dst).
0894     /// Return true if some simplification occurs.
0895     /// If the simplification isn't exact (that is, if it is conservative
0896     /// in terms of dependence), set consistent to false.
0897     bool propagateDistance(const SCEV *&Src,
0898                            const SCEV *&Dst,
0899                            Constraint &CurConstraint,
0900                            bool &Consistent);
0901 
0902     /// propagatePoint - Attempt to propagate a point
0903     /// constraint into a subscript pair (Src and Dst).
0904     /// Return true if some simplification occurs.
0905     bool propagatePoint(const SCEV *&Src,
0906                         const SCEV *&Dst,
0907                         Constraint &CurConstraint);
0908 
0909     /// propagateLine - Attempt to propagate a line
0910     /// constraint into a subscript pair (Src and Dst).
0911     /// Return true if some simplification occurs.
0912     /// If the simplification isn't exact (that is, if it is conservative
0913     /// in terms of dependence), set consistent to false.
0914     bool propagateLine(const SCEV *&Src,
0915                        const SCEV *&Dst,
0916                        Constraint &CurConstraint,
0917                        bool &Consistent);
0918 
0919     /// findCoefficient - Given a linear SCEV,
0920     /// return the coefficient corresponding to specified loop.
0921     /// If there isn't one, return the SCEV constant 0.
0922     /// For example, given a*i + b*j + c*k, returning the coefficient
0923     /// corresponding to the j loop would yield b.
0924     const SCEV *findCoefficient(const SCEV *Expr,
0925                                 const Loop *TargetLoop) const;
0926 
0927     /// zeroCoefficient - Given a linear SCEV,
0928     /// return the SCEV given by zeroing out the coefficient
0929     /// corresponding to the specified loop.
0930     /// For example, given a*i + b*j + c*k, zeroing the coefficient
0931     /// corresponding to the j loop would yield a*i + c*k.
0932     const SCEV *zeroCoefficient(const SCEV *Expr,
0933                                 const Loop *TargetLoop) const;
0934 
0935     /// addToCoefficient - Given a linear SCEV Expr,
0936     /// return the SCEV given by adding some Value to the
0937     /// coefficient corresponding to the specified TargetLoop.
0938     /// For example, given a*i + b*j + c*k, adding 1 to the coefficient
0939     /// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
0940     const SCEV *addToCoefficient(const SCEV *Expr,
0941                                  const Loop *TargetLoop,
0942                                  const SCEV *Value)  const;
0943 
0944     /// updateDirection - Update direction vector entry
0945     /// based on the current constraint.
0946     void updateDirection(Dependence::DVEntry &Level,
0947                          const Constraint &CurConstraint) const;
0948 
0949     /// Given a linear access function, tries to recover subscripts
0950     /// for each dimension of the array element access.
0951     bool tryDelinearize(Instruction *Src, Instruction *Dst,
0952                         SmallVectorImpl<Subscript> &Pair);
0953 
0954     /// Tries to delinearize \p Src and \p Dst access functions for a fixed size
0955     /// multi-dimensional array. Calls tryDelinearizeFixedSizeImpl() to
0956     /// delinearize \p Src and \p Dst separately,
0957     bool tryDelinearizeFixedSize(Instruction *Src, Instruction *Dst,
0958                                  const SCEV *SrcAccessFn,
0959                                  const SCEV *DstAccessFn,
0960                                  SmallVectorImpl<const SCEV *> &SrcSubscripts,
0961                                  SmallVectorImpl<const SCEV *> &DstSubscripts);
0962 
0963     /// Tries to delinearize access function for a multi-dimensional array with
0964     /// symbolic runtime sizes.
0965     /// Returns true upon success and false otherwise.
0966     bool tryDelinearizeParametricSize(
0967         Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
0968         const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
0969         SmallVectorImpl<const SCEV *> &DstSubscripts);
0970 
0971     /// checkSubscript - Helper function for checkSrcSubscript and
0972     /// checkDstSubscript to avoid duplicate code
0973     bool checkSubscript(const SCEV *Expr, const Loop *LoopNest,
0974                         SmallBitVector &Loops, bool IsSrc);
0975   }; // class DependenceInfo
0976 
0977   /// AnalysisPass to compute dependence information in a function
0978   class DependenceAnalysis : public AnalysisInfoMixin<DependenceAnalysis> {
0979   public:
0980     typedef DependenceInfo Result;
0981     Result run(Function &F, FunctionAnalysisManager &FAM);
0982 
0983   private:
0984     static AnalysisKey Key;
0985     friend struct AnalysisInfoMixin<DependenceAnalysis>;
0986   }; // class DependenceAnalysis
0987 
0988   /// Printer pass to dump DA results.
0989   struct DependenceAnalysisPrinterPass
0990       : public PassInfoMixin<DependenceAnalysisPrinterPass> {
0991     DependenceAnalysisPrinterPass(raw_ostream &OS,
0992                                   bool NormalizeResults = false)
0993         : OS(OS), NormalizeResults(NormalizeResults) {}
0994 
0995     PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
0996 
0997     static bool isRequired() { return true; }
0998 
0999   private:
1000     raw_ostream &OS;
1001     bool NormalizeResults;
1002   }; // class DependenceAnalysisPrinterPass
1003 
1004   /// Legacy pass manager pass to access dependence information
1005   class DependenceAnalysisWrapperPass : public FunctionPass {
1006   public:
1007     static char ID; // Class identification, replacement for typeinfo
1008     DependenceAnalysisWrapperPass();
1009 
1010     bool runOnFunction(Function &F) override;
1011     void releaseMemory() override;
1012     void getAnalysisUsage(AnalysisUsage &) const override;
1013     void print(raw_ostream &, const Module * = nullptr) const override;
1014     DependenceInfo &getDI() const;
1015 
1016   private:
1017     std::unique_ptr<DependenceInfo> info;
1018   }; // class DependenceAnalysisWrapperPass
1019 
1020   /// createDependenceAnalysisPass - This creates an instance of the
1021   /// DependenceAnalysis wrapper pass.
1022   FunctionPass *createDependenceAnalysisWrapperPass();
1023 
1024 } // namespace llvm
1025 
1026 #endif