Rename a 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 "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
133   /// isForwardingAliasSet - Return true if this alias set should be ignored as
134   /// part of the AliasSetTracker object.
135   bool isForwardingAliasSet() const { return Forward; }
136
137   /// mergeSetIn - Merge the specified alias set into this alias set...
138   ///
139   void mergeSetIn(AliasSet &AS);
140
141   // Alias Set iteration - Allow access to all of the pointer which are part of
142   // this alias set...
143   class iterator;
144   iterator begin() const { return iterator(PtrList); }
145   iterator end()   const { return iterator(); }
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     iterator& operator++() {                // Preincrement
173       assert(CurNode && "Advancing past AliasSet.end()!");
174       CurNode = CurNode->second.getNext();
175       return *this;
176     }
177     iterator operator++(int) { // Postincrement
178       iterator tmp = *this; ++*this; return tmp; 
179     }
180   };
181
182 private:
183   // Can only be created by AliasSetTracker
184   AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
185                AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
186   }
187   AliasSet(const AliasSet &AS) {
188     // AliasSet's only get copy constructed in simple circumstances.  In
189     // particular, they cannot have any pointers in their list.  Despite this,
190     // we have to be sure to update the PtrListEnd to not point to the source
191     // AliasSet's list.
192     assert(AS.PtrList == 0 && "AliasSet has pointers in it!");
193     PtrList = 0; PtrListEnd = &PtrList;
194     Forward = AS.Forward; RefCount = AS.RefCount;
195     AccessTy = AS.AccessTy; AliasTy = AS.AliasTy; Volatile = AS.Volatile;
196   }
197
198   HashNodePair *getSomePointer() const {
199     return PtrList;
200   }
201
202   /// getForwardedTarget - Return the real alias set this represents.  If this
203   /// has been merged with another set and is forwarding, return the ultimate
204   /// destination set.  This also implements the union-find collapsing as well.
205   AliasSet *getForwardedTarget(AliasSetTracker &AST) {
206     if (!Forward) return this;
207
208     AliasSet *Dest = Forward->getForwardedTarget(AST);
209     if (Dest != Forward) {
210       Dest->RefCount++;
211       if (--Forward->RefCount == 0)
212         Forward->removeFromTracker(AST);
213       Forward = Dest;
214     }
215     return Dest;
216   }
217
218   void removeFromTracker(AliasSetTracker &AST);
219
220   void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
221   void addCallSite(CallSite CS, AliasAnalysis &AA);
222   void setVolatile() { Volatile = true; }
223
224   /// aliasesPointer - Return true if the specified pointer "may" (or must)
225   /// alias one of the members in the set.
226   ///
227   bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
228   bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
229 };
230
231 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
232   AS.print(OS);
233   return OS;
234 }
235
236
237 class AliasSetTracker {
238   AliasAnalysis &AA;
239   ilist<AliasSet> AliasSets;
240
241   // Map from pointers to their node
242   hash_map<Value*, AliasSet::PointerRec> PointerMap;
243 public:
244   /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
245   /// the specified alias analysis object to disambiguate load and store
246   /// addresses.
247   AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
248
249   /// add methods - These methods are used to add different types of
250   /// instructions to the alias sets.  Adding a new instruction can result in
251   /// one of three actions happening:
252   ///
253   ///   1. If the instruction doesn't alias any other sets, create a new set.
254   ///   2. If the instruction aliases exactly one set, add it to the set
255   ///   3. If the instruction aliases multiple sets, merge the sets, and add
256   ///      the instruction to the result.
257   ///
258   void add(LoadInst *LI);
259   void add(StoreInst *SI);
260   void add(CallSite CS);          // Call/Invoke instructions
261   void add(CallInst *CI)   { add(CallSite(CI)); }
262   void add(InvokeInst *II) { add(CallSite(II)); }
263   void add(Instruction *I);       // Dispatch to one of the other add methods...
264   void add(BasicBlock &BB);       // Add all instructions in basic block
265   void add(const AliasSetTracker &AST); // Add alias relations from another AST
266
267   /// deleteValue method - This method is used to remove a pointer value from
268   /// the AliasSetTracker entirely.  It should be used when an instruction is
269   /// deleted from the program to update the AST.  If you don't use this, you
270   /// would have dangling pointers to deleted instructions.
271   ///
272   void deleteValue(Value *PtrVal);
273
274   /// getAliasSets - Return the alias sets that are active.
275   const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
276
277   /// getAliasSetForPointer - Return the alias set that the specified pointer
278   /// lives in...
279   AliasSet &getAliasSetForPointer(Value *P, unsigned Size);
280
281   /// getAliasAnalysis - Return the underlying alias analysis object used by
282   /// this tracker.
283   AliasAnalysis &getAliasAnalysis() const { return AA; }
284
285   typedef ilist<AliasSet>::iterator iterator;
286   typedef ilist<AliasSet>::const_iterator const_iterator;
287
288   const_iterator begin() const { return AliasSets.begin(); }
289   const_iterator end()   const { return AliasSets.end(); }
290
291   iterator begin() { return AliasSets.begin(); }
292   iterator end()   { return AliasSets.end(); }
293
294   void print(std::ostream &OS) const;
295   void dump() const;
296
297 private:
298   friend class AliasSet;
299   void removeAliasSet(AliasSet *AS);
300
301   AliasSet::HashNodePair &getEntryFor(Value *V) {
302     // Standard operator[], except that it returns the whole pair, not just
303     // ->second.
304     return *PointerMap.insert(AliasSet::HashNodePair(V,
305                                             AliasSet::PointerRec())).first;
306   }
307
308   AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E) {
309     AliasSet &AS = getAliasSetForPointer(P, Size);
310     AS.AccessTy |= E;
311     return AS;
312   }
313   AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
314
315   AliasSet *findAliasSetForCallSite(CallSite CS);
316 };
317
318 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
319   AST.print(OS);
320   return OS;
321 }
322
323 } // End llvm namespace
324
325 #endif