Add an AliasSetTracker::copyValue method
[oota-llvm.git] / include / llvm / Analysis / AliasSetTracker.h
1 //===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines two classes: AliasSetTracker and AliasSet.  These interface
11 // are used to classify a collection of pointer references into a maximal number
12 // of disjoint sets.  Each AliasSet object constructed by the AliasSetTracker
13 // object refers to memory disjoint from the other sets.
14 // 
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
18 #define LLVM_ANALYSIS_ALIASSETTRACKER_H
19
20 #include "llvm/Support/CallSite.h"
21 #include "llvm/ADT/iterator"
22 #include "llvm/ADT/hash_map"
23 #include "llvm/ADT/ilist"
24
25 namespace llvm {
26
27 class AliasAnalysis;
28 class LoadInst;
29 class StoreInst;
30 class FreeInst;
31 class AliasSetTracker;
32 class AliasSet;
33
34 class AliasSet {
35   friend class AliasSetTracker;
36
37   struct PointerRec;
38   typedef std::pair<Value* const, PointerRec> HashNodePair;
39
40   class PointerRec {
41     HashNodePair **PrevInList, *NextInList;
42     AliasSet *AS;
43     unsigned Size;
44   public:
45     PointerRec() : PrevInList(0), NextInList(0), AS(0), Size(0) {}
46
47     HashNodePair *getNext() const { return NextInList; }
48     bool hasAliasSet() const { return AS != 0; }
49
50     HashNodePair** setPrevInList(HashNodePair **PIL) {
51       PrevInList = PIL;
52       return &NextInList;
53     }
54
55     void updateSize(unsigned NewSize) {
56       if (NewSize > Size) Size = NewSize;
57     }
58
59     unsigned getSize() const { return Size; }
60
61     AliasSet *getAliasSet(AliasSetTracker &AST) { 
62       assert(AS && "No AliasSet yet!");
63       if (AS->Forward) {
64         AliasSet *OldAS = AS;
65         AS = OldAS->getForwardedTarget(AST);
66         AS->addRef();
67         OldAS->dropRef(AST);
68       }
69       return AS;
70     }
71
72     void setAliasSet(AliasSet *as) {
73       assert(AS == 0 && "Already have an alias set!");
74       AS = as;
75     }
76
77     void removeFromList() {
78       if (NextInList) NextInList->second.PrevInList = PrevInList;
79       *PrevInList = NextInList;
80     }
81   };
82
83   HashNodePair *PtrList, **PtrListEnd;  // Doubly linked list of nodes
84   AliasSet *Forward;             // Forwarding pointer
85   AliasSet *Next, *Prev;         // Doubly linked list of AliasSets
86
87   std::vector<CallSite> CallSites; // All calls & invokes in this node
88
89   // RefCount - Number of nodes pointing to this AliasSet plus the number of
90   // AliasSets forwarding to it.
91   unsigned RefCount : 28;
92
93   /// AccessType - Keep track of whether this alias set merely refers to the
94   /// locations of memory, whether it modifies the memory, or whether it does
95   /// both.  The lattice goes from "NoModRef" to either Refs or Mods, then to
96   /// ModRef as necessary.
97   ///
98   enum AccessType {
99     NoModRef = 0, Refs = 1,         // Ref = bit 1
100     Mods     = 2, ModRef = 3        // Mod = bit 2
101   };
102   unsigned AccessTy : 2;
103
104   /// AliasType - Keep track the relationships between the pointers in the set.
105   /// Lattice goes from MustAlias to MayAlias.
106   ///
107   enum AliasType {
108     MustAlias = 0, MayAlias = 1
109   };
110   unsigned AliasTy : 1;
111
112   // Volatile - True if this alias set contains volatile loads or stores.
113   bool Volatile : 1;
114
115   friend class ilist_traits<AliasSet>;
116   AliasSet *getPrev() const { return Prev; }
117   AliasSet *getNext() const { return Next; }
118   void setPrev(AliasSet *P) { Prev = P; }
119   void setNext(AliasSet *N) { Next = N; }
120
121   void addRef() { ++RefCount; }
122   void dropRef(AliasSetTracker &AST) {
123     assert(RefCount >= 1 && "Invalid reference count detected!");
124     if (--RefCount == 0)
125       removeFromTracker(AST);
126   }
127
128 public:
129   /// Accessors...
130   bool isRef() const { return AccessTy & Refs; }
131   bool isMod() const { return AccessTy & Mods; }
132   bool isMustAlias() const { return AliasTy == MustAlias; }
133   bool isMayAlias()  const { return AliasTy == MayAlias; }
134
135   // isVolatile - Return true if this alias set contains volatile loads or
136   // stores.
137   bool isVolatile() const { return Volatile; }
138
139   /// isForwardingAliasSet - Return true if this alias set should be ignored as
140   /// part of the AliasSetTracker object.
141   bool isForwardingAliasSet() const { return Forward; }
142
143   /// mergeSetIn - Merge the specified alias set into this alias set...
144   ///
145   void mergeSetIn(AliasSet &AS);
146
147   // Alias Set iteration - Allow access to all of the pointer which are part of
148   // this alias set...
149   class iterator;
150   iterator begin() const { return iterator(PtrList); }
151   iterator end()   const { return iterator(); }
152   bool empty() const { return PtrList == 0; }
153
154   void print(std::ostream &OS) const;
155   void dump() const;
156
157   /// Define an iterator for alias sets... this is just a forward iterator.
158   class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
159     HashNodePair *CurNode;
160   public:
161     iterator(HashNodePair *CN = 0) : CurNode(CN) {}
162     
163     bool operator==(const iterator& x) const {
164       return CurNode == x.CurNode;
165     }
166     bool operator!=(const iterator& x) const { return !operator==(x); }
167
168     const iterator &operator=(const iterator &I) {
169       CurNode = I.CurNode;
170       return *this;
171     }
172   
173     value_type &operator*() const {
174       assert(CurNode && "Dereferencing AliasSet.end()!");
175       return *CurNode;
176     }
177     value_type *operator->() const { return &operator*(); }
178
179     Value *getPointer() const { return CurNode->first; }
180     unsigned getSize() const { return CurNode->second.getSize(); }
181   
182     iterator& operator++() {                // Preincrement
183       assert(CurNode && "Advancing past AliasSet.end()!");
184       CurNode = CurNode->second.getNext();
185       return *this;
186     }
187     iterator operator++(int) { // Postincrement
188       iterator tmp = *this; ++*this; return tmp; 
189     }
190   };
191
192 private:
193   // Can only be created by AliasSetTracker
194   AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
195                AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
196   }
197
198   AliasSet(const AliasSet &AS) {
199     assert(0 && "Copy ctor called!?!?!");
200     abort();
201   }
202
203   HashNodePair *getSomePointer() const {
204     return PtrList;
205   }
206
207   /// getForwardedTarget - Return the real alias set this represents.  If this
208   /// has been merged with another set and is forwarding, return the ultimate
209   /// destination set.  This also implements the union-find collapsing as well.
210   AliasSet *getForwardedTarget(AliasSetTracker &AST) {
211     if (!Forward) return this;
212
213     AliasSet *Dest = Forward->getForwardedTarget(AST);
214     if (Dest != Forward) {
215       Dest->addRef();
216       Forward->dropRef(AST);
217       Forward = Dest;
218     }
219     return Dest;
220   }
221
222   void removeFromTracker(AliasSetTracker &AST);
223
224   void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size,
225                   bool KnownMustAlias = false);
226   void addCallSite(CallSite CS, AliasAnalysis &AA);
227   void setVolatile() { Volatile = true; }
228
229   /// aliasesPointer - Return true if the specified pointer "may" (or must)
230   /// alias one of the members in the set.
231   ///
232   bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
233   bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
234 };
235
236 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
237   AS.print(OS);
238   return OS;
239 }
240
241
242 class AliasSetTracker {
243   AliasAnalysis &AA;
244   ilist<AliasSet> AliasSets;
245
246   // Map from pointers to their node
247   hash_map<Value*, AliasSet::PointerRec> PointerMap;
248 public:
249   /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
250   /// the specified alias analysis object to disambiguate load and store
251   /// addresses.
252   AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
253
254   /// add methods - These methods are used to add different types of
255   /// instructions to the alias sets.  Adding a new instruction can result in
256   /// one of three actions happening:
257   ///
258   ///   1. If the instruction doesn't alias any other sets, create a new set.
259   ///   2. If the instruction aliases exactly one set, add it to the set
260   ///   3. If the instruction aliases multiple sets, merge the sets, and add
261   ///      the instruction to the result.
262   ///
263   /// These methods return true if inserting the instruction resulted in the
264   /// addition of a new alias set (i.e., the pointer did not alias anything).
265   ///
266   bool add(Value *Ptr, unsigned Size);  // Add a location
267   bool add(LoadInst *LI);
268   bool add(StoreInst *SI);
269   bool add(FreeInst *FI);
270   bool add(CallSite CS);          // Call/Invoke instructions
271   bool add(CallInst *CI)   { return add(CallSite(CI)); }
272   bool add(InvokeInst *II) { return add(CallSite(II)); }
273   bool add(Instruction *I);       // Dispatch to one of the other add methods...
274   void add(BasicBlock &BB);       // Add all instructions in basic block
275   void add(const AliasSetTracker &AST); // Add alias relations from another AST
276
277   /// remove methods - These methods are used to remove all entries that might
278   /// be aliased by the specified instruction.  These methods return true if any
279   /// alias sets were eliminated.
280   bool remove(Value *Ptr, unsigned Size);  // Remove a location
281   bool remove(LoadInst *LI);
282   bool remove(StoreInst *SI);
283   bool remove(FreeInst *FI);
284   bool remove(CallSite CS);
285   bool remove(CallInst *CI)   { return remove(CallSite(CI)); }
286   bool remove(InvokeInst *II) { return remove(CallSite(II)); }
287   bool remove(Instruction *I);
288   void remove(AliasSet &AS);
289
290   /// getAliasSets - Return the alias sets that are active.
291   ///
292   const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
293
294   /// getAliasSetForPointer - Return the alias set that the specified pointer
295   /// lives in.  If the New argument is non-null, this method sets the value to
296   /// true if a new alias set is created to contain the pointer (because the
297   /// pointer didn't alias anything).
298   AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
299
300   /// getAliasSetForPointerIfExists - Return the alias set containing the
301   /// location specified if one exists, otherwise return null.
302   AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
303     return findAliasSetForPointer(P, Size);
304   }
305
306   /// getAliasAnalysis - Return the underlying alias analysis object used by
307   /// this tracker.
308   AliasAnalysis &getAliasAnalysis() const { return AA; }
309
310   /// deleteValue method - This method is used to remove a pointer value from
311   /// the AliasSetTracker entirely.  It should be used when an instruction is
312   /// deleted from the program to update the AST.  If you don't use this, you
313   /// would have dangling pointers to deleted instructions.
314   ///
315   void deleteValue(Value *PtrVal);
316
317   /// copyValue - This method should be used whenever a preexisting value in the
318   /// program is copied or cloned, introducing a new value.  Note that it is ok
319   /// for clients that use this method to introduce the same value multiple
320   /// times: if the tracker already knows about a value, it will ignore the
321   /// request.
322   ///
323   void copyValue(Value *From, Value *To);
324
325
326   typedef ilist<AliasSet>::iterator iterator;
327   typedef ilist<AliasSet>::const_iterator const_iterator;
328
329   const_iterator begin() const { return AliasSets.begin(); }
330   const_iterator end()   const { return AliasSets.end(); }
331
332   iterator begin() { return AliasSets.begin(); }
333   iterator end()   { return AliasSets.end(); }
334
335   void print(std::ostream &OS) const;
336   void dump() const;
337
338 private:
339   friend class AliasSet;
340   void removeAliasSet(AliasSet *AS);
341
342   AliasSet::HashNodePair &getEntryFor(Value *V) {
343     // Standard operator[], except that it returns the whole pair, not just
344     // ->second.
345     return *PointerMap.insert(AliasSet::HashNodePair(V,
346                                             AliasSet::PointerRec())).first;
347   }
348
349   AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
350                        bool &NewSet) {
351     NewSet = false;
352     AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
353     AS.AccessTy |= E;
354     return AS;
355   }
356   AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
357
358   AliasSet *findAliasSetForCallSite(CallSite CS);
359 };
360
361 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
362   AST.print(OS);
363   return OS;
364 }
365
366 } // End llvm namespace
367
368 #endif