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