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 "llvm/Support/Streams.h"
22 #include "llvm/ADT/iterator"
23 #include "llvm/ADT/hash_map"
24 #include "llvm/ADT/ilist"
32 class AliasSetTracker;
36 friend class AliasSetTracker;
39 typedef std::pair<Value* const, PointerRec> HashNodePair;
42 HashNodePair **PrevInList, *NextInList;
46 PointerRec() : PrevInList(0), NextInList(0), AS(0), Size(0) {}
48 HashNodePair *getNext() const { return NextInList; }
49 bool hasAliasSet() const { return AS != 0; }
51 HashNodePair** setPrevInList(HashNodePair **PIL) {
56 void updateSize(unsigned NewSize) {
57 if (NewSize > Size) Size = NewSize;
60 unsigned getSize() const { return Size; }
62 AliasSet *getAliasSet(AliasSetTracker &AST) {
63 assert(AS && "No AliasSet yet!");
66 AS = OldAS->getForwardedTarget(AST);
73 void setAliasSet(AliasSet *as) {
74 assert(AS == 0 && "Already have an alias set!");
78 void removeFromList() {
79 if (NextInList) NextInList->second.PrevInList = PrevInList;
80 *PrevInList = NextInList;
81 if (AS->PtrListEnd == &NextInList) {
82 AS->PtrListEnd = PrevInList;
83 assert(*AS->PtrListEnd == 0 && "List not terminated right!");
88 HashNodePair *PtrList, **PtrListEnd; // Doubly linked list of nodes
89 AliasSet *Forward; // Forwarding pointer
90 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
92 std::vector<CallSite> CallSites; // All calls & invokes in this node
94 // RefCount - Number of nodes pointing to this AliasSet plus the number of
95 // AliasSets forwarding to it.
96 unsigned RefCount : 28;
98 /// AccessType - Keep track of whether this alias set merely refers to the
99 /// locations of memory, whether it modifies the memory, or whether it does
100 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
101 /// ModRef as necessary.
104 NoModRef = 0, Refs = 1, // Ref = bit 1
105 Mods = 2, ModRef = 3 // Mod = bit 2
107 unsigned AccessTy : 2;
109 /// AliasType - Keep track the relationships between the pointers in the set.
110 /// Lattice goes from MustAlias to MayAlias.
113 MustAlias = 0, MayAlias = 1
115 unsigned AliasTy : 1;
117 // Volatile - True if this alias set contains volatile loads or stores.
120 friend struct ilist_traits<AliasSet>;
121 AliasSet *getPrev() const { return Prev; }
122 AliasSet *getNext() const { return Next; }
123 void setPrev(AliasSet *P) { Prev = P; }
124 void setNext(AliasSet *N) { Next = N; }
126 void addRef() { ++RefCount; }
127 void dropRef(AliasSetTracker &AST) {
128 assert(RefCount >= 1 && "Invalid reference count detected!");
130 removeFromTracker(AST);
135 bool isRef() const { return AccessTy & Refs; }
136 bool isMod() const { return AccessTy & Mods; }
137 bool isMustAlias() const { return AliasTy == MustAlias; }
138 bool isMayAlias() const { return AliasTy == MayAlias; }
140 // isVolatile - Return true if this alias set contains volatile loads or
142 bool isVolatile() const { return Volatile; }
144 /// isForwardingAliasSet - Return true if this alias set should be ignored as
145 /// part of the AliasSetTracker object.
146 bool isForwardingAliasSet() const { return Forward; }
148 /// mergeSetIn - Merge the specified alias set into this alias set...
150 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
152 // Alias Set iteration - Allow access to all of the pointer which are part of
155 iterator begin() const { return iterator(PtrList); }
156 iterator end() const { return iterator(); }
157 bool empty() const { return PtrList == 0; }
159 void print(std::ostream &OS) const;
160 void print(std::ostream *OS) const { if (OS) print(*OS); }
163 /// Define an iterator for alias sets... this is just a forward iterator.
164 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
165 HashNodePair *CurNode;
167 explicit iterator(HashNodePair *CN = 0) : CurNode(CN) {}
169 bool operator==(const iterator& x) const {
170 return CurNode == x.CurNode;
172 bool operator!=(const iterator& x) const { return !operator==(x); }
174 const iterator &operator=(const iterator &I) {
179 value_type &operator*() const {
180 assert(CurNode && "Dereferencing AliasSet.end()!");
183 value_type *operator->() const { return &operator*(); }
185 Value *getPointer() const { return CurNode->first; }
186 unsigned getSize() const { return CurNode->second.getSize(); }
188 iterator& operator++() { // Preincrement
189 assert(CurNode && "Advancing past AliasSet.end()!");
190 CurNode = CurNode->second.getNext();
193 iterator operator++(int) { // Postincrement
194 iterator tmp = *this; ++*this; return tmp;
199 // Can only be created by AliasSetTracker
200 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
201 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
204 AliasSet(const AliasSet &AS) {
205 assert(0 && "Copy ctor called!?!?!");
209 HashNodePair *getSomePointer() const {
213 /// getForwardedTarget - Return the real alias set this represents. If this
214 /// has been merged with another set and is forwarding, return the ultimate
215 /// destination set. This also implements the union-find collapsing as well.
216 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
217 if (!Forward) return this;
219 AliasSet *Dest = Forward->getForwardedTarget(AST);
220 if (Dest != Forward) {
222 Forward->dropRef(AST);
228 void removeFromTracker(AliasSetTracker &AST);
230 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size,
231 bool KnownMustAlias = false);
232 void addCallSite(CallSite CS, AliasAnalysis &AA);
233 void removeCallSite(CallSite CS) {
234 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
235 if (CallSites[i].getInstruction() == CS.getInstruction()) {
236 CallSites[i] = CallSites.back();
237 CallSites.pop_back();
240 void setVolatile() { Volatile = true; }
242 /// aliasesPointer - Return true if the specified pointer "may" (or must)
243 /// alias one of the members in the set.
245 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
246 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
249 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
255 class AliasSetTracker {
257 ilist<AliasSet> AliasSets;
259 // Map from pointers to their node
260 hash_map<Value*, AliasSet::PointerRec> PointerMap;
262 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
263 /// the specified alias analysis object to disambiguate load and store
265 explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
267 /// add methods - These methods are used to add different types of
268 /// instructions to the alias sets. Adding a new instruction can result in
269 /// one of three actions happening:
271 /// 1. If the instruction doesn't alias any other sets, create a new set.
272 /// 2. If the instruction aliases exactly one set, add it to the set
273 /// 3. If the instruction aliases multiple sets, merge the sets, and add
274 /// the instruction to the result.
276 /// These methods return true if inserting the instruction resulted in the
277 /// addition of a new alias set (i.e., the pointer did not alias anything).
279 bool add(Value *Ptr, unsigned Size); // Add a location
280 bool add(LoadInst *LI);
281 bool add(StoreInst *SI);
282 bool add(FreeInst *FI);
283 bool add(CallSite CS); // Call/Invoke instructions
284 bool add(CallInst *CI) { return add(CallSite(CI)); }
285 bool add(InvokeInst *II) { return add(CallSite(II)); }
286 bool add(Instruction *I); // Dispatch to one of the other add methods...
287 void add(BasicBlock &BB); // Add all instructions in basic block
288 void add(const AliasSetTracker &AST); // Add alias relations from another AST
290 /// remove methods - These methods are used to remove all entries that might
291 /// be aliased by the specified instruction. These methods return true if any
292 /// alias sets were eliminated.
293 bool remove(Value *Ptr, unsigned Size); // Remove a location
294 bool remove(LoadInst *LI);
295 bool remove(StoreInst *SI);
296 bool remove(FreeInst *FI);
297 bool remove(CallSite CS);
298 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
299 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
300 bool remove(Instruction *I);
301 void remove(AliasSet &AS);
308 /// getAliasSets - Return the alias sets that are active.
310 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
312 /// getAliasSetForPointer - Return the alias set that the specified pointer
313 /// lives in. If the New argument is non-null, this method sets the value to
314 /// true if a new alias set is created to contain the pointer (because the
315 /// pointer didn't alias anything).
316 AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
318 /// getAliasSetForPointerIfExists - Return the alias set containing the
319 /// location specified if one exists, otherwise return null.
320 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
321 return findAliasSetForPointer(P, Size);
324 /// containsPointer - Return true if the specified location is represented by
325 /// this alias set, false otherwise. This does not modify the AST object or
327 bool containsPointer(Value *P, unsigned Size) const;
329 /// getAliasAnalysis - Return the underlying alias analysis object used by
331 AliasAnalysis &getAliasAnalysis() const { return AA; }
333 /// deleteValue method - This method is used to remove a pointer value from
334 /// the AliasSetTracker entirely. It should be used when an instruction is
335 /// deleted from the program to update the AST. If you don't use this, you
336 /// would have dangling pointers to deleted instructions.
338 void deleteValue(Value *PtrVal);
340 /// copyValue - This method should be used whenever a preexisting value in the
341 /// program is copied or cloned, introducing a new value. Note that it is ok
342 /// for clients that use this method to introduce the same value multiple
343 /// times: if the tracker already knows about a value, it will ignore the
346 void copyValue(Value *From, Value *To);
349 typedef ilist<AliasSet>::iterator iterator;
350 typedef ilist<AliasSet>::const_iterator const_iterator;
352 const_iterator begin() const { return AliasSets.begin(); }
353 const_iterator end() const { return AliasSets.end(); }
355 iterator begin() { return AliasSets.begin(); }
356 iterator end() { return AliasSets.end(); }
358 void print(std::ostream &OS) const;
359 void print(std::ostream *OS) const { if (OS) print(*OS); }
363 friend class AliasSet;
364 void removeAliasSet(AliasSet *AS);
366 AliasSet::HashNodePair &getEntryFor(Value *V) {
367 // Standard operator[], except that it returns the whole pair, not just
369 return *PointerMap.insert(AliasSet::HashNodePair(V,
370 AliasSet::PointerRec())).first;
373 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
376 AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
380 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
382 AliasSet *findAliasSetForCallSite(CallSite CS);
385 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
390 } // End llvm namespace