LLVM API Documentation

MemoryDependenceAnalysis.h
Go to the documentation of this file.
00001 //===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps  --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MemoryDependenceAnalysis analysis pass.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
00015 #define LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/PointerIntPair.h"
00019 #include "llvm/ADT/SmallPtrSet.h"
00020 #include "llvm/Analysis/AliasAnalysis.h"
00021 #include "llvm/IR/BasicBlock.h"
00022 #include "llvm/IR/ValueHandle.h"
00023 #include "llvm/Pass.h"
00024 
00025 namespace llvm {
00026   class Function;
00027   class FunctionPass;
00028   class Instruction;
00029   class CallSite;
00030   class AliasAnalysis;
00031   class AssumptionTracker;
00032   class DataLayout;
00033   class MemoryDependenceAnalysis;
00034   class PredIteratorCache;
00035   class DominatorTree;
00036   class PHITransAddr;
00037 
00038   /// MemDepResult - A memory dependence query can return one of three different
00039   /// answers, described below.
00040   class MemDepResult {
00041     enum DepType {
00042       /// Invalid - Clients of MemDep never see this.
00043       Invalid = 0,
00044 
00045       /// Clobber - This is a dependence on the specified instruction which
00046       /// clobbers the desired value.  The pointer member of the MemDepResult
00047       /// pair holds the instruction that clobbers the memory.  For example,
00048       /// this occurs when we see a may-aliased store to the memory location we
00049       /// care about.
00050       ///
00051       /// There are several cases that may be interesting here:
00052       ///   1. Loads are clobbered by may-alias stores.
00053       ///   2. Loads are considered clobbered by partially-aliased loads.  The
00054       ///      client may choose to analyze deeper into these cases.
00055       Clobber,
00056 
00057       /// Def - This is a dependence on the specified instruction which
00058       /// defines/produces the desired memory location.  The pointer member of
00059       /// the MemDepResult pair holds the instruction that defines the memory.
00060       /// Cases of interest:
00061       ///   1. This could be a load or store for dependence queries on
00062       ///      load/store.  The value loaded or stored is the produced value.
00063       ///      Note that the pointer operand may be different than that of the
00064       ///      queried pointer due to must aliases and phi translation.  Note
00065       ///      that the def may not be the same type as the query, the pointers
00066       ///      may just be must aliases.
00067       ///   2. For loads and stores, this could be an allocation instruction. In
00068       ///      this case, the load is loading an undef value or a store is the
00069       ///      first store to (that part of) the allocation.
00070       ///   3. Dependence queries on calls return Def only when they are
00071       ///      readonly calls or memory use intrinsics with identical callees
00072       ///      and no intervening clobbers.  No validation is done that the
00073       ///      operands to the calls are the same.
00074       Def,
00075 
00076       /// Other - This marker indicates that the query has no known dependency
00077       /// in the specified block.  More detailed state info is encoded in the
00078       /// upper part of the pair (i.e. the Instruction*)
00079       Other
00080     };
00081     /// If DepType is "Other", the upper part of the pair
00082     /// (i.e. the Instruction* part) is instead used to encode more detailed
00083     /// type information as follows
00084     enum OtherType {
00085       /// NonLocal - This marker indicates that the query has no dependency in
00086       /// the specified block.  To find out more, the client should query other
00087       /// predecessor blocks.
00088       NonLocal = 0x4,
00089       /// NonFuncLocal - This marker indicates that the query has no
00090       /// dependency in the specified function.
00091       NonFuncLocal = 0x8,
00092       /// Unknown - This marker indicates that the query dependency
00093       /// is unknown.
00094       Unknown = 0xc
00095     };
00096 
00097     typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
00098     PairTy Value;
00099     explicit MemDepResult(PairTy V) : Value(V) {}
00100   public:
00101     MemDepResult() : Value(nullptr, Invalid) {}
00102 
00103     /// get methods: These are static ctor methods for creating various
00104     /// MemDepResult kinds.
00105     static MemDepResult getDef(Instruction *Inst) {
00106       assert(Inst && "Def requires inst");
00107       return MemDepResult(PairTy(Inst, Def));
00108     }
00109     static MemDepResult getClobber(Instruction *Inst) {
00110       assert(Inst && "Clobber requires inst");
00111       return MemDepResult(PairTy(Inst, Clobber));
00112     }
00113     static MemDepResult getNonLocal() {
00114       return MemDepResult(
00115         PairTy(reinterpret_cast<Instruction*>(NonLocal), Other));
00116     }
00117     static MemDepResult getNonFuncLocal() {
00118       return MemDepResult(
00119         PairTy(reinterpret_cast<Instruction*>(NonFuncLocal), Other));
00120     }
00121     static MemDepResult getUnknown() {
00122       return MemDepResult(
00123         PairTy(reinterpret_cast<Instruction*>(Unknown), Other));
00124     }
00125 
00126     /// isClobber - Return true if this MemDepResult represents a query that is
00127     /// an instruction clobber dependency.
00128     bool isClobber() const { return Value.getInt() == Clobber; }
00129 
00130     /// isDef - Return true if this MemDepResult represents a query that is
00131     /// an instruction definition dependency.
00132     bool isDef() const { return Value.getInt() == Def; }
00133 
00134     /// isNonLocal - Return true if this MemDepResult represents a query that
00135     /// is transparent to the start of the block, but where a non-local hasn't
00136     /// been done.
00137     bool isNonLocal() const {
00138       return Value.getInt() == Other
00139         && Value.getPointer() == reinterpret_cast<Instruction*>(NonLocal);
00140     }
00141 
00142     /// isNonFuncLocal - Return true if this MemDepResult represents a query
00143     /// that is transparent to the start of the function.
00144     bool isNonFuncLocal() const {
00145       return Value.getInt() == Other
00146         && Value.getPointer() == reinterpret_cast<Instruction*>(NonFuncLocal);
00147     }
00148 
00149     /// isUnknown - Return true if this MemDepResult represents a query which
00150     /// cannot and/or will not be computed.
00151     bool isUnknown() const {
00152       return Value.getInt() == Other
00153         && Value.getPointer() == reinterpret_cast<Instruction*>(Unknown);
00154     }
00155 
00156     /// getInst() - If this is a normal dependency, return the instruction that
00157     /// is depended on.  Otherwise, return null.
00158     Instruction *getInst() const {
00159       if (Value.getInt() == Other) return nullptr;
00160       return Value.getPointer();
00161     }
00162 
00163     bool operator==(const MemDepResult &M) const { return Value == M.Value; }
00164     bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
00165     bool operator<(const MemDepResult &M) const { return Value < M.Value; }
00166     bool operator>(const MemDepResult &M) const { return Value > M.Value; }
00167   private:
00168     friend class MemoryDependenceAnalysis;
00169     /// Dirty - Entries with this marker occur in a LocalDeps map or
00170     /// NonLocalDeps map when the instruction they previously referenced was
00171     /// removed from MemDep.  In either case, the entry may include an
00172     /// instruction pointer.  If so, the pointer is an instruction in the
00173     /// block where scanning can start from, saving some work.
00174     ///
00175     /// In a default-constructed MemDepResult object, the type will be Dirty
00176     /// and the instruction pointer will be null.
00177     ///
00178 
00179     /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
00180     /// state.
00181     bool isDirty() const { return Value.getInt() == Invalid; }
00182 
00183     static MemDepResult getDirty(Instruction *Inst) {
00184       return MemDepResult(PairTy(Inst, Invalid));
00185     }
00186   };
00187 
00188   /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache.  For
00189   /// each BasicBlock (the BB entry) it keeps a MemDepResult.
00190   class NonLocalDepEntry {
00191     BasicBlock *BB;
00192     MemDepResult Result;
00193   public:
00194     NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
00195       : BB(bb), Result(result) {}
00196 
00197     // This is used for searches.
00198     NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
00199 
00200     // BB is the sort key, it can't be changed.
00201     BasicBlock *getBB() const { return BB; }
00202 
00203     void setResult(const MemDepResult &R) { Result = R; }
00204 
00205     const MemDepResult &getResult() const { return Result; }
00206 
00207     bool operator<(const NonLocalDepEntry &RHS) const {
00208       return BB < RHS.BB;
00209     }
00210   };
00211 
00212   /// NonLocalDepResult - This is a result from a NonLocal dependence query.
00213   /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
00214   /// (potentially phi translated) address that was live in the block.
00215   class NonLocalDepResult {
00216     NonLocalDepEntry Entry;
00217     Value *Address;
00218   public:
00219     NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
00220       : Entry(bb, result), Address(address) {}
00221 
00222     // BB is the sort key, it can't be changed.
00223     BasicBlock *getBB() const { return Entry.getBB(); }
00224 
00225     void setResult(const MemDepResult &R, Value *Addr) {
00226       Entry.setResult(R);
00227       Address = Addr;
00228     }
00229 
00230     const MemDepResult &getResult() const { return Entry.getResult(); }
00231 
00232     /// getAddress - Return the address of this pointer in this block.  This can
00233     /// be different than the address queried for the non-local result because
00234     /// of phi translation.  This returns null if the address was not available
00235     /// in a block (i.e. because phi translation failed) or if this is a cached
00236     /// result and that address was deleted.
00237     ///
00238     /// The address is always null for a non-local 'call' dependence.
00239     Value *getAddress() const { return Address; }
00240   };
00241 
00242   /// MemoryDependenceAnalysis - This is an analysis that determines, for a
00243   /// given memory operation, what preceding memory operations it depends on.
00244   /// It builds on alias analysis information, and tries to provide a lazy,
00245   /// caching interface to a common kind of alias information query.
00246   ///
00247   /// The dependency information returned is somewhat unusual, but is pragmatic.
00248   /// If queried about a store or call that might modify memory, the analysis
00249   /// will return the instruction[s] that may either load from that memory or
00250   /// store to it.  If queried with a load or call that can never modify memory,
00251   /// the analysis will return calls and stores that might modify the pointer,
00252   /// but generally does not return loads unless a) they are volatile, or
00253   /// b) they load from *must-aliased* pointers.  Returning a dependence on
00254   /// must-alias'd pointers instead of all pointers interacts well with the
00255   /// internal caching mechanism.
00256   ///
00257   class MemoryDependenceAnalysis : public FunctionPass {
00258     // A map from instructions to their dependency.
00259     typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
00260     LocalDepMapType LocalDeps;
00261 
00262   public:
00263     typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
00264   private:
00265     /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
00266     /// the dependence is a read only dependence, false if read/write.
00267     typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair;
00268 
00269     /// BBSkipFirstBlockPair - This pair is used when caching information for a
00270     /// block.  If the pointer is null, the cache value is not a full query that
00271     /// starts at the specified block.  If non-null, the bool indicates whether
00272     /// or not the contents of the block was skipped.
00273     typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
00274 
00275     /// NonLocalPointerInfo - This record is the information kept for each
00276     /// (value, is load) pair.
00277     struct NonLocalPointerInfo {
00278       /// Pair - The pair of the block and the skip-first-block flag.
00279       BBSkipFirstBlockPair Pair;
00280       /// NonLocalDeps - The results of the query for each relevant block.
00281       NonLocalDepInfo NonLocalDeps;
00282       /// Size - The maximum size of the dereferences of the
00283       /// pointer. May be UnknownSize if the sizes are unknown.
00284       uint64_t Size;
00285       /// AATags - The AA tags associated with dereferences of the
00286       /// pointer. The members may be null if there are no tags or
00287       /// conflicting tags.
00288       AAMDNodes AATags;
00289 
00290       NonLocalPointerInfo() : Size(AliasAnalysis::UnknownSize) {}
00291     };
00292 
00293     /// CachedNonLocalPointerInfo - This map stores the cached results of doing
00294     /// a pointer lookup at the bottom of a block.  The key of this map is the
00295     /// pointer+isload bit, the value is a list of <bb->result> mappings.
00296     typedef DenseMap<ValueIsLoadPair,
00297                      NonLocalPointerInfo> CachedNonLocalPointerInfo;
00298     CachedNonLocalPointerInfo NonLocalPointerDeps;
00299 
00300     // A map from instructions to their non-local pointer dependencies.
00301     typedef DenseMap<Instruction*,
00302                      SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
00303     ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
00304 
00305 
00306     /// PerInstNLInfo - This is the instruction we keep for each cached access
00307     /// that we have for an instruction.  The pointer is an owning pointer and
00308     /// the bool indicates whether we have any dirty bits in the set.
00309     typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
00310 
00311     // A map from instructions to their non-local dependencies.
00312     typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
00313 
00314     NonLocalDepMapType NonLocalDeps;
00315 
00316     // A reverse mapping from dependencies to the dependees.  This is
00317     // used when removing instructions to keep the cache coherent.
00318     typedef DenseMap<Instruction*,
00319                      SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
00320     ReverseDepMapType ReverseLocalDeps;
00321 
00322     // A reverse mapping from dependencies to the non-local dependees.
00323     ReverseDepMapType ReverseNonLocalDeps;
00324 
00325     /// Current AA implementation, just a cache.
00326     AliasAnalysis *AA;
00327     const DataLayout *DL;
00328     DominatorTree *DT;
00329     AssumptionTracker *AT;
00330     std::unique_ptr<PredIteratorCache> PredCache;
00331 
00332   public:
00333     MemoryDependenceAnalysis();
00334     ~MemoryDependenceAnalysis();
00335     static char ID;
00336 
00337     /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
00338     bool runOnFunction(Function &) override;
00339 
00340     /// Clean up memory in between runs
00341     void releaseMemory() override;
00342 
00343     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
00344     /// and Alias Analysis.
00345     ///
00346     void getAnalysisUsage(AnalysisUsage &AU) const override;
00347 
00348     /// getDependency - Return the instruction on which a memory operation
00349     /// depends.  See the class comment for more details.  It is illegal to call
00350     /// this on non-memory instructions.
00351     MemDepResult getDependency(Instruction *QueryInst);
00352 
00353     /// getNonLocalCallDependency - Perform a full dependency query for the
00354     /// specified call, returning the set of blocks that the value is
00355     /// potentially live across.  The returned set of results will include a
00356     /// "NonLocal" result for all blocks where the value is live across.
00357     ///
00358     /// This method assumes the instruction returns a "NonLocal" dependency
00359     /// within its own block.
00360     ///
00361     /// This returns a reference to an internal data structure that may be
00362     /// invalidated on the next non-local query or when an instruction is
00363     /// removed.  Clients must copy this data if they want it around longer than
00364     /// that.
00365     const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
00366 
00367 
00368     /// getNonLocalPointerDependency - Perform a full dependency query for an
00369     /// access to the specified (non-volatile) memory location, returning the
00370     /// set of instructions that either define or clobber the value.
00371     ///
00372     /// This method assumes the pointer has a "NonLocal" dependency within BB.
00373     void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc,
00374                                       bool isLoad, BasicBlock *BB,
00375                                     SmallVectorImpl<NonLocalDepResult> &Result);
00376 
00377     /// removeInstruction - Remove an instruction from the dependence analysis,
00378     /// updating the dependence of instructions that previously depended on it.
00379     void removeInstruction(Instruction *InstToRemove);
00380 
00381     /// invalidateCachedPointerInfo - This method is used to invalidate cached
00382     /// information about the specified pointer, because it may be too
00383     /// conservative in memdep.  This is an optional call that can be used when
00384     /// the client detects an equivalence between the pointer and some other
00385     /// value and replaces the other value with ptr. This can make Ptr available
00386     /// in more places that cached info does not necessarily keep.
00387     void invalidateCachedPointerInfo(Value *Ptr);
00388 
00389     /// invalidateCachedPredecessors - Clear the PredIteratorCache info.
00390     /// This needs to be done when the CFG changes, e.g., due to splitting
00391     /// critical edges.
00392     void invalidateCachedPredecessors();
00393 
00394     /// getPointerDependencyFrom - Return the instruction on which a memory
00395     /// location depends.  If isLoad is true, this routine ignores may-aliases
00396     /// with read-only operations.  If isLoad is false, this routine ignores
00397     /// may-aliases with reads from read-only locations. If possible, pass
00398     /// the query instruction as well; this function may take advantage of 
00399     /// the metadata annotated to the query instruction to refine the result.
00400     ///
00401     /// Note that this is an uncached query, and thus may be inefficient.
00402     ///
00403     MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc,
00404                                           bool isLoad,
00405                                           BasicBlock::iterator ScanIt,
00406                                           BasicBlock *BB,
00407                                           Instruction *QueryInst = nullptr);
00408 
00409 
00410     /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that
00411     /// looks at a memory location for a load (specified by MemLocBase, Offs,
00412     /// and Size) and compares it against a load.  If the specified load could
00413     /// be safely widened to a larger integer load that is 1) still efficient,
00414     /// 2) safe for the target, and 3) would provide the specified memory
00415     /// location value, then this function returns the size in bytes of the
00416     /// load width to use.  If not, this returns zero.
00417     static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase,
00418                                                     int64_t MemLocOffs,
00419                                                     unsigned MemLocSize,
00420                                                     const LoadInst *LI,
00421                                                     const DataLayout &DL);
00422 
00423   private:
00424     MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
00425                                            BasicBlock::iterator ScanIt,
00426                                            BasicBlock *BB);
00427     bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
00428                                      const AliasAnalysis::Location &Loc,
00429                                      bool isLoad, BasicBlock *BB,
00430                                      SmallVectorImpl<NonLocalDepResult> &Result,
00431                                      DenseMap<BasicBlock*, Value*> &Visited,
00432                                      bool SkipFirstBlock = false);
00433     MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
00434                                          bool isLoad, BasicBlock *BB,
00435                                          NonLocalDepInfo *Cache,
00436                                          unsigned NumSortedEntries);
00437 
00438     void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
00439 
00440     /// verifyRemoved - Verify that the specified instruction does not occur
00441     /// in our internal data structures.
00442     void verifyRemoved(Instruction *Inst) const;
00443 
00444   };
00445 
00446 } // End llvm namespace
00447 
00448 #endif