MemoryDependenceAnalysis.h revision 750e0e0ad0e599fe701e5492eef5c2cab05f2e5c
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;
344c724006256032e827177afeae04ea62436796e7Chris Lattner
354c724006256032e827177afeae04ea62436796e7Chris Lattner  /// MemDepResult - A memory dependence query can return one of three different
36fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner  /// answers, described below.
374c724006256032e827177afeae04ea62436796e7Chris Lattner  class MemDepResult {
384c724006256032e827177afeae04ea62436796e7Chris Lattner    enum DepType {
39fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      /// Invalid - Clients of MemDep never see this.
40fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      Invalid = 0,
410f41ad3bc9a295332a8e6d025b42cebc9b9e8125Chris Lattner
42b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// Clobber - This is a dependence on the specified instruction which
43b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// clobbers the desired value.  The pointer member of the MemDepResult
44b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// pair holds the instruction that clobbers the memory.  For example,
45b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// this occurs when we see a may-aliased store to the memory location we
46b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// care about.
47b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      Clobber,
48fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner
49b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// Def - This is a dependence on the specified instruction which
50b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// defines/produces the desired memory location.  The pointer member of
51b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// the MemDepResult pair holds the instruction that defines the memory.
52b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      /// Cases of interest:
53b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///   1. This could be a load or store for dependence queries on
54b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      load/store.  The value loaded or stored is the produced value.
55b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      Note that the pointer operand may be different than that of the
56b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      queried pointer due to must aliases and phi translation.  Note
57b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      that the def may not be the same type as the query, the pointers
58b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      may just be must aliases.
59b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///   2. For loads and stores, this could be an allocation instruction. In
60b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      this case, the load is loading an undef value or a store is the
61b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///      first store to (that part of) the allocation.
62b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      ///   3. Dependence queries on calls return Def only when they are
63750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky      ///      readonly calls or memory use intrinsics with identical callees
64750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky      ///      and no intervening clobbers.  No validation is done that the
65750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky      ///      operands to the calls are the same.
66b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      Def,
67b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner
68fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      /// NonLocal - This marker indicates that the query has no dependency in
69fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      /// the specified block.  To find out more, the client should query other
70fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      /// predecessor blocks.
71b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      NonLocal
724c724006256032e827177afeae04ea62436796e7Chris Lattner    };
734c724006256032e827177afeae04ea62436796e7Chris Lattner    typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
744c724006256032e827177afeae04ea62436796e7Chris Lattner    PairTy Value;
754c724006256032e827177afeae04ea62436796e7Chris Lattner    explicit MemDepResult(PairTy V) : Value(V) {}
764c724006256032e827177afeae04ea62436796e7Chris Lattner  public:
774c724006256032e827177afeae04ea62436796e7Chris Lattner    MemDepResult() : Value(0, Invalid) {}
784c724006256032e827177afeae04ea62436796e7Chris Lattner
794c724006256032e827177afeae04ea62436796e7Chris Lattner    /// get methods: These are static ctor methods for creating various
804c724006256032e827177afeae04ea62436796e7Chris Lattner    /// MemDepResult kinds.
81b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    static MemDepResult getDef(Instruction *Inst) {
82b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      return MemDepResult(PairTy(Inst, Def));
83b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    }
84b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    static MemDepResult getClobber(Instruction *Inst) {
85b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner      return MemDepResult(PairTy(Inst, Clobber));
864c724006256032e827177afeae04ea62436796e7Chris Lattner    }
874c724006256032e827177afeae04ea62436796e7Chris Lattner    static MemDepResult getNonLocal() {
884c724006256032e827177afeae04ea62436796e7Chris Lattner      return MemDepResult(PairTy(0, NonLocal));
894c724006256032e827177afeae04ea62436796e7Chris Lattner    }
904c724006256032e827177afeae04ea62436796e7Chris Lattner
91b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    /// isClobber - Return true if this MemDepResult represents a query that is
92b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    /// a instruction clobber dependency.
93b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    bool isClobber() const { return Value.getInt() == Clobber; }
94b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner
95b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    /// isDef - Return true if this MemDepResult represents a query that is
96b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    /// a instruction definition dependency.
97b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner    bool isDef() const { return Value.getInt() == Def; }
984c724006256032e827177afeae04ea62436796e7Chris Lattner
9981acc554b9599bacf843e080dbf4c72fe88de0f7Dan Gohman    /// isNonLocal - Return true if this MemDepResult represents a query that
1004c724006256032e827177afeae04ea62436796e7Chris Lattner    /// is transparent to the start of the block, but where a non-local hasn't
1014c724006256032e827177afeae04ea62436796e7Chris Lattner    /// been done.
10286b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    bool isNonLocal() const { return Value.getInt() == NonLocal; }
1034c724006256032e827177afeae04ea62436796e7Chris Lattner
1044c724006256032e827177afeae04ea62436796e7Chris Lattner    /// getInst() - If this is a normal dependency, return the instruction that
1054c724006256032e827177afeae04ea62436796e7Chris Lattner    /// is depended on.  Otherwise, return null.
106fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    Instruction *getInst() const { return Value.getPointer(); }
1074c724006256032e827177afeae04ea62436796e7Chris Lattner
1089817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling    bool operator==(const MemDepResult &M) const { return Value == M.Value; }
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; }
112fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner  private:
113fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    friend class MemoryDependenceAnalysis;
114fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// Dirty - Entries with this marker occur in a LocalDeps map or
115fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// NonLocalDeps map when the instruction they previously referenced was
116fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// removed from MemDep.  In either case, the entry may include an
117fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// instruction pointer.  If so, the pointer is an instruction in the
118fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// block where scanning can start from, saving some work.
119fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    ///
120bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// In a default-constructed MemDepResult object, the type will be Dirty
121fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// and the instruction pointer will be null.
122fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    ///
123bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner
124fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
125fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    /// state.
126fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    bool isDirty() const { return Value.getInt() == Invalid; }
127bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner
128fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    static MemDepResult getDirty(Instruction *Inst) {
129fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner      return MemDepResult(PairTy(Inst, Invalid));
130fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    }
1314c724006256032e827177afeae04ea62436796e7Chris Lattner  };
13278e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
133e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  /// MemoryDependenceAnalysis - This is an analysis that determines, for a
134e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  /// given memory operation, what preceding memory operations it depends on.
135e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  /// It builds on alias analysis information, and tries to provide a lazy,
136e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  /// caching interface to a common kind of alias information query.
1370e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  ///
1380e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// The dependency information returned is somewhat unusual, but is pragmatic.
1390e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// If queried about a store or call that might modify memory, the analysis
1400e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// will return the instruction[s] that may either load from that memory or
1410e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// store to it.  If queried with a load or call that can never modify memory,
1420e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// the analysis will return calls and stores that might modify the pointer,
1430e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// but generally does not return loads unless a) they are volatile, or
1440e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// b) they load from *must-aliased* pointers.  Returning a dependence on
1450e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// must-alias'd pointers instead of all pointers interacts well with the
1460e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  /// internal caching mechanism.
1470e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner  ///
148e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner  class MemoryDependenceAnalysis : public FunctionPass {
1497f52422a3c821c1deb7171808ffcf83386970791Chris Lattner    // A map from instructions to their dependency.
150fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner    typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
15139f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner    LocalDepMapType LocalDeps;
152df464195fe049d5ea921e2e37f4f833c2ea4e3ecDavid Greene
153bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner  public:
154bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
155bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
156bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner  private:
1576290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
1586290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    /// the dependence is a read only dependence, false if read/write.
159c765a5acad26ae3d86832d4153e6569d1043a9caDan Gohman    typedef PointerIntPair<Value*, 1, bool> ValueIsLoadPair;
1609e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner
1619e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    /// BBSkipFirstBlockPair - This pair is used when caching information for a
1629e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    /// block.  If the pointer is null, the cache value is not a full query that
1639e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    /// starts at the specified block.  If non-null, the bool indicates whether
1649e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    /// or not the contents of the block was skipped.
1659e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
1669e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner
1676290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    /// CachedNonLocalPointerInfo - This map stores the cached results of doing
168e91a4881e17127686345f62df08da6370b14d51fChris Lattner    /// a pointer lookup at the bottom of a block.  The key of this map is the
1696290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    /// pointer+isload bit, the value is a list of <bb->result> mappings.
1709e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    typedef DenseMap<ValueIsLoadPair, std::pair<BBSkipFirstBlockPair,
1719e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner                  NonLocalDepInfo> > CachedNonLocalPointerInfo;
1726290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    CachedNonLocalPointerInfo NonLocalPointerDeps;
1736290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner
1746290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    // A map from instructions to their non-local pointer dependencies.
1756290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    typedef DenseMap<Instruction*,
1766a0dcc10778468b1556474aa43d4a86a14ab15d7Chris Lattner                     SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
1776290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
1786290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner
1796290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner
1804a69bade2385022ca776edc22150f3b750cdf23cChris Lattner    /// PerInstNLInfo - This is the instruction we keep for each cached access
1814a69bade2385022ca776edc22150f3b750cdf23cChris Lattner    /// that we have for an instruction.  The pointer is an owning pointer and
1824a69bade2385022ca776edc22150f3b750cdf23cChris Lattner    /// the bool indicates whether we have any dirty bits in the set.
183bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
18425f4b2b7a3f1f2bbaf954257e7834ba29a6ede7cChris Lattner
1854d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson    // A map from instructions to their non-local dependencies.
1864a69bade2385022ca776edc22150f3b750cdf23cChris Lattner    typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
1874a69bade2385022ca776edc22150f3b750cdf23cChris Lattner
1888c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner    NonLocalDepMapType NonLocalDeps;
1894d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson
190956033a4f5de491fcc07bc3ef0700ad22fd836a0Chris Lattner    // A reverse mapping from dependencies to the dependees.  This is
191179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson    // used when removing instructions to keep the cache coherent.
1927f52422a3c821c1deb7171808ffcf83386970791Chris Lattner    typedef DenseMap<Instruction*,
1938c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner                     SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
1948c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner    ReverseDepMapType ReverseLocalDeps;
19578e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
1964d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson    // A reverse mapping form dependencies to the non-local dependees.
1978c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner    ReverseDepMapType ReverseNonLocalDeps;
1984d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson
199d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    /// Current AA implementation, just a cache.
200d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    AliasAnalysis *AA;
201d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    TargetData *TD;
2024012fdda13710d21b415a79475adc2bbb6628527Chris Lattner    OwningPtr<PredIteratorCache> PredCache;
203179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson  public:
2044012fdda13710d21b415a79475adc2bbb6628527Chris Lattner    MemoryDependenceAnalysis();
2054012fdda13710d21b415a79475adc2bbb6628527Chris Lattner    ~MemoryDependenceAnalysis();
20639f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner    static char ID;
2071cee94f04111cfd7114979d6dfddce2669c9380dDevang Patel
208d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
209d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner    bool runOnFunction(Function &);
2106b278fc7860c1e0e5cf72340e43f78a87159be4cOwen Anderson
2116b278fc7860c1e0e5cf72340e43f78a87159be4cOwen Anderson    /// Clean up memory in between runs
2124012fdda13710d21b415a79475adc2bbb6628527Chris Lattner    void releaseMemory();
2134012fdda13710d21b415a79475adc2bbb6628527Chris Lattner
21478e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
21578e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// and Alias Analysis.
21678e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    ///
21778e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    virtual void getAnalysisUsage(AnalysisUsage &AU) const;
21878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
21978e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// getDependency - Return the instruction on which a memory operation
2206951381995f24dc9c7bbcacefd5a1315784f66f3Chris Lattner    /// depends.  See the class comment for more details.  It is illegal to call
2216951381995f24dc9c7bbcacefd5a1315784f66f3Chris Lattner    /// this on non-memory instructions.
2225391a1d8046396fb4dd005b1910973789f5427f4Chris Lattner    MemDepResult getDependency(Instruction *QueryInst);
2235391a1d8046396fb4dd005b1910973789f5427f4Chris Lattner
2241559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner    /// getNonLocalCallDependency - Perform a full dependency query for the
2251559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner    /// specified call, returning the set of blocks that the value is
22686b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    /// potentially live across.  The returned set of results will include a
22786b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    /// "NonLocal" result for all blocks where the value is live across.
22886b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    ///
229bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// This method assumes the instruction returns a "NonLocal" dependency
23086b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner    /// within its own block.
231bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    ///
232bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// This returns a reference to an internal data structure that may be
233bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// invalidated on the next non-local query or when an instruction is
234bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// removed.  Clients must copy this data if they want it around longer than
235bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner    /// that.
2361559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner    const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
23778e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
2387ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner
2397ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    /// getNonLocalPointerDependency - Perform a full dependency query for an
2407ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    /// access to the specified (non-volatile) memory location, returning the
2417ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    /// set of instructions that either define or clobber the value.
2427ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    ///
2439a193fd8ae630478123938e12b56f84c5b2227b9Chris Lattner    /// This method assumes the pointer has a "NonLocal" dependency within BB.
2447ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner    void getNonLocalPointerDependency(Value *Pointer, bool isLoad,
2457ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner                                      BasicBlock *BB,
2467ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner                                     SmallVectorImpl<NonLocalDepEntry> &Result);
2477ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner
2486f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner    /// GetPHITranslatedValue - Find an available version of the specified value
24962deff066c5ee4474be80ed2d95aca010e237343Chris Lattner    /// PHI translated across the specified edge.  If MemDep isn't able to
25062deff066c5ee4474be80ed2d95aca010e237343Chris Lattner    /// satisfy this request, it returns null.
2516f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner    Value *GetPHITranslatedValue(Value *V,
2526f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner                                 BasicBlock *CurBB, BasicBlock *PredBB,
2536f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner                                 const TargetData *TD) const;
254616613d7a4ddc7cefce53b2bfe3fdcdec6b032c2Chris Lattner
2556f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner    /// GetAvailablePHITranslatedValue - Return the value computed by
2566f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner    /// PHITranslatePointer if it dominates PredBB, otherwise return null.
2576f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner    Value *GetAvailablePHITranslatedValue(Value *V,
2586f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner                                          BasicBlock *CurBB, BasicBlock *PredBB,
2596f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner                                          const TargetData *TD,
2606f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner                                          const DominatorTree &DT) const;
2616f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner
262616613d7a4ddc7cefce53b2bfe3fdcdec6b032c2Chris Lattner    /// InsertPHITranslatedPointer - Insert a computation of the PHI translated
263616613d7a4ddc7cefce53b2bfe3fdcdec6b032c2Chris Lattner    /// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
264dd696052f0e4ecc973d105be19cf1b4b72f9a0c4Chris Lattner    /// block.  All newly created instructions are added to the NewInsts list.
265616613d7a4ddc7cefce53b2bfe3fdcdec6b032c2Chris Lattner    Value *InsertPHITranslatedPointer(Value *V,
266616613d7a4ddc7cefce53b2bfe3fdcdec6b032c2Chris Lattner                                      BasicBlock *CurBB, BasicBlock *PredBB,
2676f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner                                      const TargetData *TD,
268dd696052f0e4ecc973d105be19cf1b4b72f9a0c4Chris Lattner                                      const DominatorTree &DT,
269dd696052f0e4ecc973d105be19cf1b4b72f9a0c4Chris Lattner                                 SmallVectorImpl<Instruction*> &NewInsts) const;
27062deff066c5ee4474be80ed2d95aca010e237343Chris Lattner
27178e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// removeInstruction - Remove an instruction from the dependence analysis,
27278e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson    /// updating the dependence of instructions that previously depended on it.
27339f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner    void removeInstruction(Instruction *InstToRemove);
274179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson
275bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// invalidateCachedPointerInfo - This method is used to invalidate cached
276bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// information about the specified pointer, because it may be too
277bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// conservative in memdep.  This is an optional call that can be used when
278bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// the client detects an equivalence between the pointer and some other
279bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// value and replaces the other value with ptr. This can make Ptr available
280bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    /// in more places that cached info does not necessarily keep.
281bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner    void invalidateCachedPointerInfo(Value *Ptr);
282bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner
283179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson  private:
284e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner    MemDepResult getPointerDependencyFrom(Value *Pointer, uint64_t MemSize,
285e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner                                          bool isLoad,
286e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner                                          BasicBlock::iterator ScanIt,
287e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner                                          BasicBlock *BB);
28820d6f0982ad33818cfa141f80157ac13e36d5550Chris Lattner    MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
2898ef57c5faf77890828ac439482420646b2a0beb8Chris Lattner                                           BasicBlock::iterator ScanIt,
2908ef57c5faf77890828ac439482420646b2a0beb8Chris Lattner                                           BasicBlock *BB);
2919e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner    bool getNonLocalPointerDepFromBB(Value *Pointer, uint64_t Size,
2929863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                     bool isLoad, BasicBlock *BB,
2939863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                     SmallVectorImpl<NonLocalDepEntry> &Result,
2949e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner                                     DenseMap<BasicBlock*, Value*> &Visited,
2959e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner                                     bool SkipFirstBlock = false);
2969863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner    MemDepResult GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize,
2979863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                         bool isLoad, BasicBlock *BB,
2989863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                         NonLocalDepInfo *Cache,
2999863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner                                         unsigned NumSortedEntries);
3009863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner
3016290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner    void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
3029a193fd8ae630478123938e12b56f84c5b2227b9Chris Lattner
3038b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner    /// verifyRemoved - Verify that the specified instruction does not occur
3048b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner    /// in our internal data structures.
3058b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner    void verifyRemoved(Instruction *Inst) const;
3068b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner
30778e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson  };
30878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
30978e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson} // End llvm namespace
31078e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson
31178e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson#endif
312