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