MemoryDependenceAnalysis.h revision 05e15f8897bd949f9d4bce073d53ed3256c71e2b
178e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps  --*- C++ -*-===//
278e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//
378e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//                     The LLVM Compiler Infrastructure
478e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
778e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//
878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//===----------------------------------------------------------------------===//
978e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//
10e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner// This file defines the MemoryDependenceAnalysis analysis pass.
1178e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//
1278e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson//===----------------------------------------------------------------------===//
1378e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
1478e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson#ifndef LLVM_ANALYSIS_MEMORY_DEPENDENCE_H
1578e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson#define LLVM_ANALYSIS_MEMORY_DEPENDENCE_H
1678e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
175391a1d8046396fb4dd005b1910973789f5427f4Chris Lattner#include "llvm/BasicBlock.h"
1878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson#include "llvm/Pass.h"
1978e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson#include "llvm/ADT/DenseMap.h"
204beedbd0063a8ba6f97db02c3c94707d8ab45b50Owen Anderson#include "llvm/ADT/SmallPtrSet.h"
214012fdda13710d21b415a79475adc2bbb6628527Chris Lattner#include "llvm/ADT/OwningPtr.h"
2239f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner#include "llvm/ADT/PointerIntPair.h"
2378e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
2478e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Andersonnamespace llvm {
25e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  class Function;
26e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  class FunctionPass;
27e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  class Instruction;
28b390b1728e6c36f1f28d75d6f8f27cb91f8a8c56Chris Lattner  class CallSite;
29d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner  class AliasAnalysis;
30d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner  class TargetData;
31fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner  class MemoryDependenceAnalysis;
324012fdda13710d21b415a79475adc2bbb6628527Chris Lattner  class PredIteratorCache;
336f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner  class DominatorTree;
3405e15f8897bd949f9d4bce073d53ed3256c71e2bChris Lattner  class PHITransAddr;
354c724006256032e827177afeae04ea62436796e7Chris Lattner
364c724006256032e827177afeae04ea62436796e7Chris Lattner  /// MemDepResult - A memory dependence query can return one of three different
37fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner  /// answers, described below.
384c724006256032e827177afeae04ea62436796e7Chris Lattner  class MemDepResult {
394c724006256032e827177afeae04ea62436796e7Chris Lattner    enum DepType {
40fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      /// Invalid - Clients of MemDep never see this.
41fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      Invalid = 0,
420f41ad3bc9a295332a8e6d025b42cebc9b9e8125Chris Lattner
43b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// Clobber - This is a dependence on the specified instruction which
44b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// clobbers the desired value.  The pointer member of the MemDepResult
45b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// pair holds the instruction that clobbers the memory.  For example,
46b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// this occurs when we see a may-aliased store to the memory location we
47b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// care about.
48b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      Clobber,
49fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner
50b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// Def - This is a dependence on the specified instruction which
51b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// defines/produces the desired memory location.  The pointer member of
52b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// the MemDepResult pair holds the instruction that defines the memory.
53b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// Cases of interest:
54b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///   1. This could be a load or store for dependence queries on
55b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      load/store.  The value loaded or stored is the produced value.
56b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      Note that the pointer operand may be different than that of the
57b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      queried pointer due to must aliases and phi translation.  Note
58b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      that the def may not be the same type as the query, the pointers
59b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      may just be must aliases.
60b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///   2. For loads and stores, this could be an allocation instruction. In
61b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      this case, the load is loading an undef value or a store is the
62b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      first store to (that part of) the allocation.
63b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///   3. Dependence queries on calls return Def only when they are
64750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky      ///      readonly calls or memory use intrinsics with identical callees
65750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky      ///      and no intervening clobbers.  No validation is done that the
66750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky      ///      operands to the calls are the same.
67b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      Def,
68b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner
69fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      /// NonLocal - This marker indicates that the query has no dependency in
70fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      /// the specified block.  To find out more, the client should query other
71fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      /// predecessor blocks.
72b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      NonLocal
734c724006256032e827177afeae04ea62436796e7Chris Lattner    };
744c724006256032e827177afeae04ea62436796e7Chris Lattner    typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
754c724006256032e827177afeae04ea62436796e7Chris Lattner    PairTy Value;
764c724006256032e827177afeae04ea62436796e7Chris Lattner    explicit MemDepResult(PairTy V) : Value(V) {}
774c724006256032e827177afeae04ea62436796e7Chris Lattner  public:
784c724006256032e827177afeae04ea62436796e7Chris Lattner    MemDepResult() : Value(0, Invalid) {}
794c724006256032e827177afeae04ea62436796e7Chris Lattner
804c724006256032e827177afeae04ea62436796e7Chris Lattner    /// get methods: These are static ctor methods for creating various
814c724006256032e827177afeae04ea62436796e7Chris Lattner    /// MemDepResult kinds.
82b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    static MemDepResult getDef(Instruction *Inst) {
83b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      return MemDepResult(PairTy(Inst, Def));
84b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    }
85b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    static MemDepResult getClobber(Instruction *Inst) {
86b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      return MemDepResult(PairTy(Inst, Clobber));
874c724006256032e827177afeae04ea62436796e7Chris Lattner    }
884c724006256032e827177afeae04ea62436796e7Chris Lattner    static MemDepResult getNonLocal() {
894c724006256032e827177afeae04ea62436796e7Chris Lattner      return MemDepResult(PairTy(0, NonLocal));
904c724006256032e827177afeae04ea62436796e7Chris Lattner    }
914c724006256032e827177afeae04ea62436796e7Chris Lattner
92b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    /// isClobber - Return true if this MemDepResult represents a query that is
93b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    /// a instruction clobber dependency.
94b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    bool isClobber() const { return Value.getInt() == Clobber; }
95b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner
96b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    /// isDef - Return true if this MemDepResult represents a query that is
97b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    /// a instruction definition dependency.
98b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    bool isDef() const { return Value.getInt() == Def; }
994c724006256032e827177afeae04ea62436796e7Chris Lattner
10081acc554b9599bacf843e080dbf4c72fe88de0f7Dan Gohman    /// isNonLocal - Return true if this MemDepResult represents a query that
1014c724006256032e827177afeae04ea62436796e7Chris Lattner    /// is transparent to the start of the block, but where a non-local hasn't
1024c724006256032e827177afeae04ea62436796e7Chris Lattner    /// been done.
10386b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    bool isNonLocal() const { return Value.getInt() == NonLocal; }
1044c724006256032e827177afeae04ea62436796e7Chris Lattner
1054c724006256032e827177afeae04ea62436796e7Chris Lattner    /// getInst() - If this is a normal dependency, return the instruction that
1064c724006256032e827177afeae04ea62436796e7Chris Lattner    /// is depended on.  Otherwise, return null.
107fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    Instruction *getInst() const { return Value.getPointer(); }
1084c724006256032e827177afeae04ea62436796e7Chris Lattner
1099817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling    bool operator==(const MemDepResult &M) const { return Value == M.Value; }
1109817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling    bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
1119817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling    bool operator<(const MemDepResult &M) const { return Value < M.Value; }
1129817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling    bool operator>(const MemDepResult &M) const { return Value > M.Value; }
113fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner  private:
114fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    friend class MemoryDependenceAnalysis;
115fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// Dirty - Entries with this marker occur in a LocalDeps map or
116fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// NonLocalDeps map when the instruction they previously referenced was
117fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// removed from MemDep.  In either case, the entry may include an
118fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// instruction pointer.  If so, the pointer is an instruction in the
119fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// block where scanning can start from, saving some work.
120fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    ///
121bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// In a default-constructed MemDepResult object, the type will be Dirty
122fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// and the instruction pointer will be null.
123fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    ///
124bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner
125fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
126fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// state.
127fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    bool isDirty() const { return Value.getInt() == Invalid; }
128bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner
129fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    static MemDepResult getDirty(Instruction *Inst) {
130fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      return MemDepResult(PairTy(Inst, Invalid));
131fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    }
1324c724006256032e827177afeae04ea62436796e7Chris Lattner  };
13378e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
134e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  /// MemoryDependenceAnalysis - This is an analysis that determines, for a
135e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  /// given memory operation, what preceding memory operations it depends on.
136e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  /// It builds on alias analysis information, and tries to provide a lazy,
137e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  /// caching interface to a common kind of alias information query.
1380e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  ///
1390e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// The dependency information returned is somewhat unusual, but is pragmatic.
1400e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// If queried about a store or call that might modify memory, the analysis
1410e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// will return the instruction[s] that may either load from that memory or
1420e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// store to it.  If queried with a load or call that can never modify memory,
1430e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// the analysis will return calls and stores that might modify the pointer,
1440e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// but generally does not return loads unless a) they are volatile, or
1450e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// b) they load from *must-aliased* pointers.  Returning a dependence on
1460e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// must-alias'd pointers instead of all pointers interacts well with the
1470e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// internal caching mechanism.
1480e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  ///
149e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  class MemoryDependenceAnalysis : public FunctionPass {
1507f52422a3c821c1deb7171808ffcf83386970791Chris Lattner    // A map from instructions to their dependency.
151fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
15239f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner    LocalDepMapType LocalDeps;
153df464195fe049d5ea921e2e37f4f833c2ea4e3ecDavid Greene
154bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner  public:
155bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
156bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
157bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner  private:
1586290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
1596290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    /// the dependence is a read only dependence, false if read/write.
160c765a5acad26ae3d86832d4153e6569d1043a9caDan Gohman    typedef PointerIntPair<Value*, 1, bool> ValueIsLoadPair;
1619e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner
1629e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    /// BBSkipFirstBlockPair - This pair is used when caching information for a
1639e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    /// block.  If the pointer is null, the cache value is not a full query that
1649e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    /// starts at the specified block.  If non-null, the bool indicates whether
1659e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    /// or not the contents of the block was skipped.
1669e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
1679e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner
1686290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    /// CachedNonLocalPointerInfo - This map stores the cached results of doing
169e91a4881e17127686345f62df08da6370b14d51fChris Lattner    /// a pointer lookup at the bottom of a block.  The key of this map is the
1706290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    /// pointer+isload bit, the value is a list of <bb->result> mappings.
1719e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    typedef DenseMap<ValueIsLoadPair, std::pair<BBSkipFirstBlockPair,
1729e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner                  NonLocalDepInfo> > CachedNonLocalPointerInfo;
1736290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    CachedNonLocalPointerInfo NonLocalPointerDeps;
1746290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner
1756290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    // A map from instructions to their non-local pointer dependencies.
1766290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    typedef DenseMap<Instruction*,
1776a0dcc10778468b1556474aa43d4a86a14ab15d7Chris Lattner                     SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
1786290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
1796290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner
1806290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner
1814a69bade2385022ca776edc22150f3b750cdf23cChris Lattner    /// PerInstNLInfo - This is the instruction we keep for each cached access
1824a69bade2385022ca776edc22150f3b750cdf23cChris Lattner    /// that we have for an instruction.  The pointer is an owning pointer and
1834a69bade2385022ca776edc22150f3b750cdf23cChris Lattner    /// the bool indicates whether we have any dirty bits in the set.
184bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
18525f4b2b7a3f1f2bbaf954257e7834ba29a6ede7cChris Lattner
1864d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson    // A map from instructions to their non-local dependencies.
1874a69bade2385022ca776edc22150f3b750cdf23cChris Lattner    typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
1884a69bade2385022ca776edc22150f3b750cdf23cChris Lattner
1898c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner    NonLocalDepMapType NonLocalDeps;
1904d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson
191956033a4f5de491fcc07bc3ef0700ad22fd836a0Chris Lattner    // A reverse mapping from dependencies to the dependees.  This is
192179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson    // used when removing instructions to keep the cache coherent.
1937f52422a3c821c1deb7171808ffcf83386970791Chris Lattner    typedef DenseMap<Instruction*,
1948c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner                     SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
1958c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner    ReverseDepMapType ReverseLocalDeps;
19678e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
1974d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson    // A reverse mapping form dependencies to the non-local dependees.
1988c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner    ReverseDepMapType ReverseNonLocalDeps;
1994d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson
200d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    /// Current AA implementation, just a cache.
201d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    AliasAnalysis *AA;
202d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    TargetData *TD;
2034012fdda13710d21b415a79475adc2bbb6628527Chris Lattner    OwningPtr<PredIteratorCache> PredCache;
204179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson  public:
2054012fdda13710d21b415a79475adc2bbb6628527Chris Lattner    MemoryDependenceAnalysis();
2064012fdda13710d21b415a79475adc2bbb6628527Chris Lattner    ~MemoryDependenceAnalysis();
20739f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner    static char ID;
2081cee94f04111cfd7114979d6dfddce2669c9380dDevang Patel
209d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
210d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    bool runOnFunction(Function &);
2116b278fc7860c1e0e5cf72340e43f78a87159be4cOwen Anderson
2126b278fc7860c1e0e5cf72340e43f78a87159be4cOwen Anderson    /// Clean up memory in between runs
2134012fdda13710d21b415a79475adc2bbb6628527Chris Lattner    void releaseMemory();
2144012fdda13710d21b415a79475adc2bbb6628527Chris Lattner
21578e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
21678e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// and Alias Analysis.
21778e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    ///
21878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    virtual void getAnalysisUsage(AnalysisUsage &AU) const;
21978e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
22078e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// getDependency - Return the instruction on which a memory operation
2216951381995f24dc9c7bbcacefd5a1315784f66f3Chris Lattner    /// depends.  See the class comment for more details.  It is illegal to call
2226951381995f24dc9c7bbcacefd5a1315784f66f3Chris Lattner    /// this on non-memory instructions.
2235391a1d8046396fb4dd005b1910973789f5427f4Chris Lattner    MemDepResult getDependency(Instruction *QueryInst);
2245391a1d8046396fb4dd005b1910973789f5427f4Chris Lattner
2251559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner    /// getNonLocalCallDependency - Perform a full dependency query for the
2261559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner    /// specified call, returning the set of blocks that the value is
22786b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    /// potentially live across.  The returned set of results will include a
22886b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    /// "NonLocal" result for all blocks where the value is live across.
22986b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    ///
230bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// This method assumes the instruction returns a "NonLocal" dependency
23186b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    /// within its own block.
232bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    ///
233bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// This returns a reference to an internal data structure that may be
234bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// invalidated on the next non-local query or when an instruction is
235bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// removed.  Clients must copy this data if they want it around longer than
236bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// that.
2371559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner    const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
23878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
2397ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner
2407ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    /// getNonLocalPointerDependency - Perform a full dependency query for an
2417ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    /// access to the specified (non-volatile) memory location, returning the
2427ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    /// set of instructions that either define or clobber the value.
2437ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    ///
2449a193fd8ae630478123938e12b56f84c5b2227b9Chris Lattner    /// This method assumes the pointer has a "NonLocal" dependency within BB.
2457ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    void getNonLocalPointerDependency(Value *Pointer, bool isLoad,
2467ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner                                      BasicBlock *BB,
2477ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner                                     SmallVectorImpl<NonLocalDepEntry> &Result);
2487ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner
24978e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// removeInstruction - Remove an instruction from the dependence analysis,
25078e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// updating the dependence of instructions that previously depended on it.
25139f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner    void removeInstruction(Instruction *InstToRemove);
252179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson
253bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// invalidateCachedPointerInfo - This method is used to invalidate cached
254bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// information about the specified pointer, because it may be too
255bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// conservative in memdep.  This is an optional call that can be used when
256bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// the client detects an equivalence between the pointer and some other
257bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// value and replaces the other value with ptr. This can make Ptr available
258bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// in more places that cached info does not necessarily keep.
259bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    void invalidateCachedPointerInfo(Value *Ptr);
260bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner
261179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson  private:
262e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner    MemDepResult getPointerDependencyFrom(Value *Pointer, uint64_t MemSize,
263e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner                                          bool isLoad,
264e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner                                          BasicBlock::iterator ScanIt,
265e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner                                          BasicBlock *BB);
26620d6f0982ad33818cfa141f80157ac13e36d5550Chris Lattner    MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
2678ef57c5faf77890828ac439482420646b2a0beb8Chris Lattner                                           BasicBlock::iterator ScanIt,
2688ef57c5faf77890828ac439482420646b2a0beb8Chris Lattner                                           BasicBlock *BB);
26905e15f8897bd949f9d4bce073d53ed3256c71e2bChris Lattner    bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer, uint64_t Size,
2709863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                     bool isLoad, BasicBlock *BB,
2719863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                     SmallVectorImpl<NonLocalDepEntry> &Result,
2729e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner                                     DenseMap<BasicBlock*, Value*> &Visited,
2739e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner                                     bool SkipFirstBlock = false);
2749863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner    MemDepResult GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize,
2759863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                         bool isLoad, BasicBlock *BB,
2769863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                         NonLocalDepInfo *Cache,
2779863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                         unsigned NumSortedEntries);
2789863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner
2796290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
2809a193fd8ae630478123938e12b56f84c5b2227b9Chris Lattner
2818b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner    /// verifyRemoved - Verify that the specified instruction does not occur
2828b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner    /// in our internal data structures.
2838b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner    void verifyRemoved(Instruction *Inst) const;
2848b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner
28578e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson  };
28678e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
28778e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson} // End llvm namespace
28878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
28978e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson#endif
290