MemoryDependenceAnalysis.h revision ec9b4ac914e91791c580148cf8068c82d4b2cb91
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" 19dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner#include "llvm/Support/ValueHandle.h" 20c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman#include "llvm/Analysis/AliasAnalysis.h" 2178e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson#include "llvm/ADT/DenseMap.h" 224beedbd0063a8ba6f97db02c3c94707d8ab45b50Owen Anderson#include "llvm/ADT/SmallPtrSet.h" 234012fdda13710d21b415a79475adc2bbb6628527Chris Lattner#include "llvm/ADT/OwningPtr.h" 2439f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner#include "llvm/ADT/PointerIntPair.h" 2578e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson 2678e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Andersonnamespace llvm { 27e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner class Function; 28e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner class FunctionPass; 29e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner class Instruction; 30b390b1728e6c36f1f28d75d6f8f27cb91f8a8c56Chris Lattner class CallSite; 31d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner class AliasAnalysis; 32d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner class TargetData; 33fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner class MemoryDependenceAnalysis; 344012fdda13710d21b415a79475adc2bbb6628527Chris Lattner class PredIteratorCache; 356f7b210b2577fbc9247a9fc5223655390008ae89Chris Lattner class DominatorTree; 3605e15f8897bd949f9d4bce073d53ed3256c71e2bChris Lattner class PHITransAddr; 374c724006256032e827177afeae04ea62436796e7Chris Lattner 384c724006256032e827177afeae04ea62436796e7Chris Lattner /// MemDepResult - A memory dependence query can return one of three different 39fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// answers, described below. 404c724006256032e827177afeae04ea62436796e7Chris Lattner class MemDepResult { 414c724006256032e827177afeae04ea62436796e7Chris Lattner enum DepType { 42fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// Invalid - Clients of MemDep never see this. 43fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner Invalid = 0, 440f41ad3bc9a295332a8e6d025b42cebc9b9e8125Chris Lattner 45b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// Clobber - This is a dependence on the specified instruction which 46b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// clobbers the desired value. The pointer member of the MemDepResult 47b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// pair holds the instruction that clobbers the memory. For example, 48b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// this occurs when we see a may-aliased store to the memory location we 49b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// care about. 50b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner Clobber, 51fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner 52b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// Def - This is a dependence on the specified instruction which 53b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// defines/produces the desired memory location. The pointer member of 54b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// the MemDepResult pair holds the instruction that defines the memory. 55b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// Cases of interest: 56b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// 1. This could be a load or store for dependence queries on 57b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// load/store. The value loaded or stored is the produced value. 58b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// Note that the pointer operand may be different than that of the 59b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// queried pointer due to must aliases and phi translation. Note 60b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// that the def may not be the same type as the query, the pointers 61b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// may just be must aliases. 62b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// 2. For loads and stores, this could be an allocation instruction. In 63b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// this case, the load is loading an undef value or a store is the 64b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// first store to (that part of) the allocation. 65b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// 3. Dependence queries on calls return Def only when they are 66750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky /// readonly calls or memory use intrinsics with identical callees 67750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky /// and no intervening clobbers. No validation is done that the 68750e0e0ad0e599fe701e5492eef5c2cab05f2e5cNick Lewycky /// operands to the calls are the same. 69b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner Def, 70b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner 71fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// NonLocal - This marker indicates that the query has no dependency in 72fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// the specified block. To find out more, the client should query other 73fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// predecessor blocks. 74b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner NonLocal 754c724006256032e827177afeae04ea62436796e7Chris Lattner }; 764c724006256032e827177afeae04ea62436796e7Chris Lattner typedef PointerIntPair<Instruction*, 2, DepType> PairTy; 774c724006256032e827177afeae04ea62436796e7Chris Lattner PairTy Value; 784c724006256032e827177afeae04ea62436796e7Chris Lattner explicit MemDepResult(PairTy V) : Value(V) {} 794c724006256032e827177afeae04ea62436796e7Chris Lattner public: 804c724006256032e827177afeae04ea62436796e7Chris Lattner MemDepResult() : Value(0, Invalid) {} 814c724006256032e827177afeae04ea62436796e7Chris Lattner 824c724006256032e827177afeae04ea62436796e7Chris Lattner /// get methods: These are static ctor methods for creating various 834c724006256032e827177afeae04ea62436796e7Chris Lattner /// MemDepResult kinds. 84b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner static MemDepResult getDef(Instruction *Inst) { 85b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner return MemDepResult(PairTy(Inst, Def)); 86b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner } 87b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner static MemDepResult getClobber(Instruction *Inst) { 88b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner return MemDepResult(PairTy(Inst, Clobber)); 894c724006256032e827177afeae04ea62436796e7Chris Lattner } 904c724006256032e827177afeae04ea62436796e7Chris Lattner static MemDepResult getNonLocal() { 914c724006256032e827177afeae04ea62436796e7Chris Lattner return MemDepResult(PairTy(0, NonLocal)); 924c724006256032e827177afeae04ea62436796e7Chris Lattner } 934c724006256032e827177afeae04ea62436796e7Chris Lattner 94b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// isClobber - Return true if this MemDepResult represents a query that is 95b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// a instruction clobber dependency. 96b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner bool isClobber() const { return Value.getInt() == Clobber; } 97b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner 98b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// isDef - Return true if this MemDepResult represents a query that is 99b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner /// a instruction definition dependency. 100b51deb929ca95ce62e622b0475a05d83f26ab04dChris Lattner bool isDef() const { return Value.getInt() == Def; } 1014c724006256032e827177afeae04ea62436796e7Chris Lattner 10281acc554b9599bacf843e080dbf4c72fe88de0f7Dan Gohman /// isNonLocal - Return true if this MemDepResult represents a query that 1034c724006256032e827177afeae04ea62436796e7Chris Lattner /// is transparent to the start of the block, but where a non-local hasn't 1044c724006256032e827177afeae04ea62436796e7Chris Lattner /// been done. 10586b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner bool isNonLocal() const { return Value.getInt() == NonLocal; } 1064c724006256032e827177afeae04ea62436796e7Chris Lattner 1074c724006256032e827177afeae04ea62436796e7Chris Lattner /// getInst() - If this is a normal dependency, return the instruction that 1084c724006256032e827177afeae04ea62436796e7Chris Lattner /// is depended on. Otherwise, return null. 109fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner Instruction *getInst() const { return Value.getPointer(); } 1104c724006256032e827177afeae04ea62436796e7Chris Lattner 1119817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling bool operator==(const MemDepResult &M) const { return Value == M.Value; } 1129817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling bool operator!=(const MemDepResult &M) const { return Value != M.Value; } 1139817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling bool operator<(const MemDepResult &M) const { return Value < M.Value; } 1149817b24e74532c13bad2c7350f17e17473b8c57aBill Wendling bool operator>(const MemDepResult &M) const { return Value > M.Value; } 115fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner private: 116fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner friend class MemoryDependenceAnalysis; 117fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// Dirty - Entries with this marker occur in a LocalDeps map or 118fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// NonLocalDeps map when the instruction they previously referenced was 119fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// removed from MemDep. In either case, the entry may include an 120fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// instruction pointer. If so, the pointer is an instruction in the 121fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// block where scanning can start from, saving some work. 122fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// 123bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner /// In a default-constructed MemDepResult object, the type will be Dirty 124fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// and the instruction pointer will be null. 125fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// 126bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner 127fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// isDirty - Return true if this is a MemDepResult in its dirty/invalid. 128fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner /// state. 129fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner bool isDirty() const { return Value.getInt() == Invalid; } 130bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner 131fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner static MemDepResult getDirty(Instruction *Inst) { 132fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner return MemDepResult(PairTy(Inst, Invalid)); 133fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner } 1344c724006256032e827177afeae04ea62436796e7Chris Lattner }; 13578e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson 13650bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache. For 13750bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman /// each BasicBlock (the BB entry) it keeps a MemDepResult. 13850bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman class NonLocalDepEntry { 13950bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman BasicBlock *BB; 14050bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman MemDepResult Result; 14150bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman public: 14250bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman NonLocalDepEntry(BasicBlock *bb, MemDepResult result) 14350bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman : BB(bb), Result(result) {} 14450bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman 14550bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman // This is used for searches. 14650bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman NonLocalDepEntry(BasicBlock *bb) : BB(bb) {} 14750bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman 14850bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman // BB is the sort key, it can't be changed. 14950bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman BasicBlock *getBB() const { return BB; } 15050bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman 15150bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman void setResult(const MemDepResult &R) { Result = R; } 15250bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman 15350bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman const MemDepResult &getResult() const { return Result; } 15450bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman 15550bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman bool operator<(const NonLocalDepEntry &RHS) const { 15650bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman return BB < RHS.BB; 15750bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman } 15850bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman }; 15950bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman 1600ee443d169786017d151034b8bd34225bc144c98Chris Lattner /// NonLocalDepResult - This is a result from a NonLocal dependence query. 1610ee443d169786017d151034b8bd34225bc144c98Chris Lattner /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the 1620ee443d169786017d151034b8bd34225bc144c98Chris Lattner /// (potentially phi translated) address that was live in the block. 1630ee443d169786017d151034b8bd34225bc144c98Chris Lattner class NonLocalDepResult { 16450bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman NonLocalDepEntry Entry; 1650ee443d169786017d151034b8bd34225bc144c98Chris Lattner Value *Address; 166e18b97121c286eeff5efe89150b093bf1b7b7bfcChris Lattner public: 1670ee443d169786017d151034b8bd34225bc144c98Chris Lattner NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address) 16850bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman : Entry(bb, result), Address(address) {} 1690ee443d169786017d151034b8bd34225bc144c98Chris Lattner 170e18b97121c286eeff5efe89150b093bf1b7b7bfcChris Lattner // BB is the sort key, it can't be changed. 17150bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman BasicBlock *getBB() const { return Entry.getBB(); } 172e18b97121c286eeff5efe89150b093bf1b7b7bfcChris Lattner 173dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner void setResult(const MemDepResult &R, Value *Addr) { 17450bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman Entry.setResult(R); 175dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner Address = Addr; 176dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner } 1770ee443d169786017d151034b8bd34225bc144c98Chris Lattner 17850bcaece670fea4c936c6730fab9f4d869d2ec67Dan Gohman const MemDepResult &getResult() const { return Entry.getResult(); } 179e18b97121c286eeff5efe89150b093bf1b7b7bfcChris Lattner 180dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner /// getAddress - Return the address of this pointer in this block. This can 181dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner /// be different than the address queried for the non-local result because 182dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner /// of phi translation. This returns null if the address was not available 183dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner /// in a block (i.e. because phi translation failed) or if this is a cached 184dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner /// result and that address was deleted. 185dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner /// 186dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner /// The address is always null for a non-local 'call' dependence. 187dad451cb7c6b94b3af40f59271e24357616a05a9Chris Lattner Value *getAddress() const { return Address; } 1880ee443d169786017d151034b8bd34225bc144c98Chris Lattner }; 1890ee443d169786017d151034b8bd34225bc144c98Chris Lattner 190e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner /// MemoryDependenceAnalysis - This is an analysis that determines, for a 191e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner /// given memory operation, what preceding memory operations it depends on. 192e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner /// It builds on alias analysis information, and tries to provide a lazy, 193e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner /// caching interface to a common kind of alias information query. 1940e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// 1950e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// The dependency information returned is somewhat unusual, but is pragmatic. 1960e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// If queried about a store or call that might modify memory, the analysis 1970e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// will return the instruction[s] that may either load from that memory or 1980e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// store to it. If queried with a load or call that can never modify memory, 1990e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// the analysis will return calls and stores that might modify the pointer, 2000e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// but generally does not return loads unless a) they are volatile, or 2010e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// b) they load from *must-aliased* pointers. Returning a dependence on 2020e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// must-alias'd pointers instead of all pointers interacts well with the 2030e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// internal caching mechanism. 2040e0a5b690ca772a9002d8e8d21edac5f011bc7e8Chris Lattner /// 205e85866313a551fa3d4e2f118c3bf34e96af36763Chris Lattner class MemoryDependenceAnalysis : public FunctionPass { 2067f52422a3c821c1deb7171808ffcf83386970791Chris Lattner // A map from instructions to their dependency. 207fd3dcbea06f934572a3ba02821b1485eb7a073aaChris Lattner typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType; 20839f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner LocalDepMapType LocalDeps; 209df464195fe049d5ea921e2e37f4f833c2ea4e3ecDavid Greene 210bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner public: 211bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner typedef std::vector<NonLocalDepEntry> NonLocalDepInfo; 212bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner private: 2136290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if 2146290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner /// the dependence is a read only dependence, false if read/write. 215ba6ca6dd3bc4a7f30e07010c3b0b95cced719f76Dan Gohman typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair; 2169e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner 2179e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner /// BBSkipFirstBlockPair - This pair is used when caching information for a 2189e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner /// block. If the pointer is null, the cache value is not a full query that 2199e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner /// starts at the specified block. If non-null, the bool indicates whether 2209e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner /// or not the contents of the block was skipped. 2219e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair; 2229e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner 223c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman /// NonLocalPointerInfo - This record is the information kept for each 224c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman /// (value, is load) pair. 225c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman struct NonLocalPointerInfo { 226c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman /// Pair - The pair of the block and the skip-first-block flag. 227c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman BBSkipFirstBlockPair Pair; 228c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman /// NonLocalDeps - The results of the query for each relevant block. 229c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman NonLocalDepInfo NonLocalDeps; 230075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman /// Size - The maximum size of the dereferences of the 231075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman /// pointer. May be UnknownSize if the sizes are unknown. 232075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman uint64_t Size; 233c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman /// TBAATag - The TBAA tag associated with dereferences of the 234c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman /// pointer. May be null if there are no tags or conflicting tags. 235075fb5d68fcb55d26e44c48f07dfdbbfa21ccb2aDan Gohman const MDNode *TBAATag; 236bea77bb6e82e7f7feae71b2cafea4f6a9f6098c0Dan Gohman 237ec9b4ac914e91791c580148cf8068c82d4b2cb91Dan Gohman NonLocalPointerInfo() : Size(AliasAnalysis::UnknownSize), TBAATag(0) {} 238c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman }; 239c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman 2406290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner /// CachedNonLocalPointerInfo - This map stores the cached results of doing 241e91a4881e17127686345f62df08da6370b14d51fChris Lattner /// a pointer lookup at the bottom of a block. The key of this map is the 2426290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner /// pointer+isload bit, the value is a list of <bb->result> mappings. 243c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman typedef DenseMap<ValueIsLoadPair, 244c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman NonLocalPointerInfo> CachedNonLocalPointerInfo; 2456290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner CachedNonLocalPointerInfo NonLocalPointerDeps; 2466290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner 2476290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner // A map from instructions to their non-local pointer dependencies. 2486290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner typedef DenseMap<Instruction*, 2496a0dcc10778468b1556474aa43d4a86a14ab15d7Chris Lattner SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy; 2506290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps; 2516290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner 2526290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner 2534a69bade2385022ca776edc22150f3b750cdf23cChris Lattner /// PerInstNLInfo - This is the instruction we keep for each cached access 2544a69bade2385022ca776edc22150f3b750cdf23cChris Lattner /// that we have for an instruction. The pointer is an owning pointer and 2554a69bade2385022ca776edc22150f3b750cdf23cChris Lattner /// the bool indicates whether we have any dirty bits in the set. 256bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo; 25725f4b2b7a3f1f2bbaf954257e7834ba29a6ede7cChris Lattner 2584d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson // A map from instructions to their non-local dependencies. 2594a69bade2385022ca776edc22150f3b750cdf23cChris Lattner typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType; 2604a69bade2385022ca776edc22150f3b750cdf23cChris Lattner 2618c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner NonLocalDepMapType NonLocalDeps; 2624d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson 263956033a4f5de491fcc07bc3ef0700ad22fd836a0Chris Lattner // A reverse mapping from dependencies to the dependees. This is 264179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson // used when removing instructions to keep the cache coherent. 2657f52422a3c821c1deb7171808ffcf83386970791Chris Lattner typedef DenseMap<Instruction*, 2668c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner SmallPtrSet<Instruction*, 4> > ReverseDepMapType; 2678c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner ReverseDepMapType ReverseLocalDeps; 26878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson 269484d4a30c055eef3101d01a7a468db3413dd20d3Bob Wilson // A reverse mapping from dependencies to the non-local dependees. 2708c4652790e04515f34cf920b0783d6ec4161a313Chris Lattner ReverseDepMapType ReverseNonLocalDeps; 2714d13de4e3bfc5121207efd01e1b31caa6bb4e40bOwen Anderson 272d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner /// Current AA implementation, just a cache. 273d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner AliasAnalysis *AA; 274d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner TargetData *TD; 2754012fdda13710d21b415a79475adc2bbb6628527Chris Lattner OwningPtr<PredIteratorCache> PredCache; 276179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson public: 2774012fdda13710d21b415a79475adc2bbb6628527Chris Lattner MemoryDependenceAnalysis(); 2784012fdda13710d21b415a79475adc2bbb6628527Chris Lattner ~MemoryDependenceAnalysis(); 27939f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner static char ID; 2801cee94f04111cfd7114979d6dfddce2669c9380dDevang Patel 281d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner /// Pass Implementation stuff. This doesn't do any analysis eagerly. 282d777d405cdda8d418ba8e8818e5c1272dfd999a0Chris Lattner bool runOnFunction(Function &); 2836b278fc7860c1e0e5cf72340e43f78a87159be4cOwen Anderson 2846b278fc7860c1e0e5cf72340e43f78a87159be4cOwen Anderson /// Clean up memory in between runs 2854012fdda13710d21b415a79475adc2bbb6628527Chris Lattner void releaseMemory(); 2864012fdda13710d21b415a79475adc2bbb6628527Chris Lattner 28778e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson /// getAnalysisUsage - Does not modify anything. It uses Value Numbering 28878e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson /// and Alias Analysis. 28978e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson /// 29078e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson virtual void getAnalysisUsage(AnalysisUsage &AU) const; 29178e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson 29278e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson /// getDependency - Return the instruction on which a memory operation 2936951381995f24dc9c7bbcacefd5a1315784f66f3Chris Lattner /// depends. See the class comment for more details. It is illegal to call 2946951381995f24dc9c7bbcacefd5a1315784f66f3Chris Lattner /// this on non-memory instructions. 2955391a1d8046396fb4dd005b1910973789f5427f4Chris Lattner MemDepResult getDependency(Instruction *QueryInst); 2965391a1d8046396fb4dd005b1910973789f5427f4Chris Lattner 2971559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner /// getNonLocalCallDependency - Perform a full dependency query for the 2981559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner /// specified call, returning the set of blocks that the value is 29986b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner /// potentially live across. The returned set of results will include a 30086b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner /// "NonLocal" result for all blocks where the value is live across. 30186b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner /// 302bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner /// This method assumes the instruction returns a "NonLocal" dependency 30386b29ef64a36c8779ef7855b3c4b95744eb2f08bChris Lattner /// within its own block. 304bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner /// 305bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner /// This returns a reference to an internal data structure that may be 306bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner /// invalidated on the next non-local query or when an instruction is 307bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner /// removed. Clients must copy this data if they want it around longer than 308bf145d6e2ba01f5099ccaa1b58ed3619406928a0Chris Lattner /// that. 3091559b3625be7b80bee6b066af4b91b9d10dfb5faChris Lattner const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS); 31078e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson 3117ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner 3127ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner /// getNonLocalPointerDependency - Perform a full dependency query for an 3137ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner /// access to the specified (non-volatile) memory location, returning the 3147ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner /// set of instructions that either define or clobber the value. 3157ebcf0324668b7c6ba48832d5d8df95689a8d837Chris Lattner /// 3169a193fd8ae630478123938e12b56f84c5b2227b9Chris Lattner /// This method assumes the pointer has a "NonLocal" dependency within BB. 317c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, 318c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman bool isLoad, BasicBlock *BB, 319c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman SmallVectorImpl<NonLocalDepResult> &Result); 320c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman 32178e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson /// removeInstruction - Remove an instruction from the dependence analysis, 32278e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson /// updating the dependence of instructions that previously depended on it. 32339f372e23e49cecb8db2eb7120eb331173e50c74Chris Lattner void removeInstruction(Instruction *InstToRemove); 324179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson 325bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner /// invalidateCachedPointerInfo - This method is used to invalidate cached 326bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner /// information about the specified pointer, because it may be too 327bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner /// conservative in memdep. This is an optional call that can be used when 328bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner /// the client detects an equivalence between the pointer and some other 329bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner /// value and replaces the other value with ptr. This can make Ptr available 330bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner /// in more places that cached info does not necessarily keep. 331bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner void invalidateCachedPointerInfo(Value *Ptr); 332484d4a30c055eef3101d01a7a468db3413dd20d3Bob Wilson 333484d4a30c055eef3101d01a7a468db3413dd20d3Bob Wilson /// invalidateCachedPredecessors - Clear the PredIteratorCache info. 334484d4a30c055eef3101d01a7a468db3413dd20d3Bob Wilson /// This needs to be done when the CFG changes, e.g., due to splitting 335484d4a30c055eef3101d01a7a468db3413dd20d3Bob Wilson /// critical edges. 336484d4a30c055eef3101d01a7a468db3413dd20d3Bob Wilson void invalidateCachedPredecessors(); 337bc99be10b815e0bfc5102bd5746e9a80feebf6f4Chris Lattner 338179463c0d4c20bfb2c6b1e697eed6f16305a201eOwen Anderson private: 339c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc, 340e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner bool isLoad, 341e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner BasicBlock::iterator ScanIt, 342e79be944c8ced0a0cb80ede8cb9f97e4fdc6778fChris Lattner BasicBlock *BB); 34320d6f0982ad33818cfa141f80157ac13e36d5550Chris Lattner MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall, 3448ef57c5faf77890828ac439482420646b2a0beb8Chris Lattner BasicBlock::iterator ScanIt, 3458ef57c5faf77890828ac439482420646b2a0beb8Chris Lattner BasicBlock *BB); 346c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer, 347c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman const AliasAnalysis::Location &Loc, 3489863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner bool isLoad, BasicBlock *BB, 3490ee443d169786017d151034b8bd34225bc144c98Chris Lattner SmallVectorImpl<NonLocalDepResult> &Result, 3509e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner DenseMap<BasicBlock*, Value*> &Visited, 3519e59c64c14cfe55e7cc9086c6bff8cfeecac361eChris Lattner bool SkipFirstBlock = false); 352c1ac0d7623f4f2047b4ab86bd5a60a9e19432b38Dan Gohman MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc, 3539863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner bool isLoad, BasicBlock *BB, 3549863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner NonLocalDepInfo *Cache, 3559863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner unsigned NumSortedEntries); 3569863c3f507913f17de0fd707e27fde9dc35f6ca6Chris Lattner 3576290f5cac23399201f8785e5ca8b305e42a1342cChris Lattner void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P); 3589a193fd8ae630478123938e12b56f84c5b2227b9Chris Lattner 3598b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner /// verifyRemoved - Verify that the specified instruction does not occur 3608b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner /// in our internal data structures. 3618b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner void verifyRemoved(Instruction *Inst) const; 3628b589fa135d873e683b29ed0918638a79272f5d2Chris Lattner 36378e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson }; 36478e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson 36578e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson} // End llvm namespace 36678e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson 36778e02f78ce68274163e1e63be59abd17aaaf6cbfOwen Anderson#endif 368