LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

AliasSetTracker.h

Go to the documentation of this file.
00001 //===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- 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 two classes: AliasSetTracker and AliasSet.  These interface
00011 // are used to classify a collection of pointer references into a maximal number
00012 // of disjoint sets.  Each AliasSet object constructed by the AliasSetTracker
00013 // object refers to memory disjoint from the other sets.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
00018 #define LLVM_ANALYSIS_ALIASSETTRACKER_H
00019 
00020 #include "llvm/Support/CallSite.h"
00021 #include "llvm/Support/Streams.h"
00022 #include "llvm/ADT/iterator.h"
00023 #include "llvm/ADT/hash_map.h"
00024 #include "llvm/ADT/ilist.h"
00025 #include "llvm/ADT/ilist_node.h"
00026 
00027 namespace llvm {
00028 
00029 class AliasAnalysis;
00030 class LoadInst;
00031 class StoreInst;
00032 class FreeInst;
00033 class VAArgInst;
00034 class AliasSetTracker;
00035 class AliasSet;
00036 
00037 class AliasSet : public ilist_node<AliasSet> {
00038   friend class AliasSetTracker;
00039 
00040   class PointerRec;
00041   typedef std::pair<Value* const, PointerRec> HashNodePair;
00042 
00043   class PointerRec {
00044     HashNodePair **PrevInList, *NextInList;
00045     AliasSet *AS;
00046     unsigned Size;
00047   public:
00048     PointerRec() : PrevInList(0), NextInList(0), AS(0), Size(0) {}
00049 
00050     HashNodePair *getNext() const { return NextInList; }
00051     bool hasAliasSet() const { return AS != 0; }
00052 
00053     HashNodePair** setPrevInList(HashNodePair **PIL) {
00054       PrevInList = PIL;
00055       return &NextInList;
00056     }
00057 
00058     void updateSize(unsigned NewSize) {
00059       if (NewSize > Size) Size = NewSize;
00060     }
00061 
00062     unsigned getSize() const { return Size; }
00063 
00064     AliasSet *getAliasSet(AliasSetTracker &AST) {
00065       assert(AS && "No AliasSet yet!");
00066       if (AS->Forward) {
00067         AliasSet *OldAS = AS;
00068         AS = OldAS->getForwardedTarget(AST);
00069         AS->addRef();
00070         OldAS->dropRef(AST);
00071       }
00072       return AS;
00073     }
00074 
00075     void setAliasSet(AliasSet *as) {
00076       assert(AS == 0 && "Already have an alias set!");
00077       AS = as;
00078     }
00079 
00080     void removeFromList() {
00081       if (NextInList) NextInList->second.PrevInList = PrevInList;
00082       *PrevInList = NextInList;
00083       if (AS->PtrListEnd == &NextInList) {
00084         AS->PtrListEnd = PrevInList;
00085         assert(*AS->PtrListEnd == 0 && "List not terminated right!");
00086       }
00087     }
00088   };
00089 
00090   HashNodePair *PtrList, **PtrListEnd;  // Doubly linked list of nodes
00091   AliasSet *Forward;             // Forwarding pointer
00092   AliasSet *Next, *Prev;         // Doubly linked list of AliasSets
00093 
00094   std::vector<CallSite> CallSites; // All calls & invokes in this node
00095 
00096   // RefCount - Number of nodes pointing to this AliasSet plus the number of
00097   // AliasSets forwarding to it.
00098   unsigned RefCount : 28;
00099 
00100   /// AccessType - Keep track of whether this alias set merely refers to the
00101   /// locations of memory, whether it modifies the memory, or whether it does
00102   /// both.  The lattice goes from "NoModRef" to either Refs or Mods, then to
00103   /// ModRef as necessary.
00104   ///
00105   enum AccessType {
00106     NoModRef = 0, Refs = 1,         // Ref = bit 1
00107     Mods     = 2, ModRef = 3        // Mod = bit 2
00108   };
00109   unsigned AccessTy : 2;
00110 
00111   /// AliasType - Keep track the relationships between the pointers in the set.
00112   /// Lattice goes from MustAlias to MayAlias.
00113   ///
00114   enum AliasType {
00115     MustAlias = 0, MayAlias = 1
00116   };
00117   unsigned AliasTy : 1;
00118 
00119   // Volatile - True if this alias set contains volatile loads or stores.
00120   bool Volatile : 1;
00121 
00122   void addRef() { ++RefCount; }
00123   void dropRef(AliasSetTracker &AST) {
00124     assert(RefCount >= 1 && "Invalid reference count detected!");
00125     if (--RefCount == 0)
00126       removeFromTracker(AST);
00127   }
00128 
00129 public:
00130   /// Accessors...
00131   bool isRef() const { return AccessTy & Refs; }
00132   bool isMod() const { return AccessTy & Mods; }
00133   bool isMustAlias() const { return AliasTy == MustAlias; }
00134   bool isMayAlias()  const { return AliasTy == MayAlias; }
00135 
00136   // isVolatile - Return true if this alias set contains volatile loads or
00137   // stores.
00138   bool isVolatile() const { return Volatile; }
00139 
00140   /// isForwardingAliasSet - Return true if this alias set should be ignored as
00141   /// part of the AliasSetTracker object.
00142   bool isForwardingAliasSet() const { return Forward; }
00143 
00144   /// mergeSetIn - Merge the specified alias set into this alias set...
00145   ///
00146   void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
00147 
00148   // Alias Set iteration - Allow access to all of the pointer which are part of
00149   // this alias set...
00150   class iterator;
00151   iterator begin() const { return iterator(PtrList); }
00152   iterator end()   const { return iterator(); }
00153   bool empty() const { return PtrList == 0; }
00154 
00155   void print(std::ostream &OS) const;
00156   void print(std::ostream *OS) const { if (OS) print(*OS); }
00157   void dump() const;
00158 
00159   /// Define an iterator for alias sets... this is just a forward iterator.
00160   class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
00161     HashNodePair *CurNode;
00162   public:
00163     explicit iterator(HashNodePair *CN = 0) : CurNode(CN) {}
00164 
00165     bool operator==(const iterator& x) const {
00166       return CurNode == x.CurNode;
00167     }
00168     bool operator!=(const iterator& x) const { return !operator==(x); }
00169 
00170     const iterator &operator=(const iterator &I) {
00171       CurNode = I.CurNode;
00172       return *this;
00173     }
00174 
00175     value_type &operator*() const {
00176       assert(CurNode && "Dereferencing AliasSet.end()!");
00177       return *CurNode;
00178     }
00179     value_type *operator->() const { return &operator*(); }
00180 
00181     Value *getPointer() const { return CurNode->first; }
00182     unsigned getSize() const { return CurNode->second.getSize(); }
00183 
00184     iterator& operator++() {                // Preincrement
00185       assert(CurNode && "Advancing past AliasSet.end()!");
00186       CurNode = CurNode->second.getNext();
00187       return *this;
00188     }
00189     iterator operator++(int) { // Postincrement
00190       iterator tmp = *this; ++*this; return tmp;
00191     }
00192   };
00193 
00194 private:
00195   // Can only be created by AliasSetTracker. Also, ilist creates one
00196   // to serve as a sentinel.
00197   friend struct ilist_sentinel_traits<AliasSet>;
00198   AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
00199                AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
00200   }
00201 
00202   AliasSet(const AliasSet &AS);        // do not implement
00203   void operator=(const AliasSet &AS);  // do not implement
00204 
00205   HashNodePair *getSomePointer() const {
00206     return PtrList;
00207   }
00208 
00209   /// getForwardedTarget - Return the real alias set this represents.  If this
00210   /// has been merged with another set and is forwarding, return the ultimate
00211   /// destination set.  This also implements the union-find collapsing as well.
00212   AliasSet *getForwardedTarget(AliasSetTracker &AST) {
00213     if (!Forward) return this;
00214 
00215     AliasSet *Dest = Forward->getForwardedTarget(AST);
00216     if (Dest != Forward) {
00217       Dest->addRef();
00218       Forward->dropRef(AST);
00219       Forward = Dest;
00220     }
00221     return Dest;
00222   }
00223 
00224   void removeFromTracker(AliasSetTracker &AST);
00225 
00226   void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size,
00227                   bool KnownMustAlias = false);
00228   void addCallSite(CallSite CS, AliasAnalysis &AA);
00229   void removeCallSite(CallSite CS) {
00230     for (size_t i = 0, e = CallSites.size(); i != e; ++i)
00231       if (CallSites[i].getInstruction() == CS.getInstruction()) {
00232         CallSites[i] = CallSites.back();
00233         CallSites.pop_back();
00234       }
00235   }
00236   void setVolatile() { Volatile = true; }
00237 
00238   /// aliasesPointer - Return true if the specified pointer "may" (or must)
00239   /// alias one of the members in the set.
00240   ///
00241   bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
00242   bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
00243 };
00244 
00245 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
00246   AS.print(OS);
00247   return OS;
00248 }
00249 
00250 
00251 class AliasSetTracker {
00252   AliasAnalysis &AA;
00253   ilist<AliasSet> AliasSets;
00254 
00255   // Map from pointers to their node
00256   hash_map<Value*, AliasSet::PointerRec> PointerMap;
00257 public:
00258   /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
00259   /// the specified alias analysis object to disambiguate load and store
00260   /// addresses.
00261   explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
00262   ~AliasSetTracker() { clear(); }
00263 
00264   /// add methods - These methods are used to add different types of
00265   /// instructions to the alias sets.  Adding a new instruction can result in
00266   /// one of three actions happening:
00267   ///
00268   ///   1. If the instruction doesn't alias any other sets, create a new set.
00269   ///   2. If the instruction aliases exactly one set, add it to the set
00270   ///   3. If the instruction aliases multiple sets, merge the sets, and add
00271   ///      the instruction to the result.
00272   ///
00273   /// These methods return true if inserting the instruction resulted in the
00274   /// addition of a new alias set (i.e., the pointer did not alias anything).
00275   ///
00276   bool add(Value *Ptr, unsigned Size);  // Add a location
00277   bool add(LoadInst *LI);
00278   bool add(StoreInst *SI);
00279   bool add(FreeInst *FI);
00280   bool add(VAArgInst *VAAI);
00281   bool add(CallSite CS);          // Call/Invoke instructions
00282   bool add(CallInst *CI)   { return add(CallSite(CI)); }
00283   bool add(InvokeInst *II) { return add(CallSite(II)); }
00284   bool add(Instruction *I);       // Dispatch to one of the other add methods...
00285   void add(BasicBlock &BB);       // Add all instructions in basic block
00286   void add(const AliasSetTracker &AST); // Add alias relations from another AST
00287 
00288   /// remove methods - These methods are used to remove all entries that might
00289   /// be aliased by the specified instruction.  These methods return true if any
00290   /// alias sets were eliminated.
00291   bool remove(Value *Ptr, unsigned Size);  // Remove a location
00292   bool remove(LoadInst *LI);
00293   bool remove(StoreInst *SI);
00294   bool remove(FreeInst *FI);
00295   bool remove(VAArgInst *VAAI);
00296   bool remove(CallSite CS);
00297   bool remove(CallInst *CI)   { return remove(CallSite(CI)); }
00298   bool remove(InvokeInst *II) { return remove(CallSite(II)); }
00299   bool remove(Instruction *I);
00300   void remove(AliasSet &AS);
00301   
00302   void clear() {
00303     PointerMap.clear();
00304     AliasSets.clear();
00305   }
00306 
00307   /// getAliasSets - Return the alias sets that are active.
00308   ///
00309   const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
00310 
00311   /// getAliasSetForPointer - Return the alias set that the specified pointer
00312   /// lives in.  If the New argument is non-null, this method sets the value to
00313   /// true if a new alias set is created to contain the pointer (because the
00314   /// pointer didn't alias anything).
00315   AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
00316 
00317   /// getAliasSetForPointerIfExists - Return the alias set containing the
00318   /// location specified if one exists, otherwise return null.
00319   AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
00320     return findAliasSetForPointer(P, Size);
00321   }
00322 
00323   /// containsPointer - Return true if the specified location is represented by
00324   /// this alias set, false otherwise.  This does not modify the AST object or
00325   /// alias sets.
00326   bool containsPointer(Value *P, unsigned Size) const;
00327 
00328   /// getAliasAnalysis - Return the underlying alias analysis object used by
00329   /// this tracker.
00330   AliasAnalysis &getAliasAnalysis() const { return AA; }
00331 
00332   /// deleteValue method - This method is used to remove a pointer value from
00333   /// the AliasSetTracker entirely.  It should be used when an instruction is
00334   /// deleted from the program to update the AST.  If you don't use this, you
00335   /// would have dangling pointers to deleted instructions.
00336   ///
00337   void deleteValue(Value *PtrVal);
00338 
00339   /// copyValue - This method should be used whenever a preexisting value in the
00340   /// program is copied or cloned, introducing a new value.  Note that it is ok
00341   /// for clients that use this method to introduce the same value multiple
00342   /// times: if the tracker already knows about a value, it will ignore the
00343   /// request.
00344   ///
00345   void copyValue(Value *From, Value *To);
00346 
00347 
00348   typedef ilist<AliasSet>::iterator iterator;
00349   typedef ilist<AliasSet>::const_iterator const_iterator;
00350 
00351   const_iterator begin() const { return AliasSets.begin(); }
00352   const_iterator end()   const { return AliasSets.end(); }
00353 
00354   iterator begin() { return AliasSets.begin(); }
00355   iterator end()   { return AliasSets.end(); }
00356 
00357   void print(std::ostream &OS) const;
00358   void print(std::ostream *OS) const { if (OS) print(*OS); }
00359   void dump() const;
00360 
00361 private:
00362   friend class AliasSet;
00363   void removeAliasSet(AliasSet *AS);
00364 
00365   AliasSet::HashNodePair &getEntryFor(Value *V) {
00366     // Standard operator[], except that it returns the whole pair, not just
00367     // ->second.
00368     return *PointerMap.insert(AliasSet::HashNodePair(V,
00369                                             AliasSet::PointerRec())).first;
00370   }
00371 
00372   AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
00373                        bool &NewSet) {
00374     NewSet = false;
00375     AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
00376     AS.AccessTy |= E;
00377     return AS;
00378   }
00379   AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
00380 
00381   AliasSet *findAliasSetForCallSite(CallSite CS);
00382 };
00383 
00384 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
00385   AST.print(OS);
00386   return OS;
00387 }
00388 
00389 } // End llvm namespace
00390 
00391 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.