1193323Sed//===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps --*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the MemoryDependenceAnalysis analysis pass. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14249423Sdim#ifndef LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H 15249423Sdim#define LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H 16193323Sed 17193323Sed#include "llvm/ADT/DenseMap.h" 18193323Sed#include "llvm/ADT/OwningPtr.h" 19193323Sed#include "llvm/ADT/PointerIntPair.h" 20249423Sdim#include "llvm/ADT/SmallPtrSet.h" 21249423Sdim#include "llvm/Analysis/AliasAnalysis.h" 22249423Sdim#include "llvm/IR/BasicBlock.h" 23249423Sdim#include "llvm/Pass.h" 24249423Sdim#include "llvm/Support/ValueHandle.h" 25193323Sed 26193323Sednamespace llvm { 27193323Sed class Function; 28193323Sed class FunctionPass; 29193323Sed class Instruction; 30193323Sed class CallSite; 31193323Sed class AliasAnalysis; 32243830Sdim class DataLayout; 33193323Sed class MemoryDependenceAnalysis; 34193323Sed class PredIteratorCache; 35199989Srdivacky class DominatorTree; 36200581Srdivacky class PHITransAddr; 37249423Sdim 38193323Sed /// MemDepResult - A memory dependence query can return one of three different 39193323Sed /// answers, described below. 40193323Sed class MemDepResult { 41193323Sed enum DepType { 42193323Sed /// Invalid - Clients of MemDep never see this. 43193323Sed Invalid = 0, 44249423Sdim 45193323Sed /// Clobber - This is a dependence on the specified instruction which 46193323Sed /// clobbers the desired value. The pointer member of the MemDepResult 47193323Sed /// pair holds the instruction that clobbers the memory. For example, 48193323Sed /// this occurs when we see a may-aliased store to the memory location we 49193323Sed /// care about. 50218893Sdim /// 51221345Sdim /// There are several cases that may be interesting here: 52221345Sdim /// 1. Loads are clobbered by may-alias stores. 53221345Sdim /// 2. Loads are considered clobbered by partially-aliased loads. The 54221345Sdim /// client may choose to analyze deeper into these cases. 55193323Sed Clobber, 56193323Sed 57193323Sed /// Def - This is a dependence on the specified instruction which 58193323Sed /// defines/produces the desired memory location. The pointer member of 59193323Sed /// the MemDepResult pair holds the instruction that defines the memory. 60193323Sed /// Cases of interest: 61193323Sed /// 1. This could be a load or store for dependence queries on 62193323Sed /// load/store. The value loaded or stored is the produced value. 63193323Sed /// Note that the pointer operand may be different than that of the 64193323Sed /// queried pointer due to must aliases and phi translation. Note 65193323Sed /// that the def may not be the same type as the query, the pointers 66193323Sed /// may just be must aliases. 67193323Sed /// 2. For loads and stores, this could be an allocation instruction. In 68193323Sed /// this case, the load is loading an undef value or a store is the 69193323Sed /// first store to (that part of) the allocation. 70193323Sed /// 3. Dependence queries on calls return Def only when they are 71200581Srdivacky /// readonly calls or memory use intrinsics with identical callees 72200581Srdivacky /// and no intervening clobbers. No validation is done that the 73200581Srdivacky /// operands to the calls are the same. 74193323Sed Def, 75249423Sdim 76226633Sdim /// Other - This marker indicates that the query has no known dependency 77226633Sdim /// in the specified block. More detailed state info is encoded in the 78226633Sdim /// upper part of the pair (i.e. the Instruction*) 79226633Sdim Other 80226633Sdim }; 81226633Sdim /// If DepType is "Other", the upper part of the pair 82226633Sdim /// (i.e. the Instruction* part) is instead used to encode more detailed 83226633Sdim /// type information as follows 84226633Sdim enum OtherType { 85193323Sed /// NonLocal - This marker indicates that the query has no dependency in 86193323Sed /// the specified block. To find out more, the client should query other 87193323Sed /// predecessor blocks. 88226633Sdim NonLocal = 0x4, 89226633Sdim /// NonFuncLocal - This marker indicates that the query has no 90226633Sdim /// dependency in the specified function. 91226633Sdim NonFuncLocal = 0x8, 92226633Sdim /// Unknown - This marker indicates that the query dependency 93226633Sdim /// is unknown. 94226633Sdim Unknown = 0xc 95193323Sed }; 96226633Sdim 97193323Sed typedef PointerIntPair<Instruction*, 2, DepType> PairTy; 98193323Sed PairTy Value; 99193323Sed explicit MemDepResult(PairTy V) : Value(V) {} 100193323Sed public: 101193323Sed MemDepResult() : Value(0, Invalid) {} 102249423Sdim 103193323Sed /// get methods: These are static ctor methods for creating various 104193323Sed /// MemDepResult kinds. 105193323Sed static MemDepResult getDef(Instruction *Inst) { 106224145Sdim assert(Inst && "Def requires inst"); 107193323Sed return MemDepResult(PairTy(Inst, Def)); 108193323Sed } 109193323Sed static MemDepResult getClobber(Instruction *Inst) { 110224145Sdim assert(Inst && "Clobber requires inst"); 111193323Sed return MemDepResult(PairTy(Inst, Clobber)); 112193323Sed } 113193323Sed static MemDepResult getNonLocal() { 114226633Sdim return MemDepResult( 115226633Sdim PairTy(reinterpret_cast<Instruction*>(NonLocal), Other)); 116193323Sed } 117226633Sdim static MemDepResult getNonFuncLocal() { 118226633Sdim return MemDepResult( 119226633Sdim PairTy(reinterpret_cast<Instruction*>(NonFuncLocal), Other)); 120226633Sdim } 121224145Sdim static MemDepResult getUnknown() { 122226633Sdim return MemDepResult( 123226633Sdim PairTy(reinterpret_cast<Instruction*>(Unknown), Other)); 124224145Sdim } 125193323Sed 126193323Sed /// isClobber - Return true if this MemDepResult represents a query that is 127239462Sdim /// an instruction clobber dependency. 128226633Sdim bool isClobber() const { return Value.getInt() == Clobber; } 129193323Sed 130193323Sed /// isDef - Return true if this MemDepResult represents a query that is 131239462Sdim /// an instruction definition dependency. 132193323Sed bool isDef() const { return Value.getInt() == Def; } 133249423Sdim 134198090Srdivacky /// isNonLocal - Return true if this MemDepResult represents a query that 135193323Sed /// is transparent to the start of the block, but where a non-local hasn't 136193323Sed /// been done. 137226633Sdim bool isNonLocal() const { 138226633Sdim return Value.getInt() == Other 139226633Sdim && Value.getPointer() == reinterpret_cast<Instruction*>(NonLocal); 140226633Sdim } 141226633Sdim 142226633Sdim /// isNonFuncLocal - Return true if this MemDepResult represents a query 143226633Sdim /// that is transparent to the start of the function. 144226633Sdim bool isNonFuncLocal() const { 145226633Sdim return Value.getInt() == Other 146226633Sdim && Value.getPointer() == reinterpret_cast<Instruction*>(NonFuncLocal); 147226633Sdim } 148249423Sdim 149226633Sdim /// isUnknown - Return true if this MemDepResult represents a query which 150226633Sdim /// cannot and/or will not be computed. 151226633Sdim bool isUnknown() const { 152226633Sdim return Value.getInt() == Other 153226633Sdim && Value.getPointer() == reinterpret_cast<Instruction*>(Unknown); 154226633Sdim } 155226633Sdim 156193323Sed /// getInst() - If this is a normal dependency, return the instruction that 157193323Sed /// is depended on. Otherwise, return null. 158226633Sdim Instruction *getInst() const { 159226633Sdim if (Value.getInt() == Other) return NULL; 160226633Sdim return Value.getPointer(); 161226633Sdim } 162249423Sdim 163193323Sed bool operator==(const MemDepResult &M) const { return Value == M.Value; } 164193323Sed bool operator!=(const MemDepResult &M) const { return Value != M.Value; } 165193323Sed bool operator<(const MemDepResult &M) const { return Value < M.Value; } 166193323Sed bool operator>(const MemDepResult &M) const { return Value > M.Value; } 167193323Sed private: 168193323Sed friend class MemoryDependenceAnalysis; 169193323Sed /// Dirty - Entries with this marker occur in a LocalDeps map or 170193323Sed /// NonLocalDeps map when the instruction they previously referenced was 171193323Sed /// removed from MemDep. In either case, the entry may include an 172193323Sed /// instruction pointer. If so, the pointer is an instruction in the 173193323Sed /// block where scanning can start from, saving some work. 174193323Sed /// 175193323Sed /// In a default-constructed MemDepResult object, the type will be Dirty 176193323Sed /// and the instruction pointer will be null. 177193323Sed /// 178249423Sdim 179193323Sed /// isDirty - Return true if this is a MemDepResult in its dirty/invalid. 180193323Sed /// state. 181193323Sed bool isDirty() const { return Value.getInt() == Invalid; } 182249423Sdim 183193323Sed static MemDepResult getDirty(Instruction *Inst) { 184193323Sed return MemDepResult(PairTy(Inst, Invalid)); 185193323Sed } 186193323Sed }; 187193323Sed 188218893Sdim /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache. For 189218893Sdim /// each BasicBlock (the BB entry) it keeps a MemDepResult. 190218893Sdim class NonLocalDepEntry { 191218893Sdim BasicBlock *BB; 192218893Sdim MemDepResult Result; 193218893Sdim public: 194218893Sdim NonLocalDepEntry(BasicBlock *bb, MemDepResult result) 195218893Sdim : BB(bb), Result(result) {} 196218893Sdim 197218893Sdim // This is used for searches. 198218893Sdim NonLocalDepEntry(BasicBlock *bb) : BB(bb) {} 199218893Sdim 200218893Sdim // BB is the sort key, it can't be changed. 201218893Sdim BasicBlock *getBB() const { return BB; } 202249423Sdim 203218893Sdim void setResult(const MemDepResult &R) { Result = R; } 204218893Sdim 205218893Sdim const MemDepResult &getResult() const { return Result; } 206249423Sdim 207218893Sdim bool operator<(const NonLocalDepEntry &RHS) const { 208218893Sdim return BB < RHS.BB; 209218893Sdim } 210218893Sdim }; 211249423Sdim 212201360Srdivacky /// NonLocalDepResult - This is a result from a NonLocal dependence query. 213201360Srdivacky /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the 214201360Srdivacky /// (potentially phi translated) address that was live in the block. 215201360Srdivacky class NonLocalDepResult { 216218893Sdim NonLocalDepEntry Entry; 217201360Srdivacky Value *Address; 218200581Srdivacky public: 219201360Srdivacky NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address) 220218893Sdim : Entry(bb, result), Address(address) {} 221249423Sdim 222200581Srdivacky // BB is the sort key, it can't be changed. 223218893Sdim BasicBlock *getBB() const { return Entry.getBB(); } 224249423Sdim 225200581Srdivacky void setResult(const MemDepResult &R, Value *Addr) { 226218893Sdim Entry.setResult(R); 227200581Srdivacky Address = Addr; 228200581Srdivacky } 229249423Sdim 230218893Sdim const MemDepResult &getResult() const { return Entry.getResult(); } 231249423Sdim 232200581Srdivacky /// getAddress - Return the address of this pointer in this block. This can 233200581Srdivacky /// be different than the address queried for the non-local result because 234200581Srdivacky /// of phi translation. This returns null if the address was not available 235200581Srdivacky /// in a block (i.e. because phi translation failed) or if this is a cached 236200581Srdivacky /// result and that address was deleted. 237200581Srdivacky /// 238200581Srdivacky /// The address is always null for a non-local 'call' dependence. 239200581Srdivacky Value *getAddress() const { return Address; } 240201360Srdivacky }; 241249423Sdim 242193323Sed /// MemoryDependenceAnalysis - This is an analysis that determines, for a 243193323Sed /// given memory operation, what preceding memory operations it depends on. 244193323Sed /// It builds on alias analysis information, and tries to provide a lazy, 245193323Sed /// caching interface to a common kind of alias information query. 246193323Sed /// 247193323Sed /// The dependency information returned is somewhat unusual, but is pragmatic. 248193323Sed /// If queried about a store or call that might modify memory, the analysis 249193323Sed /// will return the instruction[s] that may either load from that memory or 250193323Sed /// store to it. If queried with a load or call that can never modify memory, 251193323Sed /// the analysis will return calls and stores that might modify the pointer, 252193323Sed /// but generally does not return loads unless a) they are volatile, or 253193323Sed /// b) they load from *must-aliased* pointers. Returning a dependence on 254193323Sed /// must-alias'd pointers instead of all pointers interacts well with the 255193323Sed /// internal caching mechanism. 256193323Sed /// 257193323Sed class MemoryDependenceAnalysis : public FunctionPass { 258193323Sed // A map from instructions to their dependency. 259193323Sed typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType; 260193323Sed LocalDepMapType LocalDeps; 261193323Sed 262193323Sed public: 263193323Sed typedef std::vector<NonLocalDepEntry> NonLocalDepInfo; 264193323Sed private: 265193323Sed /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if 266193323Sed /// the dependence is a read only dependence, false if read/write. 267218893Sdim typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair; 268193323Sed 269193323Sed /// BBSkipFirstBlockPair - This pair is used when caching information for a 270193323Sed /// block. If the pointer is null, the cache value is not a full query that 271193323Sed /// starts at the specified block. If non-null, the bool indicates whether 272193323Sed /// or not the contents of the block was skipped. 273193323Sed typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair; 274193323Sed 275218893Sdim /// NonLocalPointerInfo - This record is the information kept for each 276218893Sdim /// (value, is load) pair. 277218893Sdim struct NonLocalPointerInfo { 278218893Sdim /// Pair - The pair of the block and the skip-first-block flag. 279218893Sdim BBSkipFirstBlockPair Pair; 280218893Sdim /// NonLocalDeps - The results of the query for each relevant block. 281218893Sdim NonLocalDepInfo NonLocalDeps; 282218893Sdim /// Size - The maximum size of the dereferences of the 283218893Sdim /// pointer. May be UnknownSize if the sizes are unknown. 284218893Sdim uint64_t Size; 285218893Sdim /// TBAATag - The TBAA tag associated with dereferences of the 286218893Sdim /// pointer. May be null if there are no tags or conflicting tags. 287218893Sdim const MDNode *TBAATag; 288218893Sdim 289218893Sdim NonLocalPointerInfo() : Size(AliasAnalysis::UnknownSize), TBAATag(0) {} 290218893Sdim }; 291218893Sdim 292193323Sed /// CachedNonLocalPointerInfo - This map stores the cached results of doing 293193323Sed /// a pointer lookup at the bottom of a block. The key of this map is the 294193323Sed /// pointer+isload bit, the value is a list of <bb->result> mappings. 295218893Sdim typedef DenseMap<ValueIsLoadPair, 296218893Sdim NonLocalPointerInfo> CachedNonLocalPointerInfo; 297193323Sed CachedNonLocalPointerInfo NonLocalPointerDeps; 298193323Sed 299193323Sed // A map from instructions to their non-local pointer dependencies. 300249423Sdim typedef DenseMap<Instruction*, 301193323Sed SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy; 302193323Sed ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps; 303193323Sed 304249423Sdim 305193323Sed /// PerInstNLInfo - This is the instruction we keep for each cached access 306193323Sed /// that we have for an instruction. The pointer is an owning pointer and 307193323Sed /// the bool indicates whether we have any dirty bits in the set. 308193323Sed typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo; 309249423Sdim 310193323Sed // A map from instructions to their non-local dependencies. 311193323Sed typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType; 312249423Sdim 313193323Sed NonLocalDepMapType NonLocalDeps; 314249423Sdim 315193323Sed // A reverse mapping from dependencies to the dependees. This is 316193323Sed // used when removing instructions to keep the cache coherent. 317193323Sed typedef DenseMap<Instruction*, 318193323Sed SmallPtrSet<Instruction*, 4> > ReverseDepMapType; 319193323Sed ReverseDepMapType ReverseLocalDeps; 320249423Sdim 321204642Srdivacky // A reverse mapping from dependencies to the non-local dependees. 322193323Sed ReverseDepMapType ReverseNonLocalDeps; 323249423Sdim 324193323Sed /// Current AA implementation, just a cache. 325193323Sed AliasAnalysis *AA; 326243830Sdim DataLayout *TD; 327234353Sdim DominatorTree *DT; 328193323Sed OwningPtr<PredIteratorCache> PredCache; 329193323Sed public: 330193323Sed MemoryDependenceAnalysis(); 331193323Sed ~MemoryDependenceAnalysis(); 332193323Sed static char ID; 333193323Sed 334193323Sed /// Pass Implementation stuff. This doesn't do any analysis eagerly. 335193323Sed bool runOnFunction(Function &); 336249423Sdim 337193323Sed /// Clean up memory in between runs 338193323Sed void releaseMemory(); 339249423Sdim 340193323Sed /// getAnalysisUsage - Does not modify anything. It uses Value Numbering 341193323Sed /// and Alias Analysis. 342193323Sed /// 343193323Sed virtual void getAnalysisUsage(AnalysisUsage &AU) const; 344249423Sdim 345193323Sed /// getDependency - Return the instruction on which a memory operation 346193323Sed /// depends. See the class comment for more details. It is illegal to call 347193323Sed /// this on non-memory instructions. 348193323Sed MemDepResult getDependency(Instruction *QueryInst); 349193323Sed 350193323Sed /// getNonLocalCallDependency - Perform a full dependency query for the 351193323Sed /// specified call, returning the set of blocks that the value is 352193323Sed /// potentially live across. The returned set of results will include a 353193323Sed /// "NonLocal" result for all blocks where the value is live across. 354193323Sed /// 355193323Sed /// This method assumes the instruction returns a "NonLocal" dependency 356193323Sed /// within its own block. 357193323Sed /// 358193323Sed /// This returns a reference to an internal data structure that may be 359193323Sed /// invalidated on the next non-local query or when an instruction is 360193323Sed /// removed. Clients must copy this data if they want it around longer than 361193323Sed /// that. 362193323Sed const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS); 363249423Sdim 364249423Sdim 365193323Sed /// getNonLocalPointerDependency - Perform a full dependency query for an 366193323Sed /// access to the specified (non-volatile) memory location, returning the 367193323Sed /// set of instructions that either define or clobber the value. 368193323Sed /// 369193323Sed /// This method assumes the pointer has a "NonLocal" dependency within BB. 370218893Sdim void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, 371218893Sdim bool isLoad, BasicBlock *BB, 372201360Srdivacky SmallVectorImpl<NonLocalDepResult> &Result); 373218893Sdim 374193323Sed /// removeInstruction - Remove an instruction from the dependence analysis, 375193323Sed /// updating the dependence of instructions that previously depended on it. 376193323Sed void removeInstruction(Instruction *InstToRemove); 377249423Sdim 378193323Sed /// invalidateCachedPointerInfo - This method is used to invalidate cached 379193323Sed /// information about the specified pointer, because it may be too 380193323Sed /// conservative in memdep. This is an optional call that can be used when 381193323Sed /// the client detects an equivalence between the pointer and some other 382193323Sed /// value and replaces the other value with ptr. This can make Ptr available 383193323Sed /// in more places that cached info does not necessarily keep. 384193323Sed void invalidateCachedPointerInfo(Value *Ptr); 385204642Srdivacky 386204642Srdivacky /// invalidateCachedPredecessors - Clear the PredIteratorCache info. 387204642Srdivacky /// This needs to be done when the CFG changes, e.g., due to splitting 388204642Srdivacky /// critical edges. 389204642Srdivacky void invalidateCachedPredecessors(); 390249423Sdim 391218893Sdim /// getPointerDependencyFrom - Return the instruction on which a memory 392218893Sdim /// location depends. If isLoad is true, this routine ignores may-aliases 393218893Sdim /// with read-only operations. If isLoad is false, this routine ignores 394249423Sdim /// may-aliases with reads from read-only locations. If possible, pass 395249423Sdim /// the query instruction as well; this function may take advantage of 396249423Sdim /// the metadata annotated to the query instruction to refine the result. 397218893Sdim /// 398218893Sdim /// Note that this is an uncached query, and thus may be inefficient. 399218893Sdim /// 400218893Sdim MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc, 401249423Sdim bool isLoad, 402193323Sed BasicBlock::iterator ScanIt, 403249423Sdim BasicBlock *BB, 404249423Sdim Instruction *QueryInst = 0); 405249423Sdim 406249423Sdim 407221345Sdim /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that 408221345Sdim /// looks at a memory location for a load (specified by MemLocBase, Offs, 409221345Sdim /// and Size) and compares it against a load. If the specified load could 410221345Sdim /// be safely widened to a larger integer load that is 1) still efficient, 411221345Sdim /// 2) safe for the target, and 3) would provide the specified memory 412221345Sdim /// location value, then this function returns the size in bytes of the 413221345Sdim /// load width to use. If not, this returns zero. 414221345Sdim static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase, 415221345Sdim int64_t MemLocOffs, 416221345Sdim unsigned MemLocSize, 417221345Sdim const LoadInst *LI, 418243830Sdim const DataLayout &TD); 419249423Sdim 420218893Sdim private: 421193323Sed MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall, 422193323Sed BasicBlock::iterator ScanIt, 423193323Sed BasicBlock *BB); 424218893Sdim bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer, 425218893Sdim const AliasAnalysis::Location &Loc, 426193323Sed bool isLoad, BasicBlock *BB, 427201360Srdivacky SmallVectorImpl<NonLocalDepResult> &Result, 428193323Sed DenseMap<BasicBlock*, Value*> &Visited, 429193323Sed bool SkipFirstBlock = false); 430218893Sdim MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc, 431193323Sed bool isLoad, BasicBlock *BB, 432193323Sed NonLocalDepInfo *Cache, 433193323Sed unsigned NumSortedEntries); 434193323Sed 435193323Sed void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P); 436249423Sdim 437193323Sed /// verifyRemoved - Verify that the specified instruction does not occur 438193323Sed /// in our internal data structures. 439193323Sed void verifyRemoved(Instruction *Inst) const; 440249423Sdim 441193323Sed }; 442193323Sed 443193323Sed} // End llvm namespace 444193323Sed 445193323Sed#endif 446