1 //===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===//
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.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
11 #define LLVM_ANALYSIS_ALIASSETTRACKER_H
13 #include "llvm/Support/CallSite.h"
14 #include "Support/iterator"
15 #include "Support/hash_map"
16 #include "Support/ilist"
20 class AliasSetTracker;
24 friend class AliasSetTracker;
27 typedef std::pair<Value* const, PointerRec> HashNodePair;
30 HashNodePair *NextInList;
34 PointerRec() : NextInList(0), AS(0), Size(0) {}
36 HashNodePair *getNext() const { return NextInList; }
37 bool hasAliasSet() const { return AS != 0; }
39 void updateSize(unsigned NewSize) {
40 if (NewSize > Size) Size = NewSize;
43 unsigned getSize() const { return Size; }
45 AliasSet *getAliasSet(AliasSetTracker &AST) {
46 assert(AS && "No AliasSet yet!");
49 AS = OldAS->getForwardedTarget(AST);
50 if (--OldAS->RefCount == 0)
51 OldAS->removeFromTracker(AST);
57 void setAliasSet(AliasSet *as) {
58 assert(AS == 0 && "Already have an alias set!");
61 void setTail(HashNodePair *T) {
62 assert(NextInList == 0 && "Already have tail!");
67 HashNodePair *PtrListHead, *PtrListTail; // Singly linked list of nodes
68 AliasSet *Forward; // Forwarding pointer
69 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
71 std::vector<CallSite> CallSites; // All calls & invokes in this node
73 // RefCount - Number of nodes pointing to this AliasSet plus the number of
74 // AliasSets forwarding to it.
75 unsigned RefCount : 29;
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.
83 NoModRef = 0, Refs = 1, // Ref = bit 1
84 Mods = 2, ModRef = 3 // Mod = bit 2
86 unsigned AccessTy : 2;
88 /// AliasType - Keep track the relationships between the pointers in the set.
89 /// Lattice goes from MustAlias to MayAlias.
92 MustAlias = 0, MayAlias = 1
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;
100 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
102 bool operator==(const iterator& x) const {
103 return CurNode == x.CurNode;
105 bool operator!=(const iterator& x) const { return !operator==(x); }
107 const iterator &operator=(const iterator &I) {
112 value_type &operator*() const {
113 assert(CurNode && "Dereferencing AliasSet.end()!");
116 value_type *operator->() const { return &operator*(); }
118 iterator& operator++() { // Preincrement
119 assert(CurNode && "Advancing past AliasSet.end()!");
120 CurNode = CurNode->second.getNext();
123 iterator operator++(int) { // Postincrement
124 iterator tmp = *this; ++*this; return tmp;
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; }
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; }
141 /// mergeSetIn - Merge the specified alias set into this alias set...
143 void mergeSetIn(AliasSet &AS);
145 // Alias Set iteration - Allow access to all of the pointer which are part of
147 iterator begin() const { return iterator(PtrListHead); }
148 iterator end() const { return iterator(); }
150 void print(std::ostream &OS) const;
154 // Can only be created by AliasSetTracker
155 AliasSet() : PtrListHead(0), PtrListTail(0), Forward(0), RefCount(0),
156 AccessTy(NoModRef), AliasTy(MustAlias) {
158 HashNodePair *getSomePointer() const {
159 return PtrListHead ? PtrListHead : 0;
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;
168 AliasSet *Dest = Forward->getForwardedTarget(AST);
169 if (Dest != Forward) {
171 if (--Forward->RefCount == 0)
172 Forward->removeFromTracker(AST);
178 void removeFromTracker(AliasSetTracker &AST);
180 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
181 void addCallSite(CallSite CS);
183 /// aliasesPointer - Return true if the specified pointer "may" (or must)
184 /// alias one of the members in the set.
186 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
187 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
190 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
196 class AliasSetTracker {
198 ilist<AliasSet> AliasSets;
200 // Map from pointers to their node
201 hash_map<Value*, AliasSet::PointerRec> PointerMap;
203 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
204 /// the specified alias analysis object to disambiguate load and store
206 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
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:
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.
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...
224 /// getAliasSets - Return the alias sets that are active.
225 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
227 /// getAliasSetForPointer - Return the alias set that the specified pointer
229 AliasSet &getAliasSetForPointer(Value *P, unsigned Size);
231 /// getAliasAnalysis - Return the underlying alias analysis object used by
233 AliasAnalysis &getAliasAnalysis() const { return AA; }
235 typedef ilist<AliasSet>::iterator iterator;
236 typedef ilist<AliasSet>::const_iterator const_iterator;
238 const_iterator begin() const { return AliasSets.begin(); }
239 const_iterator end() const { return AliasSets.end(); }
241 iterator begin() { return AliasSets.begin(); }
242 iterator end() { return AliasSets.end(); }
244 void print(std::ostream &OS) const;
248 friend class AliasSet;
249 void removeAliasSet(AliasSet *AS);
251 AliasSet::HashNodePair &getEntryFor(Value *V) {
252 // Standard operator[], except that it returns the whole pair, not just
254 return *PointerMap.insert(AliasSet::HashNodePair(V,
255 AliasSet::PointerRec())).first;
258 void addPointer(Value *P, unsigned Size, AliasSet::AccessType E) {
259 AliasSet &AS = getAliasSetForPointer(P, Size);
262 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
264 AliasSet *findAliasSetForCallSite(CallSite CS);
267 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {