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