1 //===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
18 #define LLVM_ANALYSIS_ALIASSETTRACKER_H
20 #include "llvm/Support/CallSite.h"
21 #include "Support/iterator"
22 #include "Support/hash_map"
23 #include "Support/ilist"
31 class AliasSetTracker;
35 friend class AliasSetTracker;
38 typedef std::pair<Value* const, PointerRec> HashNodePair;
41 HashNodePair **PrevInList, *NextInList;
45 PointerRec() : PrevInList(0), NextInList(0), AS(0), Size(0) {}
47 HashNodePair *getNext() const { return NextInList; }
48 bool hasAliasSet() const { return AS != 0; }
50 HashNodePair** setPrevInList(HashNodePair **PIL) {
55 void updateSize(unsigned NewSize) {
56 if (NewSize > Size) Size = NewSize;
59 unsigned getSize() const { return Size; }
61 AliasSet *getAliasSet(AliasSetTracker &AST) {
62 assert(AS && "No AliasSet yet!");
65 AS = OldAS->getForwardedTarget(AST);
72 void setAliasSet(AliasSet *as) {
73 assert(AS == 0 && "Already have an alias set!");
77 void removeFromList() {
78 if (NextInList) NextInList->second.PrevInList = PrevInList;
79 *PrevInList = NextInList;
83 HashNodePair *PtrList, **PtrListEnd; // Doubly linked list of nodes
84 AliasSet *Forward; // Forwarding pointer
85 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
87 std::vector<CallSite> CallSites; // All calls & invokes in this node
89 // RefCount - Number of nodes pointing to this AliasSet plus the number of
90 // AliasSets forwarding to it.
91 unsigned RefCount : 28;
93 /// AccessType - Keep track of whether this alias set merely refers to the
94 /// locations of memory, whether it modifies the memory, or whether it does
95 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
96 /// ModRef as necessary.
99 NoModRef = 0, Refs = 1, // Ref = bit 1
100 Mods = 2, ModRef = 3 // Mod = bit 2
102 unsigned AccessTy : 2;
104 /// AliasType - Keep track the relationships between the pointers in the set.
105 /// Lattice goes from MustAlias to MayAlias.
108 MustAlias = 0, MayAlias = 1
110 unsigned AliasTy : 1;
112 // Volatile - True if this alias set contains volatile loads or stores.
115 friend class ilist_traits<AliasSet>;
116 AliasSet *getPrev() const { return Prev; }
117 AliasSet *getNext() const { return Next; }
118 void setPrev(AliasSet *P) { Prev = P; }
119 void setNext(AliasSet *N) { Next = N; }
121 void addRef() { ++RefCount; }
122 void dropRef(AliasSetTracker &AST) {
123 assert(RefCount >= 1 && "Invalid reference count detected!");
125 removeFromTracker(AST);
130 bool isRef() const { return AccessTy & Refs; }
131 bool isMod() const { return AccessTy & Mods; }
132 bool isMustAlias() const { return AliasTy == MustAlias; }
133 bool isMayAlias() const { return AliasTy == MayAlias; }
135 // isVolatile - Return true if this alias set contains volatile loads or
137 bool isVolatile() const { return Volatile; }
139 /// isForwardingAliasSet - Return true if this alias set should be ignored as
140 /// part of the AliasSetTracker object.
141 bool isForwardingAliasSet() const { return Forward; }
143 /// mergeSetIn - Merge the specified alias set into this alias set...
145 void mergeSetIn(AliasSet &AS);
147 // Alias Set iteration - Allow access to all of the pointer which are part of
150 iterator begin() const { return iterator(PtrList); }
151 iterator end() const { return iterator(); }
152 bool empty() const { return PtrList == 0; }
154 void print(std::ostream &OS) const;
157 /// Define an iterator for alias sets... this is just a forward iterator.
158 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
159 HashNodePair *CurNode;
161 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
163 bool operator==(const iterator& x) const {
164 return CurNode == x.CurNode;
166 bool operator!=(const iterator& x) const { return !operator==(x); }
168 const iterator &operator=(const iterator &I) {
173 value_type &operator*() const {
174 assert(CurNode && "Dereferencing AliasSet.end()!");
177 value_type *operator->() const { return &operator*(); }
179 Value *getPointer() const { return CurNode->first; }
180 unsigned getSize() const { return CurNode->second.getSize(); }
182 iterator& operator++() { // Preincrement
183 assert(CurNode && "Advancing past AliasSet.end()!");
184 CurNode = CurNode->second.getNext();
187 iterator operator++(int) { // Postincrement
188 iterator tmp = *this; ++*this; return tmp;
193 // Can only be created by AliasSetTracker
194 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
195 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
198 AliasSet(const AliasSet &AS) {
199 assert(0 && "Copy ctor called!?!?!");
203 HashNodePair *getSomePointer() const {
207 /// getForwardedTarget - Return the real alias set this represents. If this
208 /// has been merged with another set and is forwarding, return the ultimate
209 /// destination set. This also implements the union-find collapsing as well.
210 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
211 if (!Forward) return this;
213 AliasSet *Dest = Forward->getForwardedTarget(AST);
214 if (Dest != Forward) {
216 Forward->dropRef(AST);
222 void removeFromTracker(AliasSetTracker &AST);
224 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
225 void addCallSite(CallSite CS, AliasAnalysis &AA);
226 void setVolatile() { Volatile = true; }
228 /// aliasesPointer - Return true if the specified pointer "may" (or must)
229 /// alias one of the members in the set.
231 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
232 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
235 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
241 class AliasSetTracker {
243 ilist<AliasSet> AliasSets;
245 // Map from pointers to their node
246 hash_map<Value*, AliasSet::PointerRec> PointerMap;
248 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
249 /// the specified alias analysis object to disambiguate load and store
251 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
253 /// add methods - These methods are used to add different types of
254 /// instructions to the alias sets. Adding a new instruction can result in
255 /// one of three actions happening:
257 /// 1. If the instruction doesn't alias any other sets, create a new set.
258 /// 2. If the instruction aliases exactly one set, add it to the set
259 /// 3. If the instruction aliases multiple sets, merge the sets, and add
260 /// the instruction to the result.
262 /// These methods return true if inserting the instruction resulted in the
263 /// addition of a new alias set (i.e., the pointer did not alias anything).
265 bool add(Value *Ptr, unsigned Size); // Add a location
266 bool add(LoadInst *LI);
267 bool add(StoreInst *SI);
268 bool add(FreeInst *FI);
269 bool add(CallSite CS); // Call/Invoke instructions
270 bool add(CallInst *CI) { return add(CallSite(CI)); }
271 bool add(InvokeInst *II) { return add(CallSite(II)); }
272 bool add(Instruction *I); // Dispatch to one of the other add methods...
273 void add(BasicBlock &BB); // Add all instructions in basic block
274 void add(const AliasSetTracker &AST); // Add alias relations from another AST
276 /// remove methods - These methods are used to remove all entries that might
277 /// be aliased by the specified instruction. These methods return true if any
278 /// alias sets were eliminated.
279 bool remove(Value *Ptr, unsigned Size); // Remove a location
280 bool remove(LoadInst *LI);
281 bool remove(StoreInst *SI);
282 bool remove(FreeInst *FI);
283 bool remove(CallSite CS);
284 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
285 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
286 bool remove(Instruction *I);
287 void remove(AliasSet &AS);
290 /// deleteValue method - This method is used to remove a pointer value from
291 /// the AliasSetTracker entirely. It should be used when an instruction is
292 /// deleted from the program to update the AST. If you don't use this, you
293 /// would have dangling pointers to deleted instructions.
295 void deleteValue(Value *PtrVal);
297 /// getAliasSets - Return the alias sets that are active.
298 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
300 /// getAliasSetForPointer - Return the alias set that the specified pointer
301 /// lives in. If the New argument is non-null, this method sets the value to
302 /// true if a new alias set is created to contain the pointer (because the
303 /// pointer didn't alias anything).
304 AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
306 /// getAliasSetForPointerIfExists - Return the alias set containing the
307 /// location specified if one exists, otherwise return null.
308 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
309 return findAliasSetForPointer(P, Size);
312 /// getAliasAnalysis - Return the underlying alias analysis object used by
314 AliasAnalysis &getAliasAnalysis() const { return AA; }
316 typedef ilist<AliasSet>::iterator iterator;
317 typedef ilist<AliasSet>::const_iterator const_iterator;
319 const_iterator begin() const { return AliasSets.begin(); }
320 const_iterator end() const { return AliasSets.end(); }
322 iterator begin() { return AliasSets.begin(); }
323 iterator end() { return AliasSets.end(); }
325 void print(std::ostream &OS) const;
329 friend class AliasSet;
330 void removeAliasSet(AliasSet *AS);
332 AliasSet::HashNodePair &getEntryFor(Value *V) {
333 // Standard operator[], except that it returns the whole pair, not just
335 return *PointerMap.insert(AliasSet::HashNodePair(V,
336 AliasSet::PointerRec())).first;
339 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
342 AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
346 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
348 AliasSet *findAliasSetForCallSite(CallSite CS);
351 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
356 } // End llvm namespace