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