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