10b57cec5SDimitry Andric //===-- llvm/Analysis/DependenceAnalysis.h -------------------- -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // DependenceAnalysis is an LLVM pass that analyses dependences between memory
100b57cec5SDimitry Andric // accesses. Currently, it is an implementation of the approach described in
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //            Practical Dependence Testing
130b57cec5SDimitry Andric //            Goff, Kennedy, Tseng
140b57cec5SDimitry Andric //            PLDI 1991
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric // There's a single entry point that analyzes the dependence between a pair
170b57cec5SDimitry Andric // of memory references in a function, returning either NULL, for no dependence,
180b57cec5SDimitry Andric // or a more-or-less detailed description of the dependence between them.
190b57cec5SDimitry Andric //
200b57cec5SDimitry Andric // This pass exists to support the DependenceGraph pass. There are two separate
210b57cec5SDimitry Andric // passes because there's a useful separation of concerns. A dependence exists
220b57cec5SDimitry Andric // if two conditions are met:
230b57cec5SDimitry Andric //
240b57cec5SDimitry Andric //    1) Two instructions reference the same memory location, and
250b57cec5SDimitry Andric //    2) There is a flow of control leading from one instruction to the other.
260b57cec5SDimitry Andric //
270b57cec5SDimitry Andric // DependenceAnalysis attacks the first condition; DependenceGraph will attack
280b57cec5SDimitry Andric // the second (it's not yet ready).
290b57cec5SDimitry Andric //
300b57cec5SDimitry Andric // Please note that this is work in progress and the interface is subject to
310b57cec5SDimitry Andric // change.
320b57cec5SDimitry Andric //
330b57cec5SDimitry Andric // Plausible changes:
340b57cec5SDimitry Andric //    Return a set of more precise dependences instead of just one dependence
350b57cec5SDimitry Andric //    summarizing all.
360b57cec5SDimitry Andric //
370b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric #ifndef LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
400b57cec5SDimitry Andric #define LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric #include "llvm/ADT/SmallBitVector.h"
430b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
445ffd83dbSDimitry Andric #include "llvm/IR/PassManager.h"
450b57cec5SDimitry Andric #include "llvm/Pass.h"
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric namespace llvm {
485ffd83dbSDimitry Andric   class AAResults;
490b57cec5SDimitry Andric   template <typename T> class ArrayRef;
500b57cec5SDimitry Andric   class Loop;
510b57cec5SDimitry Andric   class LoopInfo;
520b57cec5SDimitry Andric   class ScalarEvolution;
530b57cec5SDimitry Andric   class SCEV;
540b57cec5SDimitry Andric   class SCEVConstant;
550b57cec5SDimitry Andric   class raw_ostream;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   /// Dependence - This class represents a dependence between two memory
580b57cec5SDimitry Andric   /// memory references in a function. It contains minimal information and
590b57cec5SDimitry Andric   /// is used in the very common situation where the compiler is unable to
600b57cec5SDimitry Andric   /// determine anything beyond the existence of a dependence; that is, it
610b57cec5SDimitry Andric   /// represents a confused dependence (see also FullDependence). In most
620b57cec5SDimitry Andric   /// cases (for output, flow, and anti dependences), the dependence implies
630b57cec5SDimitry Andric   /// an ordering, where the source must precede the destination; in contrast,
640b57cec5SDimitry Andric   /// input dependences are unordered.
650b57cec5SDimitry Andric   ///
660b57cec5SDimitry Andric   /// When a dependence graph is built, each Dependence will be a member of
670b57cec5SDimitry Andric   /// the set of predecessor edges for its destination instruction and a set
680b57cec5SDimitry Andric   /// if successor edges for its source instruction. These sets are represented
690b57cec5SDimitry Andric   /// as singly-linked lists, with the "next" fields stored in the dependence
700b57cec5SDimitry Andric   /// itelf.
710b57cec5SDimitry Andric   class Dependence {
720b57cec5SDimitry Andric   protected:
730b57cec5SDimitry Andric     Dependence(Dependence &&) = default;
740b57cec5SDimitry Andric     Dependence &operator=(Dependence &&) = default;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   public:
Dependence(Instruction * Source,Instruction * Destination)7704eeddc0SDimitry Andric     Dependence(Instruction *Source, Instruction *Destination)
7804eeddc0SDimitry Andric         : Src(Source), Dst(Destination) {}
791fd87a68SDimitry Andric     virtual ~Dependence() = default;
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric     /// Dependence::DVEntry - Each level in the distance/direction vector
820b57cec5SDimitry Andric     /// has a direction (or perhaps a union of several directions), and
830b57cec5SDimitry Andric     /// perhaps a distance.
840b57cec5SDimitry Andric     struct DVEntry {
85bdd1243dSDimitry Andric       enum : unsigned char {
86bdd1243dSDimitry Andric         NONE = 0,
870b57cec5SDimitry Andric         LT = 1,
880b57cec5SDimitry Andric         EQ = 2,
890b57cec5SDimitry Andric         LE = 3,
900b57cec5SDimitry Andric         GT = 4,
910b57cec5SDimitry Andric         NE = 5,
920b57cec5SDimitry Andric         GE = 6,
93bdd1243dSDimitry Andric         ALL = 7
94bdd1243dSDimitry Andric       };
950b57cec5SDimitry Andric       unsigned char Direction : 3; // Init to ALL, then refine.
960b57cec5SDimitry Andric       bool Scalar    : 1; // Init to true.
970b57cec5SDimitry Andric       bool PeelFirst : 1; // Peeling the first iteration will break dependence.
980b57cec5SDimitry Andric       bool PeelLast  : 1; // Peeling the last iteration will break the dependence.
990b57cec5SDimitry Andric       bool Splitable : 1; // Splitting the loop will break dependence.
10004eeddc0SDimitry Andric       const SCEV *Distance = nullptr; // NULL implies no distance available.
DVEntryDVEntry10104eeddc0SDimitry Andric       DVEntry()
10204eeddc0SDimitry Andric           : Direction(ALL), Scalar(true), PeelFirst(false), PeelLast(false),
10304eeddc0SDimitry Andric             Splitable(false) {}
1040b57cec5SDimitry Andric     };
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric     /// getSrc - Returns the source instruction for this dependence.
1070b57cec5SDimitry Andric     ///
getSrc()1080b57cec5SDimitry Andric     Instruction *getSrc() const { return Src; }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric     /// getDst - Returns the destination instruction for this dependence.
1110b57cec5SDimitry Andric     ///
getDst()1120b57cec5SDimitry Andric     Instruction *getDst() const { return Dst; }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric     /// isInput - Returns true if this is an input dependence.
1150b57cec5SDimitry Andric     ///
1160b57cec5SDimitry Andric     bool isInput() const;
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric     /// isOutput - Returns true if this is an output dependence.
1190b57cec5SDimitry Andric     ///
1200b57cec5SDimitry Andric     bool isOutput() const;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric     /// isFlow - Returns true if this is a flow (aka true) dependence.
1230b57cec5SDimitry Andric     ///
1240b57cec5SDimitry Andric     bool isFlow() const;
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric     /// isAnti - Returns true if this is an anti dependence.
1270b57cec5SDimitry Andric     ///
1280b57cec5SDimitry Andric     bool isAnti() const;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric     /// isOrdered - Returns true if dependence is Output, Flow, or Anti
1310b57cec5SDimitry Andric     ///
isOrdered()1320b57cec5SDimitry Andric     bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric     /// isUnordered - Returns true if dependence is Input
1350b57cec5SDimitry Andric     ///
isUnordered()1360b57cec5SDimitry Andric     bool isUnordered() const { return isInput(); }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric     /// isLoopIndependent - Returns true if this is a loop-independent
1390b57cec5SDimitry Andric     /// dependence.
isLoopIndependent()1400b57cec5SDimitry Andric     virtual bool isLoopIndependent() const { return true; }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric     /// isConfused - Returns true if this dependence is confused
1430b57cec5SDimitry Andric     /// (the compiler understands nothing and makes worst-case
1440b57cec5SDimitry Andric     /// assumptions).
isConfused()1450b57cec5SDimitry Andric     virtual bool isConfused() const { return true; }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric     /// isConsistent - Returns true if this dependence is consistent
1480b57cec5SDimitry Andric     /// (occurs every time the source and destination are executed).
isConsistent()1490b57cec5SDimitry Andric     virtual bool isConsistent() const { return false; }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric     /// getLevels - Returns the number of common loops surrounding the
1520b57cec5SDimitry Andric     /// source and destination of the dependence.
getLevels()1530b57cec5SDimitry Andric     virtual unsigned getLevels() const { return 0; }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric     /// getDirection - Returns the direction associated with a particular
1560b57cec5SDimitry Andric     /// level.
getDirection(unsigned Level)1570b57cec5SDimitry Andric     virtual unsigned getDirection(unsigned Level) const { return DVEntry::ALL; }
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric     /// getDistance - Returns the distance (or NULL) associated with a
1600b57cec5SDimitry Andric     /// particular level.
getDistance(unsigned Level)1610b57cec5SDimitry Andric     virtual const SCEV *getDistance(unsigned Level) const { return nullptr; }
1620b57cec5SDimitry Andric 
163bdd1243dSDimitry Andric     /// Check if the direction vector is negative. A negative direction
164bdd1243dSDimitry Andric     /// vector means Src and Dst are reversed in the actual program.
isDirectionNegative()165bdd1243dSDimitry Andric     virtual bool isDirectionNegative() const { return false; }
166bdd1243dSDimitry Andric 
167bdd1243dSDimitry Andric     /// If the direction vector is negative, normalize the direction
168bdd1243dSDimitry Andric     /// vector to make it non-negative. Normalization is done by reversing
169bdd1243dSDimitry Andric     /// Src and Dst, plus reversing the dependence directions and distances
170bdd1243dSDimitry Andric     /// in the vector.
normalize(ScalarEvolution * SE)171bdd1243dSDimitry Andric     virtual bool normalize(ScalarEvolution *SE) { return false; }
172bdd1243dSDimitry Andric 
1730b57cec5SDimitry Andric     /// isPeelFirst - Returns true if peeling the first iteration from
1740b57cec5SDimitry Andric     /// this loop will break this dependence.
isPeelFirst(unsigned Level)1750b57cec5SDimitry Andric     virtual bool isPeelFirst(unsigned Level) const { return false; }
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric     /// isPeelLast - Returns true if peeling the last iteration from
1780b57cec5SDimitry Andric     /// this loop will break this dependence.
isPeelLast(unsigned Level)1790b57cec5SDimitry Andric     virtual bool isPeelLast(unsigned Level) const { return false; }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric     /// isSplitable - Returns true if splitting this loop will break
1820b57cec5SDimitry Andric     /// the dependence.
isSplitable(unsigned Level)1830b57cec5SDimitry Andric     virtual bool isSplitable(unsigned Level) const { return false; }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric     /// isScalar - Returns true if a particular level is scalar; that is,
1860b57cec5SDimitry Andric     /// if no subscript in the source or destination mention the induction
1870b57cec5SDimitry Andric     /// variable associated with the loop at this level.
1880b57cec5SDimitry Andric     virtual bool isScalar(unsigned Level) const;
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric     /// getNextPredecessor - Returns the value of the NextPredecessor
1910b57cec5SDimitry Andric     /// field.
getNextPredecessor()1920b57cec5SDimitry Andric     const Dependence *getNextPredecessor() const { return NextPredecessor; }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric     /// getNextSuccessor - Returns the value of the NextSuccessor
1950b57cec5SDimitry Andric     /// field.
getNextSuccessor()1960b57cec5SDimitry Andric     const Dependence *getNextSuccessor() const { return NextSuccessor; }
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric     /// setNextPredecessor - Sets the value of the NextPredecessor
1990b57cec5SDimitry Andric     /// field.
setNextPredecessor(const Dependence * pred)2000b57cec5SDimitry Andric     void setNextPredecessor(const Dependence *pred) { NextPredecessor = pred; }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric     /// setNextSuccessor - Sets the value of the NextSuccessor
2030b57cec5SDimitry Andric     /// field.
setNextSuccessor(const Dependence * succ)2040b57cec5SDimitry Andric     void setNextSuccessor(const Dependence *succ) { NextSuccessor = succ; }
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric     /// dump - For debugging purposes, dumps a dependence to OS.
2070b57cec5SDimitry Andric     ///
2080b57cec5SDimitry Andric     void dump(raw_ostream &OS) const;
2090b57cec5SDimitry Andric 
210bdd1243dSDimitry Andric   protected:
2110b57cec5SDimitry Andric     Instruction *Src, *Dst;
212bdd1243dSDimitry Andric 
213bdd1243dSDimitry Andric   private:
21404eeddc0SDimitry Andric     const Dependence *NextPredecessor = nullptr, *NextSuccessor = nullptr;
2150b57cec5SDimitry Andric     friend class DependenceInfo;
2160b57cec5SDimitry Andric   };
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   /// FullDependence - This class represents a dependence between two memory
2190b57cec5SDimitry Andric   /// references in a function. It contains detailed information about the
2200b57cec5SDimitry Andric   /// dependence (direction vectors, etc.) and is used when the compiler is
2210b57cec5SDimitry Andric   /// able to accurately analyze the interaction of the references; that is,
2220b57cec5SDimitry Andric   /// it is not a confused dependence (see Dependence). In most cases
2230b57cec5SDimitry Andric   /// (for output, flow, and anti dependences), the dependence implies an
2240b57cec5SDimitry Andric   /// ordering, where the source must precede the destination; in contrast,
2250b57cec5SDimitry Andric   /// input dependences are unordered.
2260b57cec5SDimitry Andric   class FullDependence final : public Dependence {
2270b57cec5SDimitry Andric   public:
2280b57cec5SDimitry Andric     FullDependence(Instruction *Src, Instruction *Dst, bool LoopIndependent,
2290b57cec5SDimitry Andric                    unsigned Levels);
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric     /// isLoopIndependent - Returns true if this is a loop-independent
2320b57cec5SDimitry Andric     /// dependence.
isLoopIndependent()2330b57cec5SDimitry Andric     bool isLoopIndependent() const override { return LoopIndependent; }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric     /// isConfused - Returns true if this dependence is confused
2360b57cec5SDimitry Andric     /// (the compiler understands nothing and makes worst-case
2370b57cec5SDimitry Andric     /// assumptions).
isConfused()2380b57cec5SDimitry Andric     bool isConfused() const override { return false; }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric     /// isConsistent - Returns true if this dependence is consistent
2410b57cec5SDimitry Andric     /// (occurs every time the source and destination are executed).
isConsistent()2420b57cec5SDimitry Andric     bool isConsistent() const override { return Consistent; }
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric     /// getLevels - Returns the number of common loops surrounding the
2450b57cec5SDimitry Andric     /// source and destination of the dependence.
getLevels()2460b57cec5SDimitry Andric     unsigned getLevels() const override { return Levels; }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric     /// getDirection - Returns the direction associated with a particular
2490b57cec5SDimitry Andric     /// level.
2500b57cec5SDimitry Andric     unsigned getDirection(unsigned Level) const override;
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric     /// getDistance - Returns the distance (or NULL) associated with a
2530b57cec5SDimitry Andric     /// particular level.
2540b57cec5SDimitry Andric     const SCEV *getDistance(unsigned Level) const override;
2550b57cec5SDimitry Andric 
256bdd1243dSDimitry Andric     /// Check if the direction vector is negative. A negative direction
257bdd1243dSDimitry Andric     /// vector means Src and Dst are reversed in the actual program.
258bdd1243dSDimitry Andric     bool isDirectionNegative() const override;
259bdd1243dSDimitry Andric 
260bdd1243dSDimitry Andric     /// If the direction vector is negative, normalize the direction
261bdd1243dSDimitry Andric     /// vector to make it non-negative. Normalization is done by reversing
262bdd1243dSDimitry Andric     /// Src and Dst, plus reversing the dependence directions and distances
263bdd1243dSDimitry Andric     /// in the vector.
264bdd1243dSDimitry Andric     bool normalize(ScalarEvolution *SE) override;
265bdd1243dSDimitry Andric 
2660b57cec5SDimitry Andric     /// isPeelFirst - Returns true if peeling the first iteration from
2670b57cec5SDimitry Andric     /// this loop will break this dependence.
2680b57cec5SDimitry Andric     bool isPeelFirst(unsigned Level) const override;
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric     /// isPeelLast - Returns true if peeling the last iteration from
2710b57cec5SDimitry Andric     /// this loop will break this dependence.
2720b57cec5SDimitry Andric     bool isPeelLast(unsigned Level) const override;
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric     /// isSplitable - Returns true if splitting the loop will break
2750b57cec5SDimitry Andric     /// the dependence.
2760b57cec5SDimitry Andric     bool isSplitable(unsigned Level) const override;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric     /// isScalar - Returns true if a particular level is scalar; that is,
2790b57cec5SDimitry Andric     /// if no subscript in the source or destination mention the induction
2800b57cec5SDimitry Andric     /// variable associated with the loop at this level.
2810b57cec5SDimitry Andric     bool isScalar(unsigned Level) const override;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   private:
2840b57cec5SDimitry Andric     unsigned short Levels;
2850b57cec5SDimitry Andric     bool LoopIndependent;
2860b57cec5SDimitry Andric     bool Consistent; // Init to true, then refine.
2870b57cec5SDimitry Andric     std::unique_ptr<DVEntry[]> DV;
2880b57cec5SDimitry Andric     friend class DependenceInfo;
2890b57cec5SDimitry Andric   };
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   /// DependenceInfo - This class is the main dependence-analysis driver.
2920b57cec5SDimitry Andric   ///
2930b57cec5SDimitry Andric   class DependenceInfo {
2940b57cec5SDimitry Andric   public:
DependenceInfo(Function * F,AAResults * AA,ScalarEvolution * SE,LoopInfo * LI)2955ffd83dbSDimitry Andric     DependenceInfo(Function *F, AAResults *AA, ScalarEvolution *SE,
2960b57cec5SDimitry Andric                    LoopInfo *LI)
2970b57cec5SDimitry Andric         : AA(AA), SE(SE), LI(LI), F(F) {}
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric     /// Handle transitive invalidation when the cached analysis results go away.
3000b57cec5SDimitry Andric     bool invalidate(Function &F, const PreservedAnalyses &PA,
3010b57cec5SDimitry Andric                     FunctionAnalysisManager::Invalidator &Inv);
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric     /// depends - Tests for a dependence between the Src and Dst instructions.
3040b57cec5SDimitry Andric     /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
3050b57cec5SDimitry Andric     /// FullDependence) with as much information as can be gleaned.
3060b57cec5SDimitry Andric     /// The flag PossiblyLoopIndependent should be set by the caller
3070b57cec5SDimitry Andric     /// if it appears that control flow can reach from Src to Dst
3080b57cec5SDimitry Andric     /// without traversing a loop back edge.
3090b57cec5SDimitry Andric     std::unique_ptr<Dependence> depends(Instruction *Src,
3100b57cec5SDimitry Andric                                         Instruction *Dst,
3110b57cec5SDimitry Andric                                         bool PossiblyLoopIndependent);
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric     /// getSplitIteration - Give a dependence that's splittable at some
3140b57cec5SDimitry Andric     /// particular level, return the iteration that should be used to split
3150b57cec5SDimitry Andric     /// the loop.
3160b57cec5SDimitry Andric     ///
3170b57cec5SDimitry Andric     /// Generally, the dependence analyzer will be used to build
3180b57cec5SDimitry Andric     /// a dependence graph for a function (basically a map from instructions
3190b57cec5SDimitry Andric     /// to dependences). Looking for cycles in the graph shows us loops
3200b57cec5SDimitry Andric     /// that cannot be trivially vectorized/parallelized.
3210b57cec5SDimitry Andric     ///
3220b57cec5SDimitry Andric     /// We can try to improve the situation by examining all the dependences
3230b57cec5SDimitry Andric     /// that make up the cycle, looking for ones we can break.
3240b57cec5SDimitry Andric     /// Sometimes, peeling the first or last iteration of a loop will break
3250b57cec5SDimitry Andric     /// dependences, and there are flags for those possibilities.
3260b57cec5SDimitry Andric     /// Sometimes, splitting a loop at some other iteration will do the trick,
3270b57cec5SDimitry Andric     /// and we've got a flag for that case. Rather than waste the space to
3280b57cec5SDimitry Andric     /// record the exact iteration (since we rarely know), we provide
3290b57cec5SDimitry Andric     /// a method that calculates the iteration. It's a drag that it must work
3300b57cec5SDimitry Andric     /// from scratch, but wonderful in that it's possible.
3310b57cec5SDimitry Andric     ///
3320b57cec5SDimitry Andric     /// Here's an example:
3330b57cec5SDimitry Andric     ///
3340b57cec5SDimitry Andric     ///    for (i = 0; i < 10; i++)
3350b57cec5SDimitry Andric     ///        A[i] = ...
3360b57cec5SDimitry Andric     ///        ... = A[11 - i]
3370b57cec5SDimitry Andric     ///
3380b57cec5SDimitry Andric     /// There's a loop-carried flow dependence from the store to the load,
3390b57cec5SDimitry Andric     /// found by the weak-crossing SIV test. The dependence will have a flag,
3400b57cec5SDimitry Andric     /// indicating that the dependence can be broken by splitting the loop.
3410b57cec5SDimitry Andric     /// Calling getSplitIteration will return 5.
3420b57cec5SDimitry Andric     /// Splitting the loop breaks the dependence, like so:
3430b57cec5SDimitry Andric     ///
3440b57cec5SDimitry Andric     ///    for (i = 0; i <= 5; i++)
3450b57cec5SDimitry Andric     ///        A[i] = ...
3460b57cec5SDimitry Andric     ///        ... = A[11 - i]
3470b57cec5SDimitry Andric     ///    for (i = 6; i < 10; i++)
3480b57cec5SDimitry Andric     ///        A[i] = ...
3490b57cec5SDimitry Andric     ///        ... = A[11 - i]
3500b57cec5SDimitry Andric     ///
3510b57cec5SDimitry Andric     /// breaks the dependence and allows us to vectorize/parallelize
3520b57cec5SDimitry Andric     /// both loops.
3530b57cec5SDimitry Andric     const SCEV *getSplitIteration(const Dependence &Dep, unsigned Level);
3540b57cec5SDimitry Andric 
getFunction()3550b57cec5SDimitry Andric     Function *getFunction() const { return F; }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric   private:
3585ffd83dbSDimitry Andric     AAResults *AA;
3590b57cec5SDimitry Andric     ScalarEvolution *SE;
3600b57cec5SDimitry Andric     LoopInfo *LI;
3610b57cec5SDimitry Andric     Function *F;
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric     /// Subscript - This private struct represents a pair of subscripts from
3640b57cec5SDimitry Andric     /// a pair of potentially multi-dimensional array references. We use a
3650b57cec5SDimitry Andric     /// vector of them to guide subscript partitioning.
3660b57cec5SDimitry Andric     struct Subscript {
3670b57cec5SDimitry Andric       const SCEV *Src;
3680b57cec5SDimitry Andric       const SCEV *Dst;
3690b57cec5SDimitry Andric       enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
3700b57cec5SDimitry Andric       SmallBitVector Loops;
3710b57cec5SDimitry Andric       SmallBitVector GroupLoops;
3720b57cec5SDimitry Andric       SmallBitVector Group;
3730b57cec5SDimitry Andric     };
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric     struct CoefficientInfo {
3760b57cec5SDimitry Andric       const SCEV *Coeff;
3770b57cec5SDimitry Andric       const SCEV *PosPart;
3780b57cec5SDimitry Andric       const SCEV *NegPart;
3790b57cec5SDimitry Andric       const SCEV *Iterations;
3800b57cec5SDimitry Andric     };
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric     struct BoundInfo {
3830b57cec5SDimitry Andric       const SCEV *Iterations;
3840b57cec5SDimitry Andric       const SCEV *Upper[8];
3850b57cec5SDimitry Andric       const SCEV *Lower[8];
3860b57cec5SDimitry Andric       unsigned char Direction;
3870b57cec5SDimitry Andric       unsigned char DirSet;
3880b57cec5SDimitry Andric     };
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric     /// Constraint - This private class represents a constraint, as defined
3910b57cec5SDimitry Andric     /// in the paper
3920b57cec5SDimitry Andric     ///
3930b57cec5SDimitry Andric     ///           Practical Dependence Testing
3940b57cec5SDimitry Andric     ///           Goff, Kennedy, Tseng
3950b57cec5SDimitry Andric     ///           PLDI 1991
3960b57cec5SDimitry Andric     ///
3970b57cec5SDimitry Andric     /// There are 5 kinds of constraint, in a hierarchy.
3980b57cec5SDimitry Andric     ///   1) Any - indicates no constraint, any dependence is possible.
3990b57cec5SDimitry Andric     ///   2) Line - A line ax + by = c, where a, b, and c are parameters,
4000b57cec5SDimitry Andric     ///             representing the dependence equation.
4010b57cec5SDimitry Andric     ///   3) Distance - The value d of the dependence distance;
4020b57cec5SDimitry Andric     ///   4) Point - A point <x, y> representing the dependence from
4030b57cec5SDimitry Andric     ///              iteration x to iteration y.
4040b57cec5SDimitry Andric     ///   5) Empty - No dependence is possible.
4050b57cec5SDimitry Andric     class Constraint {
4060b57cec5SDimitry Andric     private:
4070b57cec5SDimitry Andric       enum ConstraintKind { Empty, Point, Distance, Line, Any } Kind;
4080b57cec5SDimitry Andric       ScalarEvolution *SE;
4090b57cec5SDimitry Andric       const SCEV *A;
4100b57cec5SDimitry Andric       const SCEV *B;
4110b57cec5SDimitry Andric       const SCEV *C;
4120b57cec5SDimitry Andric       const Loop *AssociatedLoop;
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric     public:
4150b57cec5SDimitry Andric       /// isEmpty - Return true if the constraint is of kind Empty.
isEmpty()4160b57cec5SDimitry Andric       bool isEmpty() const { return Kind == Empty; }
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric       /// isPoint - Return true if the constraint is of kind Point.
isPoint()4190b57cec5SDimitry Andric       bool isPoint() const { return Kind == Point; }
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric       /// isDistance - Return true if the constraint is of kind Distance.
isDistance()4220b57cec5SDimitry Andric       bool isDistance() const { return Kind == Distance; }
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric       /// isLine - Return true if the constraint is of kind Line.
4250b57cec5SDimitry Andric       /// Since Distance's can also be represented as Lines, we also return
4260b57cec5SDimitry Andric       /// true if the constraint is of kind Distance.
isLine()4270b57cec5SDimitry Andric       bool isLine() const { return Kind == Line || Kind == Distance; }
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric       /// isAny - Return true if the constraint is of kind Any;
isAny()4300b57cec5SDimitry Andric       bool isAny() const { return Kind == Any; }
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric       /// getX - If constraint is a point <X, Y>, returns X.
4330b57cec5SDimitry Andric       /// Otherwise assert.
4340b57cec5SDimitry Andric       const SCEV *getX() const;
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric       /// getY - If constraint is a point <X, Y>, returns Y.
4370b57cec5SDimitry Andric       /// Otherwise assert.
4380b57cec5SDimitry Andric       const SCEV *getY() const;
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric       /// getA - If constraint is a line AX + BY = C, returns A.
4410b57cec5SDimitry Andric       /// Otherwise assert.
4420b57cec5SDimitry Andric       const SCEV *getA() const;
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric       /// getB - If constraint is a line AX + BY = C, returns B.
4450b57cec5SDimitry Andric       /// Otherwise assert.
4460b57cec5SDimitry Andric       const SCEV *getB() const;
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric       /// getC - If constraint is a line AX + BY = C, returns C.
4490b57cec5SDimitry Andric       /// Otherwise assert.
4500b57cec5SDimitry Andric       const SCEV *getC() const;
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric       /// getD - If constraint is a distance, returns D.
4530b57cec5SDimitry Andric       /// Otherwise assert.
4540b57cec5SDimitry Andric       const SCEV *getD() const;
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric       /// getAssociatedLoop - Returns the loop associated with this constraint.
4570b57cec5SDimitry Andric       const Loop *getAssociatedLoop() const;
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric       /// setPoint - Change a constraint to Point.
4600b57cec5SDimitry Andric       void setPoint(const SCEV *X, const SCEV *Y, const Loop *CurrentLoop);
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric       /// setLine - Change a constraint to Line.
4630b57cec5SDimitry Andric       void setLine(const SCEV *A, const SCEV *B,
4640b57cec5SDimitry Andric                    const SCEV *C, const Loop *CurrentLoop);
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric       /// setDistance - Change a constraint to Distance.
4670b57cec5SDimitry Andric       void setDistance(const SCEV *D, const Loop *CurrentLoop);
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric       /// setEmpty - Change a constraint to Empty.
4700b57cec5SDimitry Andric       void setEmpty();
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric       /// setAny - Change a constraint to Any.
4730b57cec5SDimitry Andric       void setAny(ScalarEvolution *SE);
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric       /// dump - For debugging purposes. Dumps the constraint
4760b57cec5SDimitry Andric       /// out to OS.
4770b57cec5SDimitry Andric       void dump(raw_ostream &OS) const;
4780b57cec5SDimitry Andric     };
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric     /// establishNestingLevels - Examines the loop nesting of the Src and Dst
4810b57cec5SDimitry Andric     /// instructions and establishes their shared loops. Sets the variables
4820b57cec5SDimitry Andric     /// CommonLevels, SrcLevels, and MaxLevels.
4830b57cec5SDimitry Andric     /// The source and destination instructions needn't be contained in the same
4840b57cec5SDimitry Andric     /// loop. The routine establishNestingLevels finds the level of most deeply
4850b57cec5SDimitry Andric     /// nested loop that contains them both, CommonLevels. An instruction that's
4860b57cec5SDimitry Andric     /// not contained in a loop is at level = 0. MaxLevels is equal to the level
4870b57cec5SDimitry Andric     /// of the source plus the level of the destination, minus CommonLevels.
4880b57cec5SDimitry Andric     /// This lets us allocate vectors MaxLevels in length, with room for every
4890b57cec5SDimitry Andric     /// distinct loop referenced in both the source and destination subscripts.
4900b57cec5SDimitry Andric     /// The variable SrcLevels is the nesting depth of the source instruction.
4910b57cec5SDimitry Andric     /// It's used to help calculate distinct loops referenced by the destination.
4920b57cec5SDimitry Andric     /// Here's the map from loops to levels:
4930b57cec5SDimitry Andric     ///            0 - unused
4940b57cec5SDimitry Andric     ///            1 - outermost common loop
4950b57cec5SDimitry Andric     ///          ... - other common loops
4960b57cec5SDimitry Andric     /// CommonLevels - innermost common loop
4970b57cec5SDimitry Andric     ///          ... - loops containing Src but not Dst
4980b57cec5SDimitry Andric     ///    SrcLevels - innermost loop containing Src but not Dst
4990b57cec5SDimitry Andric     ///          ... - loops containing Dst but not Src
5000b57cec5SDimitry Andric     ///    MaxLevels - innermost loop containing Dst but not Src
5010b57cec5SDimitry Andric     /// Consider the follow code fragment:
5020b57cec5SDimitry Andric     ///    for (a = ...) {
5030b57cec5SDimitry Andric     ///      for (b = ...) {
5040b57cec5SDimitry Andric     ///        for (c = ...) {
5050b57cec5SDimitry Andric     ///          for (d = ...) {
5060b57cec5SDimitry Andric     ///            A[] = ...;
5070b57cec5SDimitry Andric     ///          }
5080b57cec5SDimitry Andric     ///        }
5090b57cec5SDimitry Andric     ///        for (e = ...) {
5100b57cec5SDimitry Andric     ///          for (f = ...) {
5110b57cec5SDimitry Andric     ///            for (g = ...) {
5120b57cec5SDimitry Andric     ///              ... = A[];
5130b57cec5SDimitry Andric     ///            }
5140b57cec5SDimitry Andric     ///          }
5150b57cec5SDimitry Andric     ///        }
5160b57cec5SDimitry Andric     ///      }
5170b57cec5SDimitry Andric     ///    }
5180b57cec5SDimitry Andric     /// If we're looking at the possibility of a dependence between the store
5190b57cec5SDimitry Andric     /// to A (the Src) and the load from A (the Dst), we'll note that they
5200b57cec5SDimitry Andric     /// have 2 loops in common, so CommonLevels will equal 2 and the direction
5210b57cec5SDimitry Andric     /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
5220b57cec5SDimitry Andric     /// A map from loop names to level indices would look like
5230b57cec5SDimitry Andric     ///     a - 1
5240b57cec5SDimitry Andric     ///     b - 2 = CommonLevels
5250b57cec5SDimitry Andric     ///     c - 3
5260b57cec5SDimitry Andric     ///     d - 4 = SrcLevels
5270b57cec5SDimitry Andric     ///     e - 5
5280b57cec5SDimitry Andric     ///     f - 6
5290b57cec5SDimitry Andric     ///     g - 7 = MaxLevels
5300b57cec5SDimitry Andric     void establishNestingLevels(const Instruction *Src,
5310b57cec5SDimitry Andric                                 const Instruction *Dst);
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric     unsigned CommonLevels, SrcLevels, MaxLevels;
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric     /// mapSrcLoop - Given one of the loops containing the source, return
5360b57cec5SDimitry Andric     /// its level index in our numbering scheme.
5370b57cec5SDimitry Andric     unsigned mapSrcLoop(const Loop *SrcLoop) const;
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric     /// mapDstLoop - Given one of the loops containing the destination,
5400b57cec5SDimitry Andric     /// return its level index in our numbering scheme.
5410b57cec5SDimitry Andric     unsigned mapDstLoop(const Loop *DstLoop) const;
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric     /// isLoopInvariant - Returns true if Expression is loop invariant
5440b57cec5SDimitry Andric     /// in LoopNest.
5450b57cec5SDimitry Andric     bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric     /// Makes sure all subscript pairs share the same integer type by
5480b57cec5SDimitry Andric     /// sign-extending as necessary.
5490b57cec5SDimitry Andric     /// Sign-extending a subscript is safe because getelementptr assumes the
5500b57cec5SDimitry Andric     /// array subscripts are signed.
5510b57cec5SDimitry Andric     void unifySubscriptType(ArrayRef<Subscript *> Pairs);
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric     /// removeMatchingExtensions - Examines a subscript pair.
5540b57cec5SDimitry Andric     /// If the source and destination are identically sign (or zero)
5550b57cec5SDimitry Andric     /// extended, it strips off the extension in an effort to
5560b57cec5SDimitry Andric     /// simplify the actual analysis.
5570b57cec5SDimitry Andric     void removeMatchingExtensions(Subscript *Pair);
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric     /// collectCommonLoops - Finds the set of loops from the LoopNest that
5600b57cec5SDimitry Andric     /// have a level <= CommonLevels and are referred to by the SCEV Expression.
5610b57cec5SDimitry Andric     void collectCommonLoops(const SCEV *Expression,
5620b57cec5SDimitry Andric                             const Loop *LoopNest,
5630b57cec5SDimitry Andric                             SmallBitVector &Loops) const;
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric     /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
5660b57cec5SDimitry Andric     /// linear. Collect the set of loops mentioned by Src.
5670b57cec5SDimitry Andric     bool checkSrcSubscript(const SCEV *Src,
5680b57cec5SDimitry Andric                            const Loop *LoopNest,
5690b57cec5SDimitry Andric                            SmallBitVector &Loops);
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric     /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
5720b57cec5SDimitry Andric     /// linear. Collect the set of loops mentioned by Dst.
5730b57cec5SDimitry Andric     bool checkDstSubscript(const SCEV *Dst,
5740b57cec5SDimitry Andric                            const Loop *LoopNest,
5750b57cec5SDimitry Andric                            SmallBitVector &Loops);
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric     /// isKnownPredicate - Compare X and Y using the predicate Pred.
5780b57cec5SDimitry Andric     /// Basically a wrapper for SCEV::isKnownPredicate,
5790b57cec5SDimitry Andric     /// but tries harder, especially in the presence of sign and zero
5800b57cec5SDimitry Andric     /// extensions and symbolics.
5810b57cec5SDimitry Andric     bool isKnownPredicate(ICmpInst::Predicate Pred,
5820b57cec5SDimitry Andric                           const SCEV *X,
5830b57cec5SDimitry Andric                           const SCEV *Y) const;
5840b57cec5SDimitry Andric 
5850b57cec5SDimitry Andric     /// isKnownLessThan - Compare to see if S is less than Size
5860b57cec5SDimitry Andric     /// Another wrapper for isKnownNegative(S - max(Size, 1)) with some extra
5870b57cec5SDimitry Andric     /// checking if S is an AddRec and we can prove lessthan using the loop
5880b57cec5SDimitry Andric     /// bounds.
5890b57cec5SDimitry Andric     bool isKnownLessThan(const SCEV *S, const SCEV *Size) const;
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric     /// isKnownNonNegative - Compare to see if S is known not to be negative
5920b57cec5SDimitry Andric     /// Uses the fact that S comes from Ptr, which may be an inbound GEP,
5930b57cec5SDimitry Andric     /// Proving there is no wrapping going on.
5940b57cec5SDimitry Andric     bool isKnownNonNegative(const SCEV *S, const Value *Ptr) const;
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric     /// collectUpperBound - All subscripts are the same type (on my machine,
5970b57cec5SDimitry Andric     /// an i64). The loop bound may be a smaller type. collectUpperBound
5980b57cec5SDimitry Andric     /// find the bound, if available, and zero extends it to the Type T.
5990b57cec5SDimitry Andric     /// (I zero extend since the bound should always be >= 0.)
6000b57cec5SDimitry Andric     /// If no upper bound is available, return NULL.
6010b57cec5SDimitry Andric     const SCEV *collectUpperBound(const Loop *l, Type *T) const;
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric     /// collectConstantUpperBound - Calls collectUpperBound(), then
6040b57cec5SDimitry Andric     /// attempts to cast it to SCEVConstant. If the cast fails,
6050b57cec5SDimitry Andric     /// returns NULL.
6060b57cec5SDimitry Andric     const SCEVConstant *collectConstantUpperBound(const Loop *l, Type *T) const;
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric     /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
6090b57cec5SDimitry Andric     /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
6100b57cec5SDimitry Andric     /// Collects the associated loops in a set.
6110b57cec5SDimitry Andric     Subscript::ClassificationKind classifyPair(const SCEV *Src,
6120b57cec5SDimitry Andric                                            const Loop *SrcLoopNest,
6130b57cec5SDimitry Andric                                            const SCEV *Dst,
6140b57cec5SDimitry Andric                                            const Loop *DstLoopNest,
6150b57cec5SDimitry Andric                                            SmallBitVector &Loops);
6160b57cec5SDimitry Andric 
6170b57cec5SDimitry Andric     /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
6180b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
6190b57cec5SDimitry Andric     /// If there might be a dependence, returns false.
6200b57cec5SDimitry Andric     /// If the dependence isn't proven to exist,
6210b57cec5SDimitry Andric     /// marks the Result as inconsistent.
6220b57cec5SDimitry Andric     bool testZIV(const SCEV *Src,
6230b57cec5SDimitry Andric                  const SCEV *Dst,
6240b57cec5SDimitry Andric                  FullDependence &Result) const;
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric     /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
6270b57cec5SDimitry Andric     /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
6280b57cec5SDimitry Andric     /// i and j are induction variables, c1 and c2 are loop invariant,
6290b57cec5SDimitry Andric     /// and a1 and a2 are constant.
6300b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
6310b57cec5SDimitry Andric     /// If there might be a dependence, returns false.
6320b57cec5SDimitry Andric     /// Sets appropriate direction vector entry and, when possible,
6330b57cec5SDimitry Andric     /// the distance vector entry.
6340b57cec5SDimitry Andric     /// If the dependence isn't proven to exist,
6350b57cec5SDimitry Andric     /// marks the Result as inconsistent.
6360b57cec5SDimitry Andric     bool testSIV(const SCEV *Src,
6370b57cec5SDimitry Andric                  const SCEV *Dst,
6380b57cec5SDimitry Andric                  unsigned &Level,
6390b57cec5SDimitry Andric                  FullDependence &Result,
6400b57cec5SDimitry Andric                  Constraint &NewConstraint,
6410b57cec5SDimitry Andric                  const SCEV *&SplitIter) const;
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric     /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
6440b57cec5SDimitry Andric     /// Things of the form [c1 + a1*i] and [c2 + a2*j]
6450b57cec5SDimitry Andric     /// where i and j are induction variables, c1 and c2 are loop invariant,
6460b57cec5SDimitry Andric     /// and a1 and a2 are constant.
6470b57cec5SDimitry Andric     /// With minor algebra, this test can also be used for things like
6480b57cec5SDimitry Andric     /// [c1 + a1*i + a2*j][c2].
6490b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
6500b57cec5SDimitry Andric     /// If there might be a dependence, returns false.
6510b57cec5SDimitry Andric     /// Marks the Result as inconsistent.
6520b57cec5SDimitry Andric     bool testRDIV(const SCEV *Src,
6530b57cec5SDimitry Andric                   const SCEV *Dst,
6540b57cec5SDimitry Andric                   FullDependence &Result) const;
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric     /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
6570b57cec5SDimitry Andric     /// Returns true if dependence disproved.
6580b57cec5SDimitry Andric     /// Can sometimes refine direction vectors.
6590b57cec5SDimitry Andric     bool testMIV(const SCEV *Src,
6600b57cec5SDimitry Andric                  const SCEV *Dst,
6610b57cec5SDimitry Andric                  const SmallBitVector &Loops,
6620b57cec5SDimitry Andric                  FullDependence &Result) const;
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric     /// strongSIVtest - Tests the strong SIV subscript pair (Src and Dst)
6650b57cec5SDimitry Andric     /// for dependence.
6660b57cec5SDimitry Andric     /// Things of the form [c1 + a*i] and [c2 + a*i],
6670b57cec5SDimitry Andric     /// where i is an induction variable, c1 and c2 are loop invariant,
6680b57cec5SDimitry Andric     /// and a is a constant
6690b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
6700b57cec5SDimitry Andric     /// If there might be a dependence, returns false.
6710b57cec5SDimitry Andric     /// Sets appropriate direction and distance.
6720b57cec5SDimitry Andric     bool strongSIVtest(const SCEV *Coeff,
6730b57cec5SDimitry Andric                        const SCEV *SrcConst,
6740b57cec5SDimitry Andric                        const SCEV *DstConst,
6750b57cec5SDimitry Andric                        const Loop *CurrentLoop,
6760b57cec5SDimitry Andric                        unsigned Level,
6770b57cec5SDimitry Andric                        FullDependence &Result,
6780b57cec5SDimitry Andric                        Constraint &NewConstraint) const;
6790b57cec5SDimitry Andric 
6800b57cec5SDimitry Andric     /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
6810b57cec5SDimitry Andric     /// (Src and Dst) for dependence.
6820b57cec5SDimitry Andric     /// Things of the form [c1 + a*i] and [c2 - a*i],
6830b57cec5SDimitry Andric     /// where i is an induction variable, c1 and c2 are loop invariant,
6840b57cec5SDimitry Andric     /// and a is a constant.
6850b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
6860b57cec5SDimitry Andric     /// If there might be a dependence, returns false.
6870b57cec5SDimitry Andric     /// Sets appropriate direction entry.
6880b57cec5SDimitry Andric     /// Set consistent to false.
6890b57cec5SDimitry Andric     /// Marks the dependence as splitable.
6900b57cec5SDimitry Andric     bool weakCrossingSIVtest(const SCEV *SrcCoeff,
6910b57cec5SDimitry Andric                              const SCEV *SrcConst,
6920b57cec5SDimitry Andric                              const SCEV *DstConst,
6930b57cec5SDimitry Andric                              const Loop *CurrentLoop,
6940b57cec5SDimitry Andric                              unsigned Level,
6950b57cec5SDimitry Andric                              FullDependence &Result,
6960b57cec5SDimitry Andric                              Constraint &NewConstraint,
6970b57cec5SDimitry Andric                              const SCEV *&SplitIter) const;
6980b57cec5SDimitry Andric 
6990b57cec5SDimitry Andric     /// ExactSIVtest - Tests the SIV subscript pair
7000b57cec5SDimitry Andric     /// (Src and Dst) for dependence.
7010b57cec5SDimitry Andric     /// Things of the form [c1 + a1*i] and [c2 + a2*i],
7020b57cec5SDimitry Andric     /// where i is an induction variable, c1 and c2 are loop invariant,
7030b57cec5SDimitry Andric     /// and a1 and a2 are constant.
7040b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
7050b57cec5SDimitry Andric     /// If there might be a dependence, returns false.
7060b57cec5SDimitry Andric     /// Sets appropriate direction entry.
7070b57cec5SDimitry Andric     /// Set consistent to false.
7080b57cec5SDimitry Andric     bool exactSIVtest(const SCEV *SrcCoeff,
7090b57cec5SDimitry Andric                       const SCEV *DstCoeff,
7100b57cec5SDimitry Andric                       const SCEV *SrcConst,
7110b57cec5SDimitry Andric                       const SCEV *DstConst,
7120b57cec5SDimitry Andric                       const Loop *CurrentLoop,
7130b57cec5SDimitry Andric                       unsigned Level,
7140b57cec5SDimitry Andric                       FullDependence &Result,
7150b57cec5SDimitry Andric                       Constraint &NewConstraint) const;
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric     /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
7180b57cec5SDimitry Andric     /// (Src and Dst) for dependence.
7190b57cec5SDimitry Andric     /// Things of the form [c1] and [c2 + a*i],
7200b57cec5SDimitry Andric     /// where i is an induction variable, c1 and c2 are loop invariant,
7210b57cec5SDimitry Andric     /// and a is a constant. See also weakZeroDstSIVtest.
7220b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
7230b57cec5SDimitry Andric     /// If there might be a dependence, returns false.
7240b57cec5SDimitry Andric     /// Sets appropriate direction entry.
7250b57cec5SDimitry Andric     /// Set consistent to false.
7260b57cec5SDimitry Andric     /// If loop peeling will break the dependence, mark appropriately.
7270b57cec5SDimitry Andric     bool weakZeroSrcSIVtest(const SCEV *DstCoeff,
7280b57cec5SDimitry Andric                             const SCEV *SrcConst,
7290b57cec5SDimitry Andric                             const SCEV *DstConst,
7300b57cec5SDimitry Andric                             const Loop *CurrentLoop,
7310b57cec5SDimitry Andric                             unsigned Level,
7320b57cec5SDimitry Andric                             FullDependence &Result,
7330b57cec5SDimitry Andric                             Constraint &NewConstraint) const;
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric     /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
7360b57cec5SDimitry Andric     /// (Src and Dst) for dependence.
7370b57cec5SDimitry Andric     /// Things of the form [c1 + a*i] and [c2],
7380b57cec5SDimitry Andric     /// where i is an induction variable, c1 and c2 are loop invariant,
7390b57cec5SDimitry Andric     /// and a is a constant. See also weakZeroSrcSIVtest.
7400b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
7410b57cec5SDimitry Andric     /// If there might be a dependence, returns false.
7420b57cec5SDimitry Andric     /// Sets appropriate direction entry.
7430b57cec5SDimitry Andric     /// Set consistent to false.
7440b57cec5SDimitry Andric     /// If loop peeling will break the dependence, mark appropriately.
7450b57cec5SDimitry Andric     bool weakZeroDstSIVtest(const SCEV *SrcCoeff,
7460b57cec5SDimitry Andric                             const SCEV *SrcConst,
7470b57cec5SDimitry Andric                             const SCEV *DstConst,
7480b57cec5SDimitry Andric                             const Loop *CurrentLoop,
7490b57cec5SDimitry Andric                             unsigned Level,
7500b57cec5SDimitry Andric                             FullDependence &Result,
7510b57cec5SDimitry Andric                             Constraint &NewConstraint) const;
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric     /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
7540b57cec5SDimitry Andric     /// Things of the form [c1 + a*i] and [c2 + b*j],
7550b57cec5SDimitry Andric     /// where i and j are induction variable, c1 and c2 are loop invariant,
7560b57cec5SDimitry Andric     /// and a and b are constants.
7570b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
7580b57cec5SDimitry Andric     /// Marks the result as inconsistent.
7590b57cec5SDimitry Andric     /// Works in some cases that symbolicRDIVtest doesn't,
7600b57cec5SDimitry Andric     /// and vice versa.
7610b57cec5SDimitry Andric     bool exactRDIVtest(const SCEV *SrcCoeff,
7620b57cec5SDimitry Andric                        const SCEV *DstCoeff,
7630b57cec5SDimitry Andric                        const SCEV *SrcConst,
7640b57cec5SDimitry Andric                        const SCEV *DstConst,
7650b57cec5SDimitry Andric                        const Loop *SrcLoop,
7660b57cec5SDimitry Andric                        const Loop *DstLoop,
7670b57cec5SDimitry Andric                        FullDependence &Result) const;
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric     /// symbolicRDIVtest - Tests the RDIV subscript pair for dependence.
7700b57cec5SDimitry Andric     /// Things of the form [c1 + a*i] and [c2 + b*j],
7710b57cec5SDimitry Andric     /// where i and j are induction variable, c1 and c2 are loop invariant,
7720b57cec5SDimitry Andric     /// and a and b are constants.
7730b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
7740b57cec5SDimitry Andric     /// Marks the result as inconsistent.
7750b57cec5SDimitry Andric     /// Works in some cases that exactRDIVtest doesn't,
7760b57cec5SDimitry Andric     /// and vice versa. Can also be used as a backup for
7770b57cec5SDimitry Andric     /// ordinary SIV tests.
7780b57cec5SDimitry Andric     bool symbolicRDIVtest(const SCEV *SrcCoeff,
7790b57cec5SDimitry Andric                           const SCEV *DstCoeff,
7800b57cec5SDimitry Andric                           const SCEV *SrcConst,
7810b57cec5SDimitry Andric                           const SCEV *DstConst,
7820b57cec5SDimitry Andric                           const Loop *SrcLoop,
7830b57cec5SDimitry Andric                           const Loop *DstLoop) const;
7840b57cec5SDimitry Andric 
7850b57cec5SDimitry Andric     /// gcdMIVtest - Tests an MIV subscript pair for dependence.
7860b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
7870b57cec5SDimitry Andric     /// Marks the result as inconsistent.
7880b57cec5SDimitry Andric     /// Can sometimes disprove the equal direction for 1 or more loops.
7890b57cec5SDimitry Andric     //  Can handle some symbolics that even the SIV tests don't get,
7900b57cec5SDimitry Andric     /// so we use it as a backup for everything.
7910b57cec5SDimitry Andric     bool gcdMIVtest(const SCEV *Src,
7920b57cec5SDimitry Andric                     const SCEV *Dst,
7930b57cec5SDimitry Andric                     FullDependence &Result) const;
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric     /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
7960b57cec5SDimitry Andric     /// Returns true if any possible dependence is disproved.
7970b57cec5SDimitry Andric     /// Marks the result as inconsistent.
7980b57cec5SDimitry Andric     /// Computes directions.
7990b57cec5SDimitry Andric     bool banerjeeMIVtest(const SCEV *Src,
8000b57cec5SDimitry Andric                          const SCEV *Dst,
8010b57cec5SDimitry Andric                          const SmallBitVector &Loops,
8020b57cec5SDimitry Andric                          FullDependence &Result) const;
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric     /// collectCoefficientInfo - Walks through the subscript,
8050b57cec5SDimitry Andric     /// collecting each coefficient, the associated loop bounds,
8060b57cec5SDimitry Andric     /// and recording its positive and negative parts for later use.
8070b57cec5SDimitry Andric     CoefficientInfo *collectCoeffInfo(const SCEV *Subscript,
8080b57cec5SDimitry Andric                                       bool SrcFlag,
8090b57cec5SDimitry Andric                                       const SCEV *&Constant) const;
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric     /// getPositivePart - X^+ = max(X, 0).
8120b57cec5SDimitry Andric     ///
8130b57cec5SDimitry Andric     const SCEV *getPositivePart(const SCEV *X) const;
8140b57cec5SDimitry Andric 
8150b57cec5SDimitry Andric     /// getNegativePart - X^- = min(X, 0).
8160b57cec5SDimitry Andric     ///
8170b57cec5SDimitry Andric     const SCEV *getNegativePart(const SCEV *X) const;
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric     /// getLowerBound - Looks through all the bounds info and
8200b57cec5SDimitry Andric     /// computes the lower bound given the current direction settings
8210b57cec5SDimitry Andric     /// at each level.
8220b57cec5SDimitry Andric     const SCEV *getLowerBound(BoundInfo *Bound) const;
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric     /// getUpperBound - Looks through all the bounds info and
8250b57cec5SDimitry Andric     /// computes the upper bound given the current direction settings
8260b57cec5SDimitry Andric     /// at each level.
8270b57cec5SDimitry Andric     const SCEV *getUpperBound(BoundInfo *Bound) const;
8280b57cec5SDimitry Andric 
8290b57cec5SDimitry Andric     /// exploreDirections - Hierarchically expands the direction vector
8300b57cec5SDimitry Andric     /// search space, combining the directions of discovered dependences
8310b57cec5SDimitry Andric     /// in the DirSet field of Bound. Returns the number of distinct
8320b57cec5SDimitry Andric     /// dependences discovered. If the dependence is disproved,
8330b57cec5SDimitry Andric     /// it will return 0.
8340b57cec5SDimitry Andric     unsigned exploreDirections(unsigned Level,
8350b57cec5SDimitry Andric                                CoefficientInfo *A,
8360b57cec5SDimitry Andric                                CoefficientInfo *B,
8370b57cec5SDimitry Andric                                BoundInfo *Bound,
8380b57cec5SDimitry Andric                                const SmallBitVector &Loops,
8390b57cec5SDimitry Andric                                unsigned &DepthExpanded,
8400b57cec5SDimitry Andric                                const SCEV *Delta) const;
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric     /// testBounds - Returns true iff the current bounds are plausible.
8430b57cec5SDimitry Andric     bool testBounds(unsigned char DirKind,
8440b57cec5SDimitry Andric                     unsigned Level,
8450b57cec5SDimitry Andric                     BoundInfo *Bound,
8460b57cec5SDimitry Andric                     const SCEV *Delta) const;
8470b57cec5SDimitry Andric 
8480b57cec5SDimitry Andric     /// findBoundsALL - Computes the upper and lower bounds for level K
8490b57cec5SDimitry Andric     /// using the * direction. Records them in Bound.
8500b57cec5SDimitry Andric     void findBoundsALL(CoefficientInfo *A,
8510b57cec5SDimitry Andric                        CoefficientInfo *B,
8520b57cec5SDimitry Andric                        BoundInfo *Bound,
8530b57cec5SDimitry Andric                        unsigned K) const;
8540b57cec5SDimitry Andric 
8550b57cec5SDimitry Andric     /// findBoundsLT - Computes the upper and lower bounds for level K
8560b57cec5SDimitry Andric     /// using the < direction. Records them in Bound.
8570b57cec5SDimitry Andric     void findBoundsLT(CoefficientInfo *A,
8580b57cec5SDimitry Andric                       CoefficientInfo *B,
8590b57cec5SDimitry Andric                       BoundInfo *Bound,
8600b57cec5SDimitry Andric                       unsigned K) const;
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric     /// findBoundsGT - Computes the upper and lower bounds for level K
8630b57cec5SDimitry Andric     /// using the > direction. Records them in Bound.
8640b57cec5SDimitry Andric     void findBoundsGT(CoefficientInfo *A,
8650b57cec5SDimitry Andric                       CoefficientInfo *B,
8660b57cec5SDimitry Andric                       BoundInfo *Bound,
8670b57cec5SDimitry Andric                       unsigned K) const;
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric     /// findBoundsEQ - Computes the upper and lower bounds for level K
8700b57cec5SDimitry Andric     /// using the = direction. Records them in Bound.
8710b57cec5SDimitry Andric     void findBoundsEQ(CoefficientInfo *A,
8720b57cec5SDimitry Andric                       CoefficientInfo *B,
8730b57cec5SDimitry Andric                       BoundInfo *Bound,
8740b57cec5SDimitry Andric                       unsigned K) const;
8750b57cec5SDimitry Andric 
8760b57cec5SDimitry Andric     /// intersectConstraints - Updates X with the intersection
8770b57cec5SDimitry Andric     /// of the Constraints X and Y. Returns true if X has changed.
8780b57cec5SDimitry Andric     bool intersectConstraints(Constraint *X,
8790b57cec5SDimitry Andric                               const Constraint *Y);
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric     /// propagate - Review the constraints, looking for opportunities
8820b57cec5SDimitry Andric     /// to simplify a subscript pair (Src and Dst).
8830b57cec5SDimitry Andric     /// Return true if some simplification occurs.
8840b57cec5SDimitry Andric     /// If the simplification isn't exact (that is, if it is conservative
8850b57cec5SDimitry Andric     /// in terms of dependence), set consistent to false.
8860b57cec5SDimitry Andric     bool propagate(const SCEV *&Src,
8870b57cec5SDimitry Andric                    const SCEV *&Dst,
8880b57cec5SDimitry Andric                    SmallBitVector &Loops,
8890b57cec5SDimitry Andric                    SmallVectorImpl<Constraint> &Constraints,
8900b57cec5SDimitry Andric                    bool &Consistent);
8910b57cec5SDimitry Andric 
8920b57cec5SDimitry Andric     /// propagateDistance - Attempt to propagate a distance
8930b57cec5SDimitry Andric     /// constraint into a subscript pair (Src and Dst).
8940b57cec5SDimitry Andric     /// Return true if some simplification occurs.
8950b57cec5SDimitry Andric     /// If the simplification isn't exact (that is, if it is conservative
8960b57cec5SDimitry Andric     /// in terms of dependence), set consistent to false.
8970b57cec5SDimitry Andric     bool propagateDistance(const SCEV *&Src,
8980b57cec5SDimitry Andric                            const SCEV *&Dst,
8990b57cec5SDimitry Andric                            Constraint &CurConstraint,
9000b57cec5SDimitry Andric                            bool &Consistent);
9010b57cec5SDimitry Andric 
9020b57cec5SDimitry Andric     /// propagatePoint - Attempt to propagate a point
9030b57cec5SDimitry Andric     /// constraint into a subscript pair (Src and Dst).
9040b57cec5SDimitry Andric     /// Return true if some simplification occurs.
9050b57cec5SDimitry Andric     bool propagatePoint(const SCEV *&Src,
9060b57cec5SDimitry Andric                         const SCEV *&Dst,
9070b57cec5SDimitry Andric                         Constraint &CurConstraint);
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric     /// propagateLine - Attempt to propagate a line
9100b57cec5SDimitry Andric     /// constraint into a subscript pair (Src and Dst).
9110b57cec5SDimitry Andric     /// Return true if some simplification occurs.
9120b57cec5SDimitry Andric     /// If the simplification isn't exact (that is, if it is conservative
9130b57cec5SDimitry Andric     /// in terms of dependence), set consistent to false.
9140b57cec5SDimitry Andric     bool propagateLine(const SCEV *&Src,
9150b57cec5SDimitry Andric                        const SCEV *&Dst,
9160b57cec5SDimitry Andric                        Constraint &CurConstraint,
9170b57cec5SDimitry Andric                        bool &Consistent);
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric     /// findCoefficient - Given a linear SCEV,
9200b57cec5SDimitry Andric     /// return the coefficient corresponding to specified loop.
9210b57cec5SDimitry Andric     /// If there isn't one, return the SCEV constant 0.
9220b57cec5SDimitry Andric     /// For example, given a*i + b*j + c*k, returning the coefficient
9230b57cec5SDimitry Andric     /// corresponding to the j loop would yield b.
9240b57cec5SDimitry Andric     const SCEV *findCoefficient(const SCEV *Expr,
9250b57cec5SDimitry Andric                                 const Loop *TargetLoop) const;
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric     /// zeroCoefficient - Given a linear SCEV,
9280b57cec5SDimitry Andric     /// return the SCEV given by zeroing out the coefficient
9290b57cec5SDimitry Andric     /// corresponding to the specified loop.
9300b57cec5SDimitry Andric     /// For example, given a*i + b*j + c*k, zeroing the coefficient
9310b57cec5SDimitry Andric     /// corresponding to the j loop would yield a*i + c*k.
9320b57cec5SDimitry Andric     const SCEV *zeroCoefficient(const SCEV *Expr,
9330b57cec5SDimitry Andric                                 const Loop *TargetLoop) const;
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric     /// addToCoefficient - Given a linear SCEV Expr,
9360b57cec5SDimitry Andric     /// return the SCEV given by adding some Value to the
9370b57cec5SDimitry Andric     /// coefficient corresponding to the specified TargetLoop.
9380b57cec5SDimitry Andric     /// For example, given a*i + b*j + c*k, adding 1 to the coefficient
9390b57cec5SDimitry Andric     /// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
9400b57cec5SDimitry Andric     const SCEV *addToCoefficient(const SCEV *Expr,
9410b57cec5SDimitry Andric                                  const Loop *TargetLoop,
9420b57cec5SDimitry Andric                                  const SCEV *Value)  const;
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric     /// updateDirection - Update direction vector entry
9450b57cec5SDimitry Andric     /// based on the current constraint.
9460b57cec5SDimitry Andric     void updateDirection(Dependence::DVEntry &Level,
9470b57cec5SDimitry Andric                          const Constraint &CurConstraint) const;
9480b57cec5SDimitry Andric 
9495ffd83dbSDimitry Andric     /// Given a linear access function, tries to recover subscripts
9505ffd83dbSDimitry Andric     /// for each dimension of the array element access.
9510b57cec5SDimitry Andric     bool tryDelinearize(Instruction *Src, Instruction *Dst,
9520b57cec5SDimitry Andric                         SmallVectorImpl<Subscript> &Pair);
953480093f4SDimitry Andric 
95481ad6265SDimitry Andric     /// Tries to delinearize \p Src and \p Dst access functions for a fixed size
95581ad6265SDimitry Andric     /// multi-dimensional array. Calls tryDelinearizeFixedSizeImpl() to
95681ad6265SDimitry Andric     /// delinearize \p Src and \p Dst separately,
9575ffd83dbSDimitry Andric     bool tryDelinearizeFixedSize(Instruction *Src, Instruction *Dst,
9585ffd83dbSDimitry Andric                                  const SCEV *SrcAccessFn,
9595ffd83dbSDimitry Andric                                  const SCEV *DstAccessFn,
9605ffd83dbSDimitry Andric                                  SmallVectorImpl<const SCEV *> &SrcSubscripts,
9615ffd83dbSDimitry Andric                                  SmallVectorImpl<const SCEV *> &DstSubscripts);
9625ffd83dbSDimitry Andric 
9635ffd83dbSDimitry Andric     /// Tries to delinearize access function for a multi-dimensional array with
9645ffd83dbSDimitry Andric     /// symbolic runtime sizes.
9655ffd83dbSDimitry Andric     /// Returns true upon success and false otherwise.
9665ffd83dbSDimitry Andric     bool tryDelinearizeParametricSize(
9675ffd83dbSDimitry Andric         Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
9685ffd83dbSDimitry Andric         const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
9695ffd83dbSDimitry Andric         SmallVectorImpl<const SCEV *> &DstSubscripts);
9705ffd83dbSDimitry Andric 
971480093f4SDimitry Andric     /// checkSubscript - Helper function for checkSrcSubscript and
972480093f4SDimitry Andric     /// checkDstSubscript to avoid duplicate code
973480093f4SDimitry Andric     bool checkSubscript(const SCEV *Expr, const Loop *LoopNest,
974480093f4SDimitry Andric                         SmallBitVector &Loops, bool IsSrc);
9750b57cec5SDimitry Andric   }; // class DependenceInfo
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric   /// AnalysisPass to compute dependence information in a function
9780b57cec5SDimitry Andric   class DependenceAnalysis : public AnalysisInfoMixin<DependenceAnalysis> {
9790b57cec5SDimitry Andric   public:
9800b57cec5SDimitry Andric     typedef DependenceInfo Result;
9810b57cec5SDimitry Andric     Result run(Function &F, FunctionAnalysisManager &FAM);
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric   private:
9840b57cec5SDimitry Andric     static AnalysisKey Key;
9850b57cec5SDimitry Andric     friend struct AnalysisInfoMixin<DependenceAnalysis>;
9860b57cec5SDimitry Andric   }; // class DependenceAnalysis
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric   /// Printer pass to dump DA results.
9890b57cec5SDimitry Andric   struct DependenceAnalysisPrinterPass
9900b57cec5SDimitry Andric       : public PassInfoMixin<DependenceAnalysisPrinterPass> {
991bdd1243dSDimitry Andric     DependenceAnalysisPrinterPass(raw_ostream &OS,
992bdd1243dSDimitry Andric                                   bool NormalizeResults = false)
993bdd1243dSDimitry Andric         : OS(OS), NormalizeResults(NormalizeResults) {}
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric     PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
9960b57cec5SDimitry Andric 
9971db9f3b2SDimitry Andric     static bool isRequired() { return true; }
9981db9f3b2SDimitry Andric 
9990b57cec5SDimitry Andric   private:
10000b57cec5SDimitry Andric     raw_ostream &OS;
1001bdd1243dSDimitry Andric     bool NormalizeResults;
10020b57cec5SDimitry Andric   }; // class DependenceAnalysisPrinterPass
10030b57cec5SDimitry Andric 
10040b57cec5SDimitry Andric   /// Legacy pass manager pass to access dependence information
10050b57cec5SDimitry Andric   class DependenceAnalysisWrapperPass : public FunctionPass {
10060b57cec5SDimitry Andric   public:
10070b57cec5SDimitry Andric     static char ID; // Class identification, replacement for typeinfo
1008480093f4SDimitry Andric     DependenceAnalysisWrapperPass();
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric     bool runOnFunction(Function &F) override;
10110b57cec5SDimitry Andric     void releaseMemory() override;
10120b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &) const override;
10130b57cec5SDimitry Andric     void print(raw_ostream &, const Module * = nullptr) const override;
10140b57cec5SDimitry Andric     DependenceInfo &getDI() const;
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   private:
10170b57cec5SDimitry Andric     std::unique_ptr<DependenceInfo> info;
10180b57cec5SDimitry Andric   }; // class DependenceAnalysisWrapperPass
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric   /// createDependenceAnalysisPass - This creates an instance of the
10210b57cec5SDimitry Andric   /// DependenceAnalysis wrapper pass.
10220b57cec5SDimitry Andric   FunctionPass *createDependenceAnalysisWrapperPass();
10230b57cec5SDimitry Andric 
10240b57cec5SDimitry Andric } // namespace llvm
10250b57cec5SDimitry Andric 
10260b57cec5SDimitry Andric #endif
1027