Added LLVM copyright header (for lack of a better term).
[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 class AliasAnalysis;
25 class LoadInst;
26 class StoreInst;
27 class AliasSetTracker;
28 class AliasSet;
29
30 class AliasSet {
31   friend class AliasSetTracker;
32
33   struct PointerRec;
34   typedef std::pair<Value* const, PointerRec> HashNodePair;
35
36   class PointerRec {
37     HashNodePair *NextInList;
38     AliasSet *AS;
39     unsigned Size;
40   public:
41     PointerRec() : NextInList(0), AS(0), Size(0) {}
42
43     HashNodePair *getNext() const { return NextInList; }
44     bool hasAliasSet() const { return AS != 0; }
45
46     void updateSize(unsigned NewSize) {
47       if (NewSize > Size) Size = NewSize;
48     }
49
50     unsigned getSize() const { return Size; }
51
52     AliasSet *getAliasSet(AliasSetTracker &AST) { 
53       assert(AS && "No AliasSet yet!");
54       if (AS->Forward) {
55         AliasSet *OldAS = AS;
56         AS = OldAS->getForwardedTarget(AST);
57         if (--OldAS->RefCount == 0)
58           OldAS->removeFromTracker(AST);
59         AS->RefCount++;
60       }
61       return AS;
62     }
63
64     void setAliasSet(AliasSet *as) {
65       assert(AS == 0 && "Already have an alias set!");
66       AS = as;
67     }
68     void setTail(HashNodePair *T) {
69       assert(NextInList == 0 && "Already have tail!");
70       NextInList = T;
71     }
72   };
73
74   HashNodePair *PtrListHead, *PtrListTail; // Singly linked list of nodes
75   AliasSet *Forward;             // Forwarding pointer
76   AliasSet *Next, *Prev;         // Doubly linked list of AliasSets
77
78   std::vector<CallSite> CallSites; // All calls & invokes in this node
79
80   // RefCount - Number of nodes pointing to this AliasSet plus the number of
81   // AliasSets forwarding to it.
82   unsigned RefCount : 29;
83
84   /// AccessType - Keep track of whether this alias set merely refers to the
85   /// locations of memory, whether it modifies the memory, or whether it does
86   /// both.  The lattice goes from "NoModRef" to either Refs or Mods, then to
87   /// ModRef as necessary.
88   ///
89   enum AccessType {
90     NoModRef = 0, Refs = 1,         // Ref = bit 1
91     Mods     = 2, ModRef = 3        // Mod = bit 2
92   };
93   unsigned AccessTy : 2;
94
95   /// AliasType - Keep track the relationships between the pointers in the set.
96   /// Lattice goes from MustAlias to MayAlias.
97   ///
98   enum AliasType {
99     MustAlias = 0, MayAlias = 1
100   };
101   unsigned AliasTy : 1;
102
103   friend class ilist_traits<AliasSet>;
104   AliasSet *getPrev() const { return Prev; }
105   AliasSet *getNext() const { return Next; }
106   void setPrev(AliasSet *P) { Prev = P; }
107   void setNext(AliasSet *N) { Next = N; }
108
109 public:
110   /// Accessors...
111   bool isRef() const { return AccessTy & Refs; }
112   bool isMod() const { return AccessTy & Mods; }
113   bool isMustAlias() const { return AliasTy == MustAlias; }
114   bool isMayAlias()  const { return AliasTy == MayAlias; }
115
116   /// isForwardingAliasSet - Return true if this alias set should be ignored as
117   /// part of the AliasSetTracker object.
118   bool isForwardingAliasSet() const { return Forward; }
119
120   /// mergeSetIn - Merge the specified alias set into this alias set...
121   ///
122   void mergeSetIn(AliasSet &AS);
123
124   // Alias Set iteration - Allow access to all of the pointer which are part of
125   // this alias set...
126   class iterator;
127   iterator begin() const { return iterator(PtrListHead); }
128   iterator end()   const { return iterator(); }
129
130   void print(std::ostream &OS) const;
131   void dump() const;
132
133   /// Define an iterator for alias sets... this is just a forward iterator.
134   class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
135     HashNodePair *CurNode;
136   public:
137     iterator(HashNodePair *CN = 0) : CurNode(CN) {}
138     
139     bool operator==(const iterator& x) const {
140       return CurNode == x.CurNode;
141     }
142     bool operator!=(const iterator& x) const { return !operator==(x); }
143
144     const iterator &operator=(const iterator &I) {
145       CurNode = I.CurNode;
146       return *this;
147     }
148   
149     value_type &operator*() const {
150       assert(CurNode && "Dereferencing AliasSet.end()!");
151       return *CurNode;
152     }
153     value_type *operator->() const { return &operator*(); }
154   
155     iterator& operator++() {                // Preincrement
156       assert(CurNode && "Advancing past AliasSet.end()!");
157       CurNode = CurNode->second.getNext();
158       return *this;
159     }
160     iterator operator++(int) { // Postincrement
161       iterator tmp = *this; ++*this; return tmp; 
162     }
163   };
164
165 private:
166   // Can only be created by AliasSetTracker
167   AliasSet() : PtrListHead(0), PtrListTail(0), Forward(0), RefCount(0),
168                AccessTy(NoModRef), AliasTy(MustAlias) {
169   }
170   HashNodePair *getSomePointer() const {
171     return PtrListHead ? PtrListHead : 0;
172   }
173
174   /// getForwardedTarget - Return the real alias set this represents.  If this
175   /// has been merged with another set and is forwarding, return the ultimate
176   /// destination set.  This also implements the union-find collapsing as well.
177   AliasSet *getForwardedTarget(AliasSetTracker &AST) {
178     if (!Forward) return this;
179
180     AliasSet *Dest = Forward->getForwardedTarget(AST);
181     if (Dest != Forward) {
182       Dest->RefCount++;
183       if (--Forward->RefCount == 0)
184         Forward->removeFromTracker(AST);
185       Forward = Dest;
186     }
187     return Dest;
188   }
189
190   void removeFromTracker(AliasSetTracker &AST);
191
192   void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
193   void addCallSite(CallSite CS);
194
195   /// aliasesPointer - Return true if the specified pointer "may" (or must)
196   /// alias one of the members in the set.
197   ///
198   bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
199   bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
200 };
201
202 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
203   AS.print(OS);
204   return OS;
205 }
206
207
208 class AliasSetTracker {
209   AliasAnalysis &AA;
210   ilist<AliasSet> AliasSets;
211
212   // Map from pointers to their node
213   hash_map<Value*, AliasSet::PointerRec> PointerMap;
214 public:
215   /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
216   /// the specified alias analysis object to disambiguate load and store
217   /// addresses.
218   AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
219
220   /// add methods - These methods are used to add different types of
221   /// instructions to the alias sets.  Adding a new instruction can result in
222   /// one of three actions happening:
223   ///
224   ///   1. If the instruction doesn't alias any other sets, create a new set.
225   ///   2. If the instruction aliases exactly one set, add it to the set
226   ///   3. If the instruction aliases multiple sets, merge the sets, and add
227   ///      the instruction to the result.
228   ///
229   void add(LoadInst *LI);
230   void add(StoreInst *SI);
231   void add(CallSite CS);          // Call/Invoke instructions
232   void add(CallInst *CI)   { add(CallSite(CI)); }
233   void add(InvokeInst *II) { add(CallSite(II)); }
234   void add(Instruction *I);       // Dispatch to one of the other add methods...
235   void add(BasicBlock &BB);       // Add all instructions in basic block
236   void add(const AliasSetTracker &AST); // Add alias relations from another AST
237
238   /// getAliasSets - Return the alias sets that are active.
239   const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
240
241   /// getAliasSetForPointer - Return the alias set that the specified pointer
242   /// lives in...
243   AliasSet &getAliasSetForPointer(Value *P, unsigned Size);
244
245   /// getAliasAnalysis - Return the underlying alias analysis object used by
246   /// this tracker.
247   AliasAnalysis &getAliasAnalysis() const { return AA; }
248
249   typedef ilist<AliasSet>::iterator iterator;
250   typedef ilist<AliasSet>::const_iterator const_iterator;
251
252   const_iterator begin() const { return AliasSets.begin(); }
253   const_iterator end()   const { return AliasSets.end(); }
254
255   iterator begin() { return AliasSets.begin(); }
256   iterator end()   { return AliasSets.end(); }
257
258   void print(std::ostream &OS) const;
259   void dump() const;
260
261 private:
262   friend class AliasSet;
263   void removeAliasSet(AliasSet *AS);
264
265   AliasSet::HashNodePair &getEntryFor(Value *V) {
266     // Standard operator[], except that it returns the whole pair, not just
267     // ->second.
268     return *PointerMap.insert(AliasSet::HashNodePair(V,
269                                             AliasSet::PointerRec())).first;
270   }
271
272   void addPointer(Value *P, unsigned Size, AliasSet::AccessType E) {
273     AliasSet &AS = getAliasSetForPointer(P, Size);
274     AS.AccessTy |= E;
275   }
276   AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
277
278   AliasSet *findAliasSetForCallSite(CallSite CS);
279 };
280
281 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
282   AST.print(OS);
283   return OS;
284 }
285
286 #endif