Home | History | Annotate | Download | only in Analysis
      1 //===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps  --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the MemoryDependenceAnalysis analysis pass.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
     15 #define LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
     16 
     17 #include "llvm/ADT/DenseMap.h"
     18 #include "llvm/ADT/PointerIntPair.h"
     19 #include "llvm/ADT/SmallPtrSet.h"
     20 #include "llvm/Analysis/AliasAnalysis.h"
     21 #include "llvm/IR/BasicBlock.h"
     22 #include "llvm/IR/ValueHandle.h"
     23 #include "llvm/Pass.h"
     24 
     25 namespace llvm {
     26   class Function;
     27   class FunctionPass;
     28   class Instruction;
     29   class CallSite;
     30   class AliasAnalysis;
     31   class DataLayout;
     32   class MemoryDependenceAnalysis;
     33   class PredIteratorCache;
     34   class DominatorTree;
     35   class PHITransAddr;
     36 
     37   /// MemDepResult - A memory dependence query can return one of three different
     38   /// answers, described below.
     39   class MemDepResult {
     40     enum DepType {
     41       /// Invalid - Clients of MemDep never see this.
     42       Invalid = 0,
     43 
     44       /// Clobber - This is a dependence on the specified instruction which
     45       /// clobbers the desired value.  The pointer member of the MemDepResult
     46       /// pair holds the instruction that clobbers the memory.  For example,
     47       /// this occurs when we see a may-aliased store to the memory location we
     48       /// care about.
     49       ///
     50       /// There are several cases that may be interesting here:
     51       ///   1. Loads are clobbered by may-alias stores.
     52       ///   2. Loads are considered clobbered by partially-aliased loads.  The
     53       ///      client may choose to analyze deeper into these cases.
     54       Clobber,
     55 
     56       /// Def - This is a dependence on the specified instruction which
     57       /// defines/produces the desired memory location.  The pointer member of
     58       /// the MemDepResult pair holds the instruction that defines the memory.
     59       /// Cases of interest:
     60       ///   1. This could be a load or store for dependence queries on
     61       ///      load/store.  The value loaded or stored is the produced value.
     62       ///      Note that the pointer operand may be different than that of the
     63       ///      queried pointer due to must aliases and phi translation.  Note
     64       ///      that the def may not be the same type as the query, the pointers
     65       ///      may just be must aliases.
     66       ///   2. For loads and stores, this could be an allocation instruction. In
     67       ///      this case, the load is loading an undef value or a store is the
     68       ///      first store to (that part of) the allocation.
     69       ///   3. Dependence queries on calls return Def only when they are
     70       ///      readonly calls or memory use intrinsics with identical callees
     71       ///      and no intervening clobbers.  No validation is done that the
     72       ///      operands to the calls are the same.
     73       Def,
     74 
     75       /// Other - This marker indicates that the query has no known dependency
     76       /// in the specified block.  More detailed state info is encoded in the
     77       /// upper part of the pair (i.e. the Instruction*)
     78       Other
     79     };
     80     /// If DepType is "Other", the upper part of the pair
     81     /// (i.e. the Instruction* part) is instead used to encode more detailed
     82     /// type information as follows
     83     enum OtherType {
     84       /// NonLocal - This marker indicates that the query has no dependency in
     85       /// the specified block.  To find out more, the client should query other
     86       /// predecessor blocks.
     87       NonLocal = 0x4,
     88       /// NonFuncLocal - This marker indicates that the query has no
     89       /// dependency in the specified function.
     90       NonFuncLocal = 0x8,
     91       /// Unknown - This marker indicates that the query dependency
     92       /// is unknown.
     93       Unknown = 0xc
     94     };
     95 
     96     typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
     97     PairTy Value;
     98     explicit MemDepResult(PairTy V) : Value(V) {}
     99   public:
    100     MemDepResult() : Value(nullptr, Invalid) {}
    101 
    102     /// get methods: These are static ctor methods for creating various
    103     /// MemDepResult kinds.
    104     static MemDepResult getDef(Instruction *Inst) {
    105       assert(Inst && "Def requires inst");
    106       return MemDepResult(PairTy(Inst, Def));
    107     }
    108     static MemDepResult getClobber(Instruction *Inst) {
    109       assert(Inst && "Clobber requires inst");
    110       return MemDepResult(PairTy(Inst, Clobber));
    111     }
    112     static MemDepResult getNonLocal() {
    113       return MemDepResult(
    114         PairTy(reinterpret_cast<Instruction*>(NonLocal), Other));
    115     }
    116     static MemDepResult getNonFuncLocal() {
    117       return MemDepResult(
    118         PairTy(reinterpret_cast<Instruction*>(NonFuncLocal), Other));
    119     }
    120     static MemDepResult getUnknown() {
    121       return MemDepResult(
    122         PairTy(reinterpret_cast<Instruction*>(Unknown), Other));
    123     }
    124 
    125     /// isClobber - Return true if this MemDepResult represents a query that is
    126     /// an instruction clobber dependency.
    127     bool isClobber() const { return Value.getInt() == Clobber; }
    128 
    129     /// isDef - Return true if this MemDepResult represents a query that is
    130     /// an instruction definition dependency.
    131     bool isDef() const { return Value.getInt() == Def; }
    132 
    133     /// isNonLocal - Return true if this MemDepResult represents a query that
    134     /// is transparent to the start of the block, but where a non-local hasn't
    135     /// been done.
    136     bool isNonLocal() const {
    137       return Value.getInt() == Other
    138         && Value.getPointer() == reinterpret_cast<Instruction*>(NonLocal);
    139     }
    140 
    141     /// isNonFuncLocal - Return true if this MemDepResult represents a query
    142     /// that is transparent to the start of the function.
    143     bool isNonFuncLocal() const {
    144       return Value.getInt() == Other
    145         && Value.getPointer() == reinterpret_cast<Instruction*>(NonFuncLocal);
    146     }
    147 
    148     /// isUnknown - Return true if this MemDepResult represents a query which
    149     /// cannot and/or will not be computed.
    150     bool isUnknown() const {
    151       return Value.getInt() == Other
    152         && Value.getPointer() == reinterpret_cast<Instruction*>(Unknown);
    153     }
    154 
    155     /// getInst() - If this is a normal dependency, return the instruction that
    156     /// is depended on.  Otherwise, return null.
    157     Instruction *getInst() const {
    158       if (Value.getInt() == Other) return nullptr;
    159       return Value.getPointer();
    160     }
    161 
    162     bool operator==(const MemDepResult &M) const { return Value == M.Value; }
    163     bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
    164     bool operator<(const MemDepResult &M) const { return Value < M.Value; }
    165     bool operator>(const MemDepResult &M) const { return Value > M.Value; }
    166   private:
    167     friend class MemoryDependenceAnalysis;
    168     /// Dirty - Entries with this marker occur in a LocalDeps map or
    169     /// NonLocalDeps map when the instruction they previously referenced was
    170     /// removed from MemDep.  In either case, the entry may include an
    171     /// instruction pointer.  If so, the pointer is an instruction in the
    172     /// block where scanning can start from, saving some work.
    173     ///
    174     /// In a default-constructed MemDepResult object, the type will be Dirty
    175     /// and the instruction pointer will be null.
    176     ///
    177 
    178     /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
    179     /// state.
    180     bool isDirty() const { return Value.getInt() == Invalid; }
    181 
    182     static MemDepResult getDirty(Instruction *Inst) {
    183       return MemDepResult(PairTy(Inst, Invalid));
    184     }
    185   };
    186 
    187   /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache.  For
    188   /// each BasicBlock (the BB entry) it keeps a MemDepResult.
    189   class NonLocalDepEntry {
    190     BasicBlock *BB;
    191     MemDepResult Result;
    192   public:
    193     NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
    194       : BB(bb), Result(result) {}
    195 
    196     // This is used for searches.
    197     NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
    198 
    199     // BB is the sort key, it can't be changed.
    200     BasicBlock *getBB() const { return BB; }
    201 
    202     void setResult(const MemDepResult &R) { Result = R; }
    203 
    204     const MemDepResult &getResult() const { return Result; }
    205 
    206     bool operator<(const NonLocalDepEntry &RHS) const {
    207       return BB < RHS.BB;
    208     }
    209   };
    210 
    211   /// NonLocalDepResult - This is a result from a NonLocal dependence query.
    212   /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
    213   /// (potentially phi translated) address that was live in the block.
    214   class NonLocalDepResult {
    215     NonLocalDepEntry Entry;
    216     Value *Address;
    217   public:
    218     NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
    219       : Entry(bb, result), Address(address) {}
    220 
    221     // BB is the sort key, it can't be changed.
    222     BasicBlock *getBB() const { return Entry.getBB(); }
    223 
    224     void setResult(const MemDepResult &R, Value *Addr) {
    225       Entry.setResult(R);
    226       Address = Addr;
    227     }
    228 
    229     const MemDepResult &getResult() const { return Entry.getResult(); }
    230 
    231     /// getAddress - Return the address of this pointer in this block.  This can
    232     /// be different than the address queried for the non-local result because
    233     /// of phi translation.  This returns null if the address was not available
    234     /// in a block (i.e. because phi translation failed) or if this is a cached
    235     /// result and that address was deleted.
    236     ///
    237     /// The address is always null for a non-local 'call' dependence.
    238     Value *getAddress() const { return Address; }
    239   };
    240 
    241   /// MemoryDependenceAnalysis - This is an analysis that determines, for a
    242   /// given memory operation, what preceding memory operations it depends on.
    243   /// It builds on alias analysis information, and tries to provide a lazy,
    244   /// caching interface to a common kind of alias information query.
    245   ///
    246   /// The dependency information returned is somewhat unusual, but is pragmatic.
    247   /// If queried about a store or call that might modify memory, the analysis
    248   /// will return the instruction[s] that may either load from that memory or
    249   /// store to it.  If queried with a load or call that can never modify memory,
    250   /// the analysis will return calls and stores that might modify the pointer,
    251   /// but generally does not return loads unless a) they are volatile, or
    252   /// b) they load from *must-aliased* pointers.  Returning a dependence on
    253   /// must-alias'd pointers instead of all pointers interacts well with the
    254   /// internal caching mechanism.
    255   ///
    256   class MemoryDependenceAnalysis : public FunctionPass {
    257     // A map from instructions to their dependency.
    258     typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
    259     LocalDepMapType LocalDeps;
    260 
    261   public:
    262     typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
    263   private:
    264     /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
    265     /// the dependence is a read only dependence, false if read/write.
    266     typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair;
    267 
    268     /// BBSkipFirstBlockPair - This pair is used when caching information for a
    269     /// block.  If the pointer is null, the cache value is not a full query that
    270     /// starts at the specified block.  If non-null, the bool indicates whether
    271     /// or not the contents of the block was skipped.
    272     typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
    273 
    274     /// NonLocalPointerInfo - This record is the information kept for each
    275     /// (value, is load) pair.
    276     struct NonLocalPointerInfo {
    277       /// Pair - The pair of the block and the skip-first-block flag.
    278       BBSkipFirstBlockPair Pair;
    279       /// NonLocalDeps - The results of the query for each relevant block.
    280       NonLocalDepInfo NonLocalDeps;
    281       /// Size - The maximum size of the dereferences of the
    282       /// pointer. May be UnknownSize if the sizes are unknown.
    283       uint64_t Size;
    284       /// TBAATag - The TBAA tag associated with dereferences of the
    285       /// pointer. May be null if there are no tags or conflicting tags.
    286       const MDNode *TBAATag;
    287 
    288       NonLocalPointerInfo()
    289         : Size(AliasAnalysis::UnknownSize), TBAATag(nullptr) {}
    290     };
    291 
    292     /// CachedNonLocalPointerInfo - This map stores the cached results of doing
    293     /// a pointer lookup at the bottom of a block.  The key of this map is the
    294     /// pointer+isload bit, the value is a list of <bb->result> mappings.
    295     typedef DenseMap<ValueIsLoadPair,
    296                      NonLocalPointerInfo> CachedNonLocalPointerInfo;
    297     CachedNonLocalPointerInfo NonLocalPointerDeps;
    298 
    299     // A map from instructions to their non-local pointer dependencies.
    300     typedef DenseMap<Instruction*,
    301                      SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
    302     ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
    303 
    304 
    305     /// PerInstNLInfo - This is the instruction we keep for each cached access
    306     /// that we have for an instruction.  The pointer is an owning pointer and
    307     /// the bool indicates whether we have any dirty bits in the set.
    308     typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
    309 
    310     // A map from instructions to their non-local dependencies.
    311     typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
    312 
    313     NonLocalDepMapType NonLocalDeps;
    314 
    315     // A reverse mapping from dependencies to the dependees.  This is
    316     // used when removing instructions to keep the cache coherent.
    317     typedef DenseMap<Instruction*,
    318                      SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
    319     ReverseDepMapType ReverseLocalDeps;
    320 
    321     // A reverse mapping from dependencies to the non-local dependees.
    322     ReverseDepMapType ReverseNonLocalDeps;
    323 
    324     /// Current AA implementation, just a cache.
    325     AliasAnalysis *AA;
    326     const DataLayout *DL;
    327     DominatorTree *DT;
    328     std::unique_ptr<PredIteratorCache> PredCache;
    329 
    330   public:
    331     MemoryDependenceAnalysis();
    332     ~MemoryDependenceAnalysis();
    333     static char ID;
    334 
    335     /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
    336     bool runOnFunction(Function &) override;
    337 
    338     /// Clean up memory in between runs
    339     void releaseMemory() override;
    340 
    341     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
    342     /// and Alias Analysis.
    343     ///
    344     void getAnalysisUsage(AnalysisUsage &AU) const override;
    345 
    346     /// getDependency - Return the instruction on which a memory operation
    347     /// depends.  See the class comment for more details.  It is illegal to call
    348     /// this on non-memory instructions.
    349     MemDepResult getDependency(Instruction *QueryInst);
    350 
    351     /// getNonLocalCallDependency - Perform a full dependency query for the
    352     /// specified call, returning the set of blocks that the value is
    353     /// potentially live across.  The returned set of results will include a
    354     /// "NonLocal" result for all blocks where the value is live across.
    355     ///
    356     /// This method assumes the instruction returns a "NonLocal" dependency
    357     /// within its own block.
    358     ///
    359     /// This returns a reference to an internal data structure that may be
    360     /// invalidated on the next non-local query or when an instruction is
    361     /// removed.  Clients must copy this data if they want it around longer than
    362     /// that.
    363     const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
    364 
    365 
    366     /// getNonLocalPointerDependency - Perform a full dependency query for an
    367     /// access to the specified (non-volatile) memory location, returning the
    368     /// set of instructions that either define or clobber the value.
    369     ///
    370     /// This method assumes the pointer has a "NonLocal" dependency within BB.
    371     void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc,
    372                                       bool isLoad, BasicBlock *BB,
    373                                     SmallVectorImpl<NonLocalDepResult> &Result);
    374 
    375     /// removeInstruction - Remove an instruction from the dependence analysis,
    376     /// updating the dependence of instructions that previously depended on it.
    377     void removeInstruction(Instruction *InstToRemove);
    378 
    379     /// invalidateCachedPointerInfo - This method is used to invalidate cached
    380     /// information about the specified pointer, because it may be too
    381     /// conservative in memdep.  This is an optional call that can be used when
    382     /// the client detects an equivalence between the pointer and some other
    383     /// value and replaces the other value with ptr. This can make Ptr available
    384     /// in more places that cached info does not necessarily keep.
    385     void invalidateCachedPointerInfo(Value *Ptr);
    386 
    387     /// invalidateCachedPredecessors - Clear the PredIteratorCache info.
    388     /// This needs to be done when the CFG changes, e.g., due to splitting
    389     /// critical edges.
    390     void invalidateCachedPredecessors();
    391 
    392     /// getPointerDependencyFrom - Return the instruction on which a memory
    393     /// location depends.  If isLoad is true, this routine ignores may-aliases
    394     /// with read-only operations.  If isLoad is false, this routine ignores
    395     /// may-aliases with reads from read-only locations. If possible, pass
    396     /// the query instruction as well; this function may take advantage of
    397     /// the metadata annotated to the query instruction to refine the result.
    398     ///
    399     /// Note that this is an uncached query, and thus may be inefficient.
    400     ///
    401     MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc,
    402                                           bool isLoad,
    403                                           BasicBlock::iterator ScanIt,
    404                                           BasicBlock *BB,
    405                                           Instruction *QueryInst = nullptr);
    406 
    407 
    408     /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that
    409     /// looks at a memory location for a load (specified by MemLocBase, Offs,
    410     /// and Size) and compares it against a load.  If the specified load could
    411     /// be safely widened to a larger integer load that is 1) still efficient,
    412     /// 2) safe for the target, and 3) would provide the specified memory
    413     /// location value, then this function returns the size in bytes of the
    414     /// load width to use.  If not, this returns zero.
    415     static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase,
    416                                                     int64_t MemLocOffs,
    417                                                     unsigned MemLocSize,
    418                                                     const LoadInst *LI,
    419                                                     const DataLayout &DL);
    420 
    421   private:
    422     MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
    423                                            BasicBlock::iterator ScanIt,
    424                                            BasicBlock *BB);
    425     bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
    426                                      const AliasAnalysis::Location &Loc,
    427                                      bool isLoad, BasicBlock *BB,
    428                                      SmallVectorImpl<NonLocalDepResult> &Result,
    429                                      DenseMap<BasicBlock*, Value*> &Visited,
    430                                      bool SkipFirstBlock = false);
    431     MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
    432                                          bool isLoad, BasicBlock *BB,
    433                                          NonLocalDepInfo *Cache,
    434                                          unsigned NumSortedEntries);
    435 
    436     void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
    437 
    438     /// verifyRemoved - Verify that the specified instruction does not occur
    439     /// in our internal data structures.
    440     void verifyRemoved(Instruction *Inst) const;
    441 
    442   };
    443 
    444 } // End llvm namespace
    445 
    446 #endif
    447