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 friend class ilist_traits<AliasSet>;
97 AliasSet *getPrev() const { return Prev; }
98 AliasSet *getNext() const { return Next; }
99 void setPrev(AliasSet *P) { Prev = P; }
100 void setNext(AliasSet *N) { Next = N; }
104 bool isRef() const { return AccessTy & Refs; }
105 bool isMod() const { return AccessTy & Mods; }
106 bool isMustAlias() const { return AliasTy == MustAlias; }
107 bool isMayAlias() const { return AliasTy == MayAlias; }
109 /// isForwardingAliasSet - Return true if this alias set should be ignored as
110 /// part of the AliasSetTracker object.
111 bool isForwardingAliasSet() const { return Forward; }
113 /// mergeSetIn - Merge the specified alias set into this alias set...
115 void mergeSetIn(AliasSet &AS);
117 // Alias Set iteration - Allow access to all of the pointer which are part of
120 iterator begin() const { return iterator(PtrListHead); }
121 iterator end() const { return iterator(); }
123 void print(std::ostream &OS) const;
126 /// Define an iterator for alias sets... this is just a forward iterator.
127 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
128 HashNodePair *CurNode;
130 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
132 bool operator==(const iterator& x) const {
133 return CurNode == x.CurNode;
135 bool operator!=(const iterator& x) const { return !operator==(x); }
137 const iterator &operator=(const iterator &I) {
142 value_type &operator*() const {
143 assert(CurNode && "Dereferencing AliasSet.end()!");
146 value_type *operator->() const { return &operator*(); }
148 iterator& operator++() { // Preincrement
149 assert(CurNode && "Advancing past AliasSet.end()!");
150 CurNode = CurNode->second.getNext();
153 iterator operator++(int) { // Postincrement
154 iterator tmp = *this; ++*this; return tmp;
159 // Can only be created by AliasSetTracker
160 AliasSet() : PtrListHead(0), PtrListTail(0), Forward(0), RefCount(0),
161 AccessTy(NoModRef), AliasTy(MustAlias) {
163 HashNodePair *getSomePointer() const {
164 return PtrListHead ? PtrListHead : 0;
167 /// getForwardedTarget - Return the real alias set this represents. If this
168 /// has been merged with another set and is forwarding, return the ultimate
169 /// destination set. This also implements the union-find collapsing as well.
170 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
171 if (!Forward) return this;
173 AliasSet *Dest = Forward->getForwardedTarget(AST);
174 if (Dest != Forward) {
176 if (--Forward->RefCount == 0)
177 Forward->removeFromTracker(AST);
183 void removeFromTracker(AliasSetTracker &AST);
185 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
186 void addCallSite(CallSite CS);
188 /// aliasesPointer - Return true if the specified pointer "may" (or must)
189 /// alias one of the members in the set.
191 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
192 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
195 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
201 class AliasSetTracker {
203 ilist<AliasSet> AliasSets;
205 // Map from pointers to their node
206 hash_map<Value*, AliasSet::PointerRec> PointerMap;
208 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
209 /// the specified alias analysis object to disambiguate load and store
211 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
213 /// add methods - These methods are used to add different types of
214 /// instructions to the alias sets. Adding a new instruction can result in
215 /// one of three actions happening:
217 /// 1. If the instruction doesn't alias any other sets, create a new set.
218 /// 2. If the instruction aliases exactly one set, add it to the set
219 /// 3. If the instruction aliases multiple sets, merge the sets, and add
220 /// the instruction to the result.
222 void add(LoadInst *LI);
223 void add(StoreInst *SI);
224 void add(CallSite CS); // Call/Invoke instructions
225 void add(CallInst *CI) { add(CallSite(CI)); }
226 void add(InvokeInst *II) { add(CallSite(II)); }
227 void add(Instruction *I); // Dispatch to one of the other add methods...
228 void add(BasicBlock &BB); // Add all instructions in basic block
229 void add(const AliasSetTracker &AST); // Add alias relations from another AST
231 /// getAliasSets - Return the alias sets that are active.
232 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
234 /// getAliasSetForPointer - Return the alias set that the specified pointer
236 AliasSet &getAliasSetForPointer(Value *P, unsigned Size);
238 /// getAliasAnalysis - Return the underlying alias analysis object used by
240 AliasAnalysis &getAliasAnalysis() const { return AA; }
242 typedef ilist<AliasSet>::iterator iterator;
243 typedef ilist<AliasSet>::const_iterator const_iterator;
245 const_iterator begin() const { return AliasSets.begin(); }
246 const_iterator end() const { return AliasSets.end(); }
248 iterator begin() { return AliasSets.begin(); }
249 iterator end() { return AliasSets.end(); }
251 void print(std::ostream &OS) const;
255 friend class AliasSet;
256 void removeAliasSet(AliasSet *AS);
258 AliasSet::HashNodePair &getEntryFor(Value *V) {
259 // Standard operator[], except that it returns the whole pair, not just
261 return *PointerMap.insert(AliasSet::HashNodePair(V,
262 AliasSet::PointerRec())).first;
265 void addPointer(Value *P, unsigned Size, AliasSet::AccessType E) {
266 AliasSet &AS = getAliasSetForPointer(P, Size);
269 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
271 AliasSet *findAliasSetForCallSite(CallSite CS);
274 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {