Add a bunch of new functionality, primarily to do with removing aliasing
[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 "Support/iterator"
22 #include "Support/hash_map"
23 #include "Support/ilist"
24
25 namespace llvm {
26
27 class AliasAnalysis;
28 class LoadInst;
29 class StoreInst;
30 class AliasSetTracker;
31 class AliasSet;
32
33 class AliasSet {
34   friend class AliasSetTracker;
35
36   struct PointerRec;
37   typedef std::pair<Value* const, PointerRec> HashNodePair;
38
39   class PointerRec {
40     HashNodePair **PrevInList, *NextInList;
41     AliasSet *AS;
42     unsigned Size;
43   public:
44     PointerRec() : PrevInList(0), NextInList(0), AS(0), Size(0) {}
45
46     HashNodePair *getNext() const { return NextInList; }
47     bool hasAliasSet() const { return AS != 0; }
48
49     HashNodePair** setPrevInList(HashNodePair **PIL) {
50       PrevInList = PIL;
51       return &NextInList;
52     }
53
54     void updateSize(unsigned NewSize) {
55       if (NewSize > Size) Size = NewSize;
56     }
57
58     unsigned getSize() const { return Size; }
59
60     AliasSet *getAliasSet(AliasSetTracker &AST) { 
61       assert(AS && "No AliasSet yet!");
62       if (AS->Forward) {
63         AliasSet *OldAS = AS;
64         AS = OldAS->getForwardedTarget(AST);
65         if (--OldAS->RefCount == 0)
66           OldAS->removeFromTracker(AST);
67         AS->RefCount++;
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 public:
122   /// Accessors...
123   bool isRef() const { return AccessTy & Refs; }
124   bool isMod() const { return AccessTy & Mods; }
125   bool isMustAlias() const { return AliasTy == MustAlias; }
126   bool isMayAlias()  const { return AliasTy == MayAlias; }
127
128   // isVolatile - Return true if this alias set contains volatile loads or
129   // stores.
130   bool isVolatile() const { return Volatile; }
131
132   /// isForwardingAliasSet - Return true if this alias set should be ignored as
133   /// part of the AliasSetTracker object.
134   bool isForwardingAliasSet() const { return Forward; }
135
136   /// mergeSetIn - Merge the specified alias set into this alias set...
137   ///
138   void mergeSetIn(AliasSet &AS);
139
140   // Alias Set iteration - Allow access to all of the pointer which are part of
141   // this alias set...
142   class iterator;
143   iterator begin() const { return iterator(PtrList); }
144   iterator end()   const { return iterator(); }
145   bool empty() const { return PtrList == 0; }
146
147   void print(std::ostream &OS) const;
148   void dump() const;
149
150   /// Define an iterator for alias sets... this is just a forward iterator.
151   class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
152     HashNodePair *CurNode;
153   public:
154     iterator(HashNodePair *CN = 0) : CurNode(CN) {}
155     
156     bool operator==(const iterator& x) const {
157       return CurNode == x.CurNode;
158     }
159     bool operator!=(const iterator& x) const { return !operator==(x); }
160
161     const iterator &operator=(const iterator &I) {
162       CurNode = I.CurNode;
163       return *this;
164     }
165   
166     value_type &operator*() const {
167       assert(CurNode && "Dereferencing AliasSet.end()!");
168       return *CurNode;
169     }
170     value_type *operator->() const { return &operator*(); }
171
172     Value *getPointer() const { return CurNode->first; }
173     unsigned getSize() const { return CurNode->second.getSize(); }
174   
175     iterator& operator++() {                // Preincrement
176       assert(CurNode && "Advancing past AliasSet.end()!");
177       CurNode = CurNode->second.getNext();
178       return *this;
179     }
180     iterator operator++(int) { // Postincrement
181       iterator tmp = *this; ++*this; return tmp; 
182     }
183   };
184
185 private:
186   // Can only be created by AliasSetTracker
187   AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
188                AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
189   }
190   AliasSet(const AliasSet &AS) {
191     // AliasSet's only get copy constructed in simple circumstances.  In
192     // particular, they cannot have any pointers in their list.  Despite this,
193     // we have to be sure to update the PtrListEnd to not point to the source
194     // AliasSet's list.
195     assert(AS.PtrList == 0 && "AliasSet has pointers in it!");
196     PtrList = 0; PtrListEnd = &PtrList;
197     Forward = AS.Forward; RefCount = AS.RefCount;
198     AccessTy = AS.AccessTy; AliasTy = AS.AliasTy; Volatile = AS.Volatile;
199   }
200
201   HashNodePair *getSomePointer() const {
202     return PtrList;
203   }
204
205   /// getForwardedTarget - Return the real alias set this represents.  If this
206   /// has been merged with another set and is forwarding, return the ultimate
207   /// destination set.  This also implements the union-find collapsing as well.
208   AliasSet *getForwardedTarget(AliasSetTracker &AST) {
209     if (!Forward) return this;
210
211     AliasSet *Dest = Forward->getForwardedTarget(AST);
212     if (Dest != Forward) {
213       Dest->RefCount++;
214       if (--Forward->RefCount == 0)
215         Forward->removeFromTracker(AST);
216       Forward = Dest;
217     }
218     return Dest;
219   }
220
221   void removeFromTracker(AliasSetTracker &AST);
222
223   void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
224   void addCallSite(CallSite CS, AliasAnalysis &AA);
225   void setVolatile() { Volatile = true; }
226
227   /// aliasesPointer - Return true if the specified pointer "may" (or must)
228   /// alias one of the members in the set.
229   ///
230   bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
231   bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
232 };
233
234 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
235   AS.print(OS);
236   return OS;
237 }
238
239
240 class AliasSetTracker {
241   AliasAnalysis &AA;
242   ilist<AliasSet> AliasSets;
243
244   // Map from pointers to their node
245   hash_map<Value*, AliasSet::PointerRec> PointerMap;
246 public:
247   /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
248   /// the specified alias analysis object to disambiguate load and store
249   /// addresses.
250   AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
251
252   /// add methods - These methods are used to add different types of
253   /// instructions to the alias sets.  Adding a new instruction can result in
254   /// one of three actions happening:
255   ///
256   ///   1. If the instruction doesn't alias any other sets, create a new set.
257   ///   2. If the instruction aliases exactly one set, add it to the set
258   ///   3. If the instruction aliases multiple sets, merge the sets, and add
259   ///      the instruction to the result.
260   ///
261   /// These methods return true if inserting the instruction resulted in the
262   /// addition of a new alias set (i.e., the pointer did not alias anything).
263   ///
264   bool add(LoadInst *LI);
265   bool add(StoreInst *SI);
266   bool add(CallSite CS);          // Call/Invoke instructions
267   bool add(CallInst *CI)   { return add(CallSite(CI)); }
268   bool add(InvokeInst *II) { return add(CallSite(II)); }
269   bool add(Instruction *I);       // Dispatch to one of the other add methods...
270   void add(BasicBlock &BB);       // Add all instructions in basic block
271   void add(const AliasSetTracker &AST); // Add alias relations from another AST
272
273   /// remove methods - These methods are used to remove all entries that might
274   /// be aliased by the specified instruction.  These methods return true if any
275   /// alias sets were eliminated.
276   bool remove(LoadInst *LI);
277   bool remove(StoreInst *SI);
278   bool remove(CallSite CS);
279   bool remove(CallInst *CI)   { return remove(CallSite(CI)); }
280   bool remove(InvokeInst *II) { return remove(CallSite(II)); }
281   bool remove(Instruction *I);
282   void remove(AliasSet &AS);
283
284
285   /// deleteValue method - This method is used to remove a pointer value from
286   /// the AliasSetTracker entirely.  It should be used when an instruction is
287   /// deleted from the program to update the AST.  If you don't use this, you
288   /// would have dangling pointers to deleted instructions.
289   ///
290   void deleteValue(Value *PtrVal);
291
292   /// getAliasSets - Return the alias sets that are active.
293   const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
294
295   /// getAliasSetForPointer - Return the alias set that the specified pointer
296   /// lives in.  If the New argument is non-null, this method sets the value to
297   /// true if a new alias set is created to contain the pointer (because the
298   /// pointer didn't alias anything).
299   AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
300
301   /// getAliasSetForPointerIfExists - Return the alias set containing the
302   /// location specified if one exists, otherwise return null.
303   AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
304     return findAliasSetForPointer(P, Size);
305   }
306
307   /// getAliasAnalysis - Return the underlying alias analysis object used by
308   /// this tracker.
309   AliasAnalysis &getAliasAnalysis() const { return AA; }
310
311   typedef ilist<AliasSet>::iterator iterator;
312   typedef ilist<AliasSet>::const_iterator const_iterator;
313
314   const_iterator begin() const { return AliasSets.begin(); }
315   const_iterator end()   const { return AliasSets.end(); }
316
317   iterator begin() { return AliasSets.begin(); }
318   iterator end()   { return AliasSets.end(); }
319
320   void print(std::ostream &OS) const;
321   void dump() const;
322
323 private:
324   friend class AliasSet;
325   void removeAliasSet(AliasSet *AS);
326
327   AliasSet::HashNodePair &getEntryFor(Value *V) {
328     // Standard operator[], except that it returns the whole pair, not just
329     // ->second.
330     return *PointerMap.insert(AliasSet::HashNodePair(V,
331                                             AliasSet::PointerRec())).first;
332   }
333
334   AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
335                        bool &NewSet) {
336     NewSet = false;
337     AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
338     AS.AccessTy |= E;
339     return AS;
340   }
341   AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
342
343   AliasSet *findAliasSetForCallSite(CallSite CS);
344 };
345
346 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
347   AST.print(OS);
348   return OS;
349 }
350
351 } // End llvm namespace
352
353 #endif