DependenceAnalysis.h revision 0b8c9a80f20772c3793201ab5b251d3520b9cea3
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com//===-- llvm/Analysis/DependenceAnalysis.h -------------------- -*- C++ -*-===//
2c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon//
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com//                     The LLVM Compiler Infrastructure
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com//
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com// This file is distributed under the University of Illinois Open Source
68fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com// License. See LICENSE.TXT for details.
78fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com//
86d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon//===----------------------------------------------------------------------===//
96d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon//
108fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com// DependenceAnalysis is an LLVM pass that analyses dependences between memory
11bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon// accesses. Currently, it is an implementation of the approach described in
12bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon//
13c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon//            Practical Dependence Testing
1442619d8df206b0bcd36d952909d972b8961e75debsalomon@google.com//            Goff, Kennedy, Tseng
159474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com//            PLDI 1991
16f7b5c1ebfdad1a77d301d1676235e79f8006883ebsalomon@google.com//
1737dd331b20a92ce79cc26556e065dec98a66cb0bbsalomon// There's a single entry point that analyzes the dependence between a pair
1837dd331b20a92ce79cc26556e065dec98a66cb0bbsalomon// of memory references in a function, returning either NULL, for no dependence,
1937dd331b20a92ce79cc26556e065dec98a66cb0bbsalomon// or a more-or-less detailed description of the dependence between them.
208fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com//
2176b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com// This pass exists to support the DependenceGraph pass. There are two separate
2200b76bd750e668a6989dd497313e715d1b476fdcbsalomon// passes because there's a useful separation of concerns. A dependence exists
2300b76bd750e668a6989dd497313e715d1b476fdcbsalomon// if two conditions are met:
2400b76bd750e668a6989dd497313e715d1b476fdcbsalomon//
2500b76bd750e668a6989dd497313e715d1b476fdcbsalomon//    1) Two instructions reference the same memory location, and
2600b76bd750e668a6989dd497313e715d1b476fdcbsalomon//    2) There is a flow of control leading from one instruction to the other.
2700b76bd750e668a6989dd497313e715d1b476fdcbsalomon//
2800b76bd750e668a6989dd497313e715d1b476fdcbsalomon// DependenceAnalysis attacks the first condition; DependenceGraph will attack
2900b76bd750e668a6989dd497313e715d1b476fdcbsalomon// the second (it's not yet ready).
30bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon//
3100b76bd750e668a6989dd497313e715d1b476fdcbsalomon// Please note that this is work in progress and the interface is subject to
32bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon// change.
3300b76bd750e668a6989dd497313e715d1b476fdcbsalomon//
3400b76bd750e668a6989dd497313e715d1b476fdcbsalomon// Plausible changes:
3500b76bd750e668a6989dd497313e715d1b476fdcbsalomon//    Return a set of more precise dependences instead of just one dependence
36bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon//    summarizing all.
37bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon//
38bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon//===----------------------------------------------------------------------===//
39bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
4076b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com#ifndef LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
41bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon#define LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
428fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
4345725db1d82615d43408ec488549aec6218f80e4bsalomon#include "llvm/ADT/SmallBitVector.h"
44c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon#include "llvm/IR/Instructions.h"
4500b76bd750e668a6989dd497313e715d1b476fdcbsalomon#include "llvm/Pass.h"
4600b76bd750e668a6989dd497313e715d1b476fdcbsalomon
4700b76bd750e668a6989dd497313e715d1b476fdcbsalomonnamespace llvm {
4800b76bd750e668a6989dd497313e715d1b476fdcbsalomon  class AliasAnalysis;
4900b76bd750e668a6989dd497313e715d1b476fdcbsalomon  class Loop;
509323b8b8e16df4adcd63ee8496a6382e8df535c9Brian Salomon  class LoopInfo;
51bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon  class ScalarEvolution;
5200b76bd750e668a6989dd497313e715d1b476fdcbsalomon  class SCEV;
5300b76bd750e668a6989dd497313e715d1b476fdcbsalomon  class SCEVConstant;
5400b76bd750e668a6989dd497313e715d1b476fdcbsalomon  class raw_ostream;
5500b76bd750e668a6989dd497313e715d1b476fdcbsalomon
5600b76bd750e668a6989dd497313e715d1b476fdcbsalomon  /// Dependence - This class represents a dependence between two memory
57bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon  /// memory references in a function. It contains minimal information and
5800b76bd750e668a6989dd497313e715d1b476fdcbsalomon  /// is used in the very common situation where the compiler is unable to
5900b76bd750e668a6989dd497313e715d1b476fdcbsalomon  /// determine anything beyond the existence of a dependence; that is, it
60bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon  /// represents a confused dependence (see also FullDependence). In most
61bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon  /// cases (for output, flow, and anti dependences), the dependence implies
6200b76bd750e668a6989dd497313e715d1b476fdcbsalomon  /// an ordering, where the source must precede the destination; in contrast,
63c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon  /// input dependences are unordered.
6400b76bd750e668a6989dd497313e715d1b476fdcbsalomon  class Dependence {
6500b76bd750e668a6989dd497313e715d1b476fdcbsalomon  public:
6600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    Dependence(Instruction *Source,
6700b76bd750e668a6989dd497313e715d1b476fdcbsalomon               Instruction *Destination) :
6800b76bd750e668a6989dd497313e715d1b476fdcbsalomon      Src(Source), Dst(Destination) {}
69c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    virtual ~Dependence() {}
7000b76bd750e668a6989dd497313e715d1b476fdcbsalomon
7100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    /// Dependence::DVEntry - Each level in the distance/direction vector
7200b76bd750e668a6989dd497313e715d1b476fdcbsalomon    /// has a direction (or perhaps a union of several directions), and
731e2530babb65a883a01df5ee87147432f6707ce3bsalomon    /// perhaps a distance.
7400b76bd750e668a6989dd497313e715d1b476fdcbsalomon    struct DVEntry {
758d034a154fec81167ecb696c07da389b98cc02a7bsalomon      enum { NONE = 0,
768d034a154fec81167ecb696c07da389b98cc02a7bsalomon             LT = 1,
778d034a154fec81167ecb696c07da389b98cc02a7bsalomon             EQ = 2,
788d034a154fec81167ecb696c07da389b98cc02a7bsalomon             LE = 3,
796d4488c5e03010c94200b3706631d34ec3201411bsalomon             GT = 4,
806d4488c5e03010c94200b3706631d34ec3201411bsalomon             NE = 5,
8100b76bd750e668a6989dd497313e715d1b476fdcbsalomon             GE = 6,
8200b76bd750e668a6989dd497313e715d1b476fdcbsalomon             ALL = 7 };
8300b76bd750e668a6989dd497313e715d1b476fdcbsalomon      unsigned char Direction : 3; // Init to ALL, then refine.
8400b76bd750e668a6989dd497313e715d1b476fdcbsalomon      bool Scalar    : 1; // Init to true.
8500b76bd750e668a6989dd497313e715d1b476fdcbsalomon      bool PeelFirst : 1; // Peeling the first iteration will break dependence.
8600b76bd750e668a6989dd497313e715d1b476fdcbsalomon      bool PeelLast  : 1; // Peeling the last iteration will break the dependence.
8700b76bd750e668a6989dd497313e715d1b476fdcbsalomon      bool Splitable : 1; // Splitting the loop will break dependence.
8800b76bd750e668a6989dd497313e715d1b476fdcbsalomon      const SCEV *Distance; // NULL implies no distance available.
8900b76bd750e668a6989dd497313e715d1b476fdcbsalomon      DVEntry() : Direction(ALL), Scalar(true), PeelFirst(false),
90bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon                  PeelLast(false), Splitable(false), Distance(NULL) { }
9100b76bd750e668a6989dd497313e715d1b476fdcbsalomon    };
9200b76bd750e668a6989dd497313e715d1b476fdcbsalomon
9300b76bd750e668a6989dd497313e715d1b476fdcbsalomon    /// getSrc - Returns the source instruction for this dependence.
9400b76bd750e668a6989dd497313e715d1b476fdcbsalomon    ///
9500b76bd750e668a6989dd497313e715d1b476fdcbsalomon    Instruction *getSrc() const { return Src; }
9600b76bd750e668a6989dd497313e715d1b476fdcbsalomon
9700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    /// getDst - Returns the destination instruction for this dependence.
9800b76bd750e668a6989dd497313e715d1b476fdcbsalomon    ///
9900b76bd750e668a6989dd497313e715d1b476fdcbsalomon    Instruction *getDst() const { return Dst; }
10000b76bd750e668a6989dd497313e715d1b476fdcbsalomon
101bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    /// isInput - Returns true if this is an input dependence.
10200b76bd750e668a6989dd497313e715d1b476fdcbsalomon    ///
10300b76bd750e668a6989dd497313e715d1b476fdcbsalomon    bool isInput() const;
10400b76bd750e668a6989dd497313e715d1b476fdcbsalomon
105bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    /// isOutput - Returns true if this is an output dependence.
106bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    ///
107bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    bool isOutput() const;
108ac211af359667dd1afcc375e688c3aae46cb13e8bsalomon
109ac211af359667dd1afcc375e688c3aae46cb13e8bsalomon    /// isFlow - Returns true if this is a flow (aka true) dependence.
110bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    ///
111bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    bool isFlow() const;
112bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
113bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    /// isAnti - Returns true if this is an anti dependence.
114bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    ///
115bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    bool isAnti() const;
116bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
11700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    /// isOrdered - Returns true if dependence is Output, Flow, or Anti
11800b76bd750e668a6989dd497313e715d1b476fdcbsalomon    ///
11900b76bd750e668a6989dd497313e715d1b476fdcbsalomon    bool isOrdered() const { return isOutput() || isFlow() || isAnti(); }
12000b76bd750e668a6989dd497313e715d1b476fdcbsalomon
121ac8d6193eaa29e02d1786fe56efa98eefee74e50bsalomon    /// isUnordered - Returns true if dependence is Input
122f96ba02513eadd9fa24d75396ec9f2d6682e464cbsalomon    ///
1231e2530babb65a883a01df5ee87147432f6707ce3bsalomon    bool isUnordered() const { return isInput(); }
124bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon
125bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    /// isLoopIndependent - Returns true if this is a loop-independent
12600b76bd750e668a6989dd497313e715d1b476fdcbsalomon    /// dependence.
12700b76bd750e668a6989dd497313e715d1b476fdcbsalomon    virtual bool isLoopIndependent() const { return true; }
12800b76bd750e668a6989dd497313e715d1b476fdcbsalomon
12900b76bd750e668a6989dd497313e715d1b476fdcbsalomon    /// isConfused - Returns true if this dependence is confused
13000b76bd750e668a6989dd497313e715d1b476fdcbsalomon    /// (the compiler understands nothing and makes worst-case
131544fe2338ff7459dbd887549aca31b8fc4cde7f4bsalomon    /// assumptions).
13200b76bd750e668a6989dd497313e715d1b476fdcbsalomon    virtual bool isConfused() const { return true; }
13300b76bd750e668a6989dd497313e715d1b476fdcbsalomon
134977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com    /// isConsistent - Returns true if this dependence is consistent
1358fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// (occurs every time the source and destination are executed).
136089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    virtual bool isConsistent() const { return false; }
137089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org
1388fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// getLevels - Returns the number of common loops surrounding the
1398fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// source and destination of the dependence.
1408fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    virtual unsigned getLevels() const { return 0; }
1418fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
1428fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// getDirection - Returns the direction associated with a particular
1438fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// level.
1448fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    virtual unsigned getDirection(unsigned Level) const { return DVEntry::ALL; }
1458fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
1468fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// getDistance - Returns the distance (or NULL) associated with a
1478fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// particular level.
148089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    virtual const SCEV *getDistance(unsigned Level) const { return NULL; }
149089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org
150089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    /// isPeelFirst - Returns true if peeling the first iteration from
151089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    /// this loop will break this dependence.
1528fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    virtual bool isPeelFirst(unsigned Level) const { return false; }
1538fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
154089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    /// isPeelLast - Returns true if peeling the last iteration from
1558fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// this loop will break this dependence.
1568fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    virtual bool isPeelLast(unsigned Level) const { return false; }
157089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org
1588fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// isSplitable - Returns true if splitting this loop will break
159cee661af926cc977addc6e039b7022975a448acebsalomon@google.com    /// the dependence.
160089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    virtual bool isSplitable(unsigned Level) const { return false; }
161089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org
162089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    /// isScalar - Returns true if a particular level is scalar; that is,
163089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    /// if no subscript in the source or destination mention the induction
164838f6e18fb13cd295f2c4d1e673cb03458f4e0a8bsalomon@google.com    /// variable associated with the loop at this level.
1651f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    virtual bool isScalar(unsigned Level) const;
1661f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com
1671f47f4f7325971dd53991e2bb02da94fa7c6d962robertphillips@google.com    /// dump - For debugging purposes, dumps a dependence to OS.
168c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    ///
169c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    void dump(raw_ostream &OS) const;
170c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon  private:
171c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    Instruction *Src, *Dst;
172c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    friend class DependenceAnalysis;
173c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon  };
174c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
175c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
176c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon  /// FullDependence - This class represents a dependence between two memory
1776d4488c5e03010c94200b3706631d34ec3201411bsalomon  /// references in a function. It contains detailed information about the
1786d4488c5e03010c94200b3706631d34ec3201411bsalomon  /// dependence (direction vectors, etc) and is used when the compiler is
1796d4488c5e03010c94200b3706631d34ec3201411bsalomon  /// able to accurately analyze the interaction of the references; that is,
180bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon  /// it is not a confused dependence (see Dependence). In most cases
1811e2530babb65a883a01df5ee87147432f6707ce3bsalomon  /// (for output, flow, and anti dependences), the dependence implies an
182744998e666073166307d2522847b2536000a7619bsalomon  /// ordering, where the source must precede the destination; in contrast,
183744998e666073166307d2522847b2536000a7619bsalomon  /// input dependences are unordered.
184744998e666073166307d2522847b2536000a7619bsalomon  class FullDependence : public Dependence {
1856d4488c5e03010c94200b3706631d34ec3201411bsalomon  public:
1866d4488c5e03010c94200b3706631d34ec3201411bsalomon    FullDependence(Instruction *Src,
187744998e666073166307d2522847b2536000a7619bsalomon                   Instruction *Dst,
188744998e666073166307d2522847b2536000a7619bsalomon                   bool LoopIndependent,
1891e2530babb65a883a01df5ee87147432f6707ce3bsalomon                   unsigned Levels);
1901e2530babb65a883a01df5ee87147432f6707ce3bsalomon    ~FullDependence() {
1911e2530babb65a883a01df5ee87147432f6707ce3bsalomon      delete[] DV;
1921e2530babb65a883a01df5ee87147432f6707ce3bsalomon    }
1931e2530babb65a883a01df5ee87147432f6707ce3bsalomon
1941e2530babb65a883a01df5ee87147432f6707ce3bsalomon    /// isLoopIndependent - Returns true if this is a loop-independent
195728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    /// dependence.
19652e9d63f7110ac691609660342cdab32082a4235bsalomon    bool isLoopIndependent() const { return LoopIndependent; }
19752e9d63f7110ac691609660342cdab32082a4235bsalomon
198c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// isConfused - Returns true if this dependence is confused
199728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    /// (the compiler understands nothing and makes worst-case
200c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// assumptions).
201c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    bool isConfused() const { return false; }
202c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
203169612621f00b3fe9f71014079991287d311751absalomon    /// isConsistent - Returns true if this dependence is consistent
204169612621f00b3fe9f71014079991287d311751absalomon    /// (occurs every time the source and destination are executed).
205169612621f00b3fe9f71014079991287d311751absalomon    bool isConsistent() const { return Consistent; }
206169612621f00b3fe9f71014079991287d311751absalomon
2076d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    /// getLevels - Returns the number of common loops surrounding the
2086d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    /// source and destination of the dependence.
20976b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com    unsigned getLevels() const { return Levels; }
21049f085dddff10473b6ebf832a974288300224e60bsalomon
211c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// getDirection - Returns the direction associated with a particular
21276b7fcc79ee47db6ebea4f27e0070c467684418absalomon@google.com    /// level.
2138fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    unsigned getDirection(unsigned Level) const;
214d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com
215d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    /// getDistance - Returns the distance (or NULL) associated with a
216d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    /// particular level.
217d364554bcfd391c3b6111af8bff963a35ab87ba7robertphillips@google.com    const SCEV *getDistance(unsigned Level) const;
2188fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2199ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com    /// isPeelFirst - Returns true if peeling the first iteration from
220a292112154f803feb9f5cc002bbfab559f7cb633bsalomon@google.com    /// this loop will break this dependence.
221c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    bool isPeelFirst(unsigned Level) const;
222c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
223c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// isPeelLast - Returns true if peeling the last iteration from
224c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// this loop will break this dependence.
225c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    bool isPeelLast(unsigned Level) const;
226c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
227c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// isSplitable - Returns true if splitting the loop will break
228c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// the dependence.
229744998e666073166307d2522847b2536000a7619bsalomon    bool isSplitable(unsigned Level) const;
230744998e666073166307d2522847b2536000a7619bsalomon
231744998e666073166307d2522847b2536000a7619bsalomon    /// isScalar - Returns true if a particular level is scalar; that is,
232744998e666073166307d2522847b2536000a7619bsalomon    /// if no subscript in the source or destination mention the induction
233744998e666073166307d2522847b2536000a7619bsalomon    /// variable associated with the loop at this level.
234744998e666073166307d2522847b2536000a7619bsalomon    bool isScalar(unsigned Level) const;
2358fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com  private:
236bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    unsigned short Levels;
237bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    bool LoopIndependent;
238515dcd36032997ce335daa0163c6d67e851bcad1commit-bot@chromium.org    bool Consistent; // Init to true, then refine.
2399474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com    DVEntry *DV;
2409474ed06176fe24c77091b0d75f35442e851073frobertphillips@google.com    friend class DependenceAnalysis;
2418fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com  };
242c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
243c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon
244c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon  /// DependenceAnalysis - This class is the main dependence-analysis driver.
2456d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon  ///
2468fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com  class DependenceAnalysis : public FunctionPass {
247c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    void operator=(const DependenceAnalysis &);     // do not implement
248c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    DependenceAnalysis(const DependenceAnalysis &); // do not implement
249c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon  public:
250c8dc1f74b6cdda9a43a638292a608c59c1d72d80bsalomon    /// depends - Tests for a dependence between the Src and Dst instructions.
251728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    /// Returns NULL if no dependence; otherwise, returns a Dependence (or a
2529ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com    /// FullDependence) with as much information as can be gleaned.
253089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    /// The flag PossiblyLoopIndependent should be set by the caller
254b77f0f4ae560e97cc4cd2758752d955549017c3cskia.committer@gmail.com    /// if it appears that control flow can reach from Src to Dst
2559ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com    /// without traversing a loop back edge.
2569ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com    Dependence *depends(Instruction *Src,
2579ef0426e7c126f6ad6ba833d4543b92a197c95afrobertphillips@google.com                        Instruction *Dst,
258728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com                        bool PossiblyLoopIndependent);
259728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com
260c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// getSplitIteration - Give a dependence that's splitable at some
261c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// particular level, return the iteration that should be used to split
262c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// the loop.
263c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    ///
264c44be0e9e4cee5402909c06370a630eee188a8f3bsalomon    /// Generally, the dependence analyzer will be used to build
2656d4488c5e03010c94200b3706631d34ec3201411bsalomon    /// a dependence graph for a function (basically a map from instructions
2666d4488c5e03010c94200b3706631d34ec3201411bsalomon    /// to dependences). Looking for cycles in the graph shows us loops
267744998e666073166307d2522847b2536000a7619bsalomon    /// that cannot be trivially vectorized/parallelized.
2686d4488c5e03010c94200b3706631d34ec3201411bsalomon    ///
2696d4488c5e03010c94200b3706631d34ec3201411bsalomon    /// We can try to improve the situation by examining all the dependences
270744998e666073166307d2522847b2536000a7619bsalomon    /// that make up the cycle, looking for ones we can break.
271bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    /// Sometimes, peeling the first or last iteration of a loop will break
272bcf0a52d4f4221b158e68a06ba0c4cc4db011060bsalomon    /// dependences, and there are flags for those possibilities.
2738fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// Sometimes, splitting a loop at some other iteration will do the trick,
2748fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// and we've got a flag for that case. Rather than waste the space to
2758fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /// record the exact iteration (since we rarely know), we provide
276    /// a method that calculates the iteration. It's a drag that it must work
277    /// from scratch, but wonderful in that it's possible.
278    ///
279    /// Here's an example:
280    ///
281    ///    for (i = 0; i < 10; i++)
282    ///        A[i] = ...
283    ///        ... = A[11 - i]
284    ///
285    /// There's a loop-carried flow dependence from the store to the load,
286    /// found by the weak-crossing SIV test. The dependence will have a flag,
287    /// indicating that the dependence can be broken by splitting the loop.
288    /// Calling getSplitIteration will return 5.
289    /// Splitting the loop breaks the dependence, like so:
290    ///
291    ///    for (i = 0; i <= 5; i++)
292    ///        A[i] = ...
293    ///        ... = A[11 - i]
294    ///    for (i = 6; i < 10; i++)
295    ///        A[i] = ...
296    ///        ... = A[11 - i]
297    ///
298    /// breaks the dependence and allows us to vectorize/parallelize
299    /// both loops.
300    const SCEV *getSplitIteration(const Dependence *Dep, unsigned Level);
301
302  private:
303    AliasAnalysis *AA;
304    ScalarEvolution *SE;
305    LoopInfo *LI;
306    Function *F;
307
308    /// Subscript - This private struct represents a pair of subscripts from
309    /// a pair of potentially multi-dimensional array references. We use a
310    /// vector of them to guide subscript partitioning.
311    struct Subscript {
312      const SCEV *Src;
313      const SCEV *Dst;
314      enum ClassificationKind { ZIV, SIV, RDIV, MIV, NonLinear } Classification;
315      SmallBitVector Loops;
316      SmallBitVector GroupLoops;
317      SmallBitVector Group;
318    };
319
320    struct CoefficientInfo {
321      const SCEV *Coeff;
322      const SCEV *PosPart;
323      const SCEV *NegPart;
324      const SCEV *Iterations;
325    };
326
327    struct BoundInfo {
328      const SCEV *Iterations;
329      const SCEV *Upper[8];
330      const SCEV *Lower[8];
331      unsigned char Direction;
332      unsigned char DirSet;
333    };
334
335    /// Constraint - This private class represents a constraint, as defined
336    /// in the paper
337    ///
338    ///           Practical Dependence Testing
339    ///           Goff, Kennedy, Tseng
340    ///           PLDI 1991
341    ///
342    /// There are 5 kinds of constraint, in a hierarchy.
343    ///   1) Any - indicates no constraint, any dependence is possible.
344    ///   2) Line - A line ax + by = c, where a, b, and c are parameters,
345    ///             representing the dependence equation.
346    ///   3) Distance - The value d of the dependence distance;
347    ///   4) Point - A point <x, y> representing the dependence from
348    ///              iteration x to iteration y.
349    ///   5) Empty - No dependence is possible.
350    class Constraint {
351    private:
352      enum ConstraintKind { Empty, Point, Distance, Line, Any } Kind;
353      ScalarEvolution *SE;
354      const SCEV *A;
355      const SCEV *B;
356      const SCEV *C;
357      const Loop *AssociatedLoop;
358    public:
359      /// isEmpty - Return true if the constraint is of kind Empty.
360      bool isEmpty() const { return Kind == Empty; }
361
362      /// isPoint - Return true if the constraint is of kind Point.
363      bool isPoint() const { return Kind == Point; }
364
365      /// isDistance - Return true if the constraint is of kind Distance.
366      bool isDistance() const { return Kind == Distance; }
367
368      /// isLine - Return true if the constraint is of kind Line.
369      /// Since Distance's can also be represented as Lines, we also return
370      /// true if the constraint is of kind Distance.
371      bool isLine() const { return Kind == Line || Kind == Distance; }
372
373      /// isAny - Return true if the constraint is of kind Any;
374      bool isAny() const { return Kind == Any; }
375
376      /// getX - If constraint is a point <X, Y>, returns X.
377      /// Otherwise assert.
378      const SCEV *getX() const;
379
380      /// getY - If constraint is a point <X, Y>, returns Y.
381      /// Otherwise assert.
382      const SCEV *getY() const;
383
384      /// getA - If constraint is a line AX + BY = C, returns A.
385      /// Otherwise assert.
386      const SCEV *getA() const;
387
388      /// getB - If constraint is a line AX + BY = C, returns B.
389      /// Otherwise assert.
390      const SCEV *getB() const;
391
392      /// getC - If constraint is a line AX + BY = C, returns C.
393      /// Otherwise assert.
394      const SCEV *getC() const;
395
396      /// getD - If constraint is a distance, returns D.
397      /// Otherwise assert.
398      const SCEV *getD() const;
399
400      /// getAssociatedLoop - Returns the loop associated with this constraint.
401      const Loop *getAssociatedLoop() const;
402
403      /// setPoint - Change a constraint to Point.
404      void setPoint(const SCEV *X, const SCEV *Y, const Loop *CurrentLoop);
405
406      /// setLine - Change a constraint to Line.
407      void setLine(const SCEV *A, const SCEV *B,
408                   const SCEV *C, const Loop *CurrentLoop);
409
410      /// setDistance - Change a constraint to Distance.
411      void setDistance(const SCEV *D, const Loop *CurrentLoop);
412
413      /// setEmpty - Change a constraint to Empty.
414      void setEmpty();
415
416      /// setAny - Change a constraint to Any.
417      void setAny(ScalarEvolution *SE);
418
419      /// dump - For debugging purposes. Dumps the constraint
420      /// out to OS.
421      void dump(raw_ostream &OS) const;
422    };
423
424
425    /// establishNestingLevels - Examines the loop nesting of the Src and Dst
426    /// instructions and establishes their shared loops. Sets the variables
427    /// CommonLevels, SrcLevels, and MaxLevels.
428    /// The source and destination instructions needn't be contained in the same
429    /// loop. The routine establishNestingLevels finds the level of most deeply
430    /// nested loop that contains them both, CommonLevels. An instruction that's
431    /// not contained in a loop is at level = 0. MaxLevels is equal to the level
432    /// of the source plus the level of the destination, minus CommonLevels.
433    /// This lets us allocate vectors MaxLevels in length, with room for every
434    /// distinct loop referenced in both the source and destination subscripts.
435    /// The variable SrcLevels is the nesting depth of the source instruction.
436    /// It's used to help calculate distinct loops referenced by the destination.
437    /// Here's the map from loops to levels:
438    ///            0 - unused
439    ///            1 - outermost common loop
440    ///          ... - other common loops
441    /// CommonLevels - innermost common loop
442    ///          ... - loops containing Src but not Dst
443    ///    SrcLevels - innermost loop containing Src but not Dst
444    ///          ... - loops containing Dst but not Src
445    ///    MaxLevels - innermost loop containing Dst but not Src
446    /// Consider the follow code fragment:
447    ///    for (a = ...) {
448    ///      for (b = ...) {
449    ///        for (c = ...) {
450    ///          for (d = ...) {
451    ///            A[] = ...;
452    ///          }
453    ///        }
454    ///        for (e = ...) {
455    ///          for (f = ...) {
456    ///            for (g = ...) {
457    ///              ... = A[];
458    ///            }
459    ///          }
460    ///        }
461    ///      }
462    ///    }
463    /// If we're looking at the possibility of a dependence between the store
464    /// to A (the Src) and the load from A (the Dst), we'll note that they
465    /// have 2 loops in common, so CommonLevels will equal 2 and the direction
466    /// vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
467    /// A map from loop names to level indices would look like
468    ///     a - 1
469    ///     b - 2 = CommonLevels
470    ///     c - 3
471    ///     d - 4 = SrcLevels
472    ///     e - 5
473    ///     f - 6
474    ///     g - 7 = MaxLevels
475    void establishNestingLevels(const Instruction *Src,
476                                const Instruction *Dst);
477
478    unsigned CommonLevels, SrcLevels, MaxLevels;
479
480    /// mapSrcLoop - Given one of the loops containing the source, return
481    /// its level index in our numbering scheme.
482    unsigned mapSrcLoop(const Loop *SrcLoop) const;
483
484    /// mapDstLoop - Given one of the loops containing the destination,
485    /// return its level index in our numbering scheme.
486    unsigned mapDstLoop(const Loop *DstLoop) const;
487
488    /// isLoopInvariant - Returns true if Expression is loop invariant
489    /// in LoopNest.
490    bool isLoopInvariant(const SCEV *Expression, const Loop *LoopNest) const;
491
492    /// removeMatchingExtensions - Examines a subscript pair.
493    /// If the source and destination are identically sign (or zero)
494    /// extended, it strips off the extension in an effort to
495    /// simplify the actual analysis.
496    void removeMatchingExtensions(Subscript *Pair);
497
498    /// collectCommonLoops - Finds the set of loops from the LoopNest that
499    /// have a level <= CommonLevels and are referred to by the SCEV Expression.
500    void collectCommonLoops(const SCEV *Expression,
501                            const Loop *LoopNest,
502                            SmallBitVector &Loops) const;
503
504    /// checkSrcSubscript - Examines the SCEV Src, returning true iff it's
505    /// linear. Collect the set of loops mentioned by Src.
506    bool checkSrcSubscript(const SCEV *Src,
507                           const Loop *LoopNest,
508                           SmallBitVector &Loops);
509
510    /// checkDstSubscript - Examines the SCEV Dst, returning true iff it's
511    /// linear. Collect the set of loops mentioned by Dst.
512    bool checkDstSubscript(const SCEV *Dst,
513                           const Loop *LoopNest,
514                           SmallBitVector &Loops);
515
516    /// isKnownPredicate - Compare X and Y using the predicate Pred.
517    /// Basically a wrapper for SCEV::isKnownPredicate,
518    /// but tries harder, especially in the presence of sign and zero
519    /// extensions and symbolics.
520    bool isKnownPredicate(ICmpInst::Predicate Pred,
521                          const SCEV *X,
522                          const SCEV *Y) const;
523
524    /// collectUpperBound - All subscripts are the same type (on my machine,
525    /// an i64). The loop bound may be a smaller type. collectUpperBound
526    /// find the bound, if available, and zero extends it to the Type T.
527    /// (I zero extend since the bound should always be >= 0.)
528    /// If no upper bound is available, return NULL.
529    const SCEV *collectUpperBound(const Loop *l, Type *T) const;
530
531    /// collectConstantUpperBound - Calls collectUpperBound(), then
532    /// attempts to cast it to SCEVConstant. If the cast fails,
533    /// returns NULL.
534    const SCEVConstant *collectConstantUpperBound(const Loop *l, Type *T) const;
535
536    /// classifyPair - Examines the subscript pair (the Src and Dst SCEVs)
537    /// and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
538    /// Collects the associated loops in a set.
539    Subscript::ClassificationKind classifyPair(const SCEV *Src,
540                                           const Loop *SrcLoopNest,
541                                           const SCEV *Dst,
542                                           const Loop *DstLoopNest,
543                                           SmallBitVector &Loops);
544
545    /// testZIV - Tests the ZIV subscript pair (Src and Dst) for dependence.
546    /// Returns true if any possible dependence is disproved.
547    /// If there might be a dependence, returns false.
548    /// If the dependence isn't proven to exist,
549    /// marks the Result as inconsistent.
550    bool testZIV(const SCEV *Src,
551                 const SCEV *Dst,
552                 FullDependence &Result) const;
553
554    /// testSIV - Tests the SIV subscript pair (Src and Dst) for dependence.
555    /// Things of the form [c1 + a1*i] and [c2 + a2*j], where
556    /// i and j are induction variables, c1 and c2 are loop invariant,
557    /// and a1 and a2 are constant.
558    /// Returns true if any possible dependence is disproved.
559    /// If there might be a dependence, returns false.
560    /// Sets appropriate direction vector entry and, when possible,
561    /// the distance vector entry.
562    /// If the dependence isn't proven to exist,
563    /// marks the Result as inconsistent.
564    bool testSIV(const SCEV *Src,
565                 const SCEV *Dst,
566                 unsigned &Level,
567                 FullDependence &Result,
568                 Constraint &NewConstraint,
569                 const SCEV *&SplitIter) const;
570
571    /// testRDIV - Tests the RDIV subscript pair (Src and Dst) for dependence.
572    /// Things of the form [c1 + a1*i] and [c2 + a2*j]
573    /// where i and j are induction variables, c1 and c2 are loop invariant,
574    /// and a1 and a2 are constant.
575    /// With minor algebra, this test can also be used for things like
576    /// [c1 + a1*i + a2*j][c2].
577    /// Returns true if any possible dependence is disproved.
578    /// If there might be a dependence, returns false.
579    /// Marks the Result as inconsistent.
580    bool testRDIV(const SCEV *Src,
581                  const SCEV *Dst,
582                  FullDependence &Result) const;
583
584    /// testMIV - Tests the MIV subscript pair (Src and Dst) for dependence.
585    /// Returns true if dependence disproved.
586    /// Can sometimes refine direction vectors.
587    bool testMIV(const SCEV *Src,
588                 const SCEV *Dst,
589                 const SmallBitVector &Loops,
590                 FullDependence &Result) const;
591
592    /// strongSIVtest - Tests the strong SIV subscript pair (Src and Dst)
593    /// for dependence.
594    /// Things of the form [c1 + a*i] and [c2 + a*i],
595    /// where i is an induction variable, c1 and c2 are loop invariant,
596    /// and a is a constant
597    /// Returns true if any possible dependence is disproved.
598    /// If there might be a dependence, returns false.
599    /// Sets appropriate direction and distance.
600    bool strongSIVtest(const SCEV *Coeff,
601                       const SCEV *SrcConst,
602                       const SCEV *DstConst,
603                       const Loop *CurrentLoop,
604                       unsigned Level,
605                       FullDependence &Result,
606                       Constraint &NewConstraint) const;
607
608    /// weakCrossingSIVtest - Tests the weak-crossing SIV subscript pair
609    /// (Src and Dst) for dependence.
610    /// Things of the form [c1 + a*i] and [c2 - a*i],
611    /// where i is an induction variable, c1 and c2 are loop invariant,
612    /// and a is a constant.
613    /// Returns true if any possible dependence is disproved.
614    /// If there might be a dependence, returns false.
615    /// Sets appropriate direction entry.
616    /// Set consistent to false.
617    /// Marks the dependence as splitable.
618    bool weakCrossingSIVtest(const SCEV *SrcCoeff,
619                             const SCEV *SrcConst,
620                             const SCEV *DstConst,
621                             const Loop *CurrentLoop,
622                             unsigned Level,
623                             FullDependence &Result,
624                             Constraint &NewConstraint,
625                             const SCEV *&SplitIter) const;
626
627    /// ExactSIVtest - Tests the SIV subscript pair
628    /// (Src and Dst) for dependence.
629    /// Things of the form [c1 + a1*i] and [c2 + a2*i],
630    /// where i is an induction variable, c1 and c2 are loop invariant,
631    /// and a1 and a2 are constant.
632    /// Returns true if any possible dependence is disproved.
633    /// If there might be a dependence, returns false.
634    /// Sets appropriate direction entry.
635    /// Set consistent to false.
636    bool exactSIVtest(const SCEV *SrcCoeff,
637                      const SCEV *DstCoeff,
638                      const SCEV *SrcConst,
639                      const SCEV *DstConst,
640                      const Loop *CurrentLoop,
641                      unsigned Level,
642                      FullDependence &Result,
643                      Constraint &NewConstraint) const;
644
645    /// weakZeroSrcSIVtest - Tests the weak-zero SIV subscript pair
646    /// (Src and Dst) for dependence.
647    /// Things of the form [c1] and [c2 + a*i],
648    /// where i is an induction variable, c1 and c2 are loop invariant,
649    /// and a is a constant. See also weakZeroDstSIVtest.
650    /// Returns true if any possible dependence is disproved.
651    /// If there might be a dependence, returns false.
652    /// Sets appropriate direction entry.
653    /// Set consistent to false.
654    /// If loop peeling will break the dependence, mark appropriately.
655    bool weakZeroSrcSIVtest(const SCEV *DstCoeff,
656                            const SCEV *SrcConst,
657                            const SCEV *DstConst,
658                            const Loop *CurrentLoop,
659                            unsigned Level,
660                            FullDependence &Result,
661                            Constraint &NewConstraint) const;
662
663    /// weakZeroDstSIVtest - Tests the weak-zero SIV subscript pair
664    /// (Src and Dst) for dependence.
665    /// Things of the form [c1 + a*i] and [c2],
666    /// where i is an induction variable, c1 and c2 are loop invariant,
667    /// and a is a constant. See also weakZeroSrcSIVtest.
668    /// Returns true if any possible dependence is disproved.
669    /// If there might be a dependence, returns false.
670    /// Sets appropriate direction entry.
671    /// Set consistent to false.
672    /// If loop peeling will break the dependence, mark appropriately.
673    bool weakZeroDstSIVtest(const SCEV *SrcCoeff,
674                            const SCEV *SrcConst,
675                            const SCEV *DstConst,
676                            const Loop *CurrentLoop,
677                            unsigned Level,
678                            FullDependence &Result,
679                            Constraint &NewConstraint) const;
680
681    /// exactRDIVtest - Tests the RDIV subscript pair for dependence.
682    /// Things of the form [c1 + a*i] and [c2 + b*j],
683    /// where i and j are induction variable, c1 and c2 are loop invariant,
684    /// and a and b are constants.
685    /// Returns true if any possible dependence is disproved.
686    /// Marks the result as inconsistent.
687    /// Works in some cases that symbolicRDIVtest doesn't,
688    /// and vice versa.
689    bool exactRDIVtest(const SCEV *SrcCoeff,
690                       const SCEV *DstCoeff,
691                       const SCEV *SrcConst,
692                       const SCEV *DstConst,
693                       const Loop *SrcLoop,
694                       const Loop *DstLoop,
695                       FullDependence &Result) const;
696
697    /// symbolicRDIVtest - Tests the RDIV subscript pair for dependence.
698    /// Things of the form [c1 + a*i] and [c2 + b*j],
699    /// where i and j are induction variable, c1 and c2 are loop invariant,
700    /// and a and b are constants.
701    /// Returns true if any possible dependence is disproved.
702    /// Marks the result as inconsistent.
703    /// Works in some cases that exactRDIVtest doesn't,
704    /// and vice versa. Can also be used as a backup for
705    /// ordinary SIV tests.
706    bool symbolicRDIVtest(const SCEV *SrcCoeff,
707                          const SCEV *DstCoeff,
708                          const SCEV *SrcConst,
709                          const SCEV *DstConst,
710                          const Loop *SrcLoop,
711                          const Loop *DstLoop) const;
712
713    /// gcdMIVtest - Tests an MIV subscript pair for dependence.
714    /// Returns true if any possible dependence is disproved.
715    /// Marks the result as inconsistent.
716    /// Can sometimes disprove the equal direction for 1 or more loops.
717    //  Can handle some symbolics that even the SIV tests don't get,
718    /// so we use it as a backup for everything.
719    bool gcdMIVtest(const SCEV *Src,
720                    const SCEV *Dst,
721                    FullDependence &Result) const;
722
723    /// banerjeeMIVtest - Tests an MIV subscript pair for dependence.
724    /// Returns true if any possible dependence is disproved.
725    /// Marks the result as inconsistent.
726    /// Computes directions.
727    bool banerjeeMIVtest(const SCEV *Src,
728                         const SCEV *Dst,
729                         const SmallBitVector &Loops,
730                         FullDependence &Result) const;
731
732    /// collectCoefficientInfo - Walks through the subscript,
733    /// collecting each coefficient, the associated loop bounds,
734    /// and recording its positive and negative parts for later use.
735    CoefficientInfo *collectCoeffInfo(const SCEV *Subscript,
736                                      bool SrcFlag,
737                                      const SCEV *&Constant) const;
738
739    /// getPositivePart - X^+ = max(X, 0).
740    ///
741    const SCEV *getPositivePart(const SCEV *X) const;
742
743    /// getNegativePart - X^- = min(X, 0).
744    ///
745    const SCEV *getNegativePart(const SCEV *X) const;
746
747    /// getLowerBound - Looks through all the bounds info and
748    /// computes the lower bound given the current direction settings
749    /// at each level.
750    const SCEV *getLowerBound(BoundInfo *Bound) const;
751
752    /// getUpperBound - Looks through all the bounds info and
753    /// computes the upper bound given the current direction settings
754    /// at each level.
755    const SCEV *getUpperBound(BoundInfo *Bound) const;
756
757    /// exploreDirections - Hierarchically expands the direction vector
758    /// search space, combining the directions of discovered dependences
759    /// in the DirSet field of Bound. Returns the number of distinct
760    /// dependences discovered. If the dependence is disproved,
761    /// it will return 0.
762    unsigned exploreDirections(unsigned Level,
763                               CoefficientInfo *A,
764                               CoefficientInfo *B,
765                               BoundInfo *Bound,
766                               const SmallBitVector &Loops,
767                               unsigned &DepthExpanded,
768                               const SCEV *Delta) const;
769
770    /// testBounds - Returns true iff the current bounds are plausible.
771    ///
772    bool testBounds(unsigned char DirKind,
773                    unsigned Level,
774                    BoundInfo *Bound,
775                    const SCEV *Delta) const;
776
777    /// findBoundsALL - Computes the upper and lower bounds for level K
778    /// using the * direction. Records them in Bound.
779    void findBoundsALL(CoefficientInfo *A,
780                       CoefficientInfo *B,
781                       BoundInfo *Bound,
782                       unsigned K) const;
783
784    /// findBoundsLT - Computes the upper and lower bounds for level K
785    /// using the < direction. Records them in Bound.
786    void findBoundsLT(CoefficientInfo *A,
787                      CoefficientInfo *B,
788                      BoundInfo *Bound,
789                      unsigned K) const;
790
791    /// findBoundsGT - Computes the upper and lower bounds for level K
792    /// using the > direction. Records them in Bound.
793    void findBoundsGT(CoefficientInfo *A,
794                      CoefficientInfo *B,
795                      BoundInfo *Bound,
796                      unsigned K) const;
797
798    /// findBoundsEQ - Computes the upper and lower bounds for level K
799    /// using the = direction. Records them in Bound.
800    void findBoundsEQ(CoefficientInfo *A,
801                      CoefficientInfo *B,
802                      BoundInfo *Bound,
803                      unsigned K) const;
804
805    /// intersectConstraints - Updates X with the intersection
806    /// of the Constraints X and Y. Returns true if X has changed.
807    bool intersectConstraints(Constraint *X,
808                              const Constraint *Y);
809
810    /// propagate - Review the constraints, looking for opportunities
811    /// to simplify a subscript pair (Src and Dst).
812    /// Return true if some simplification occurs.
813    /// If the simplification isn't exact (that is, if it is conservative
814    /// in terms of dependence), set consistent to false.
815    bool propagate(const SCEV *&Src,
816                   const SCEV *&Dst,
817                   SmallBitVector &Loops,
818                   SmallVector<Constraint, 4> &Constraints,
819                   bool &Consistent);
820
821    /// propagateDistance - Attempt to propagate a distance
822    /// constraint into a subscript pair (Src and Dst).
823    /// Return true if some simplification occurs.
824    /// If the simplification isn't exact (that is, if it is conservative
825    /// in terms of dependence), set consistent to false.
826    bool propagateDistance(const SCEV *&Src,
827                           const SCEV *&Dst,
828                           Constraint &CurConstraint,
829                           bool &Consistent);
830
831    /// propagatePoint - Attempt to propagate a point
832    /// constraint into a subscript pair (Src and Dst).
833    /// Return true if some simplification occurs.
834    bool propagatePoint(const SCEV *&Src,
835                        const SCEV *&Dst,
836                        Constraint &CurConstraint);
837
838    /// propagateLine - Attempt to propagate a line
839    /// constraint into a subscript pair (Src and Dst).
840    /// Return true if some simplification occurs.
841    /// If the simplification isn't exact (that is, if it is conservative
842    /// in terms of dependence), set consistent to false.
843    bool propagateLine(const SCEV *&Src,
844                       const SCEV *&Dst,
845                       Constraint &CurConstraint,
846                       bool &Consistent);
847
848    /// findCoefficient - Given a linear SCEV,
849    /// return the coefficient corresponding to specified loop.
850    /// If there isn't one, return the SCEV constant 0.
851    /// For example, given a*i + b*j + c*k, returning the coefficient
852    /// corresponding to the j loop would yield b.
853    const SCEV *findCoefficient(const SCEV *Expr,
854                                const Loop *TargetLoop) const;
855
856    /// zeroCoefficient - Given a linear SCEV,
857    /// return the SCEV given by zeroing out the coefficient
858    /// corresponding to the specified loop.
859    /// For example, given a*i + b*j + c*k, zeroing the coefficient
860    /// corresponding to the j loop would yield a*i + c*k.
861    const SCEV *zeroCoefficient(const SCEV *Expr,
862                                const Loop *TargetLoop) const;
863
864    /// addToCoefficient - Given a linear SCEV Expr,
865    /// return the SCEV given by adding some Value to the
866    /// coefficient corresponding to the specified TargetLoop.
867    /// For example, given a*i + b*j + c*k, adding 1 to the coefficient
868    /// corresponding to the j loop would yield a*i + (b+1)*j + c*k.
869    const SCEV *addToCoefficient(const SCEV *Expr,
870                                 const Loop *TargetLoop,
871                                 const SCEV *Value)  const;
872
873    /// updateDirection - Update direction vector entry
874    /// based on the current constraint.
875    void updateDirection(Dependence::DVEntry &Level,
876                         const Constraint &CurConstraint) const;
877  public:
878    static char ID; // Class identification, replacement for typeinfo
879    DependenceAnalysis() : FunctionPass(ID) {
880      initializeDependenceAnalysisPass(*PassRegistry::getPassRegistry());
881    }
882
883    bool runOnFunction(Function &F);
884    void releaseMemory();
885    void getAnalysisUsage(AnalysisUsage &) const;
886    void print(raw_ostream &, const Module * = 0) const;
887  }; // class DependenceAnalysis
888
889  /// createDependenceAnalysisPass - This creates an instance of the
890  /// DependenceAnalysis pass.
891  FunctionPass *createDependenceAnalysisPass();
892
893} // namespace llvm
894
895#endif
896