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/ADT/iterator"
22 #include "llvm/ADT/hash_map"
23 #include "llvm/ADT/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;
80 if (AS->PtrListEnd == &NextInList) {
81 AS->PtrListEnd = PrevInList;
82 assert(*AS->PtrListEnd == 0 && "List not terminated right!");
87 HashNodePair *PtrList, **PtrListEnd; // Doubly linked list of nodes
88 AliasSet *Forward; // Forwarding pointer
89 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
91 std::vector<CallSite> CallSites; // All calls & invokes in this node
93 // RefCount - Number of nodes pointing to this AliasSet plus the number of
94 // AliasSets forwarding to it.
95 unsigned RefCount : 28;
97 /// AccessType - Keep track of whether this alias set merely refers to the
98 /// locations of memory, whether it modifies the memory, or whether it does
99 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
100 /// ModRef as necessary.
103 NoModRef = 0, Refs = 1, // Ref = bit 1
104 Mods = 2, ModRef = 3 // Mod = bit 2
106 unsigned AccessTy : 2;
108 /// AliasType - Keep track the relationships between the pointers in the set.
109 /// Lattice goes from MustAlias to MayAlias.
112 MustAlias = 0, MayAlias = 1
114 unsigned AliasTy : 1;
116 // Volatile - True if this alias set contains volatile loads or stores.
119 friend struct ilist_traits<AliasSet>;
120 AliasSet *getPrev() const { return Prev; }
121 AliasSet *getNext() const { return Next; }
122 void setPrev(AliasSet *P) { Prev = P; }
123 void setNext(AliasSet *N) { Next = N; }
125 void addRef() { ++RefCount; }
126 void dropRef(AliasSetTracker &AST) {
127 assert(RefCount >= 1 && "Invalid reference count detected!");
129 removeFromTracker(AST);
134 bool isRef() const { return AccessTy & Refs; }
135 bool isMod() const { return AccessTy & Mods; }
136 bool isMustAlias() const { return AliasTy == MustAlias; }
137 bool isMayAlias() const { return AliasTy == MayAlias; }
139 // isVolatile - Return true if this alias set contains volatile loads or
141 bool isVolatile() const { return Volatile; }
143 /// isForwardingAliasSet - Return true if this alias set should be ignored as
144 /// part of the AliasSetTracker object.
145 bool isForwardingAliasSet() const { return Forward; }
147 /// mergeSetIn - Merge the specified alias set into this alias set...
149 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
151 // Alias Set iteration - Allow access to all of the pointer which are part of
154 iterator begin() const { return iterator(PtrList); }
155 iterator end() const { return iterator(); }
156 bool empty() const { return PtrList == 0; }
158 void print(std::ostream &OS) const;
161 /// Define an iterator for alias sets... this is just a forward iterator.
162 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
163 HashNodePair *CurNode;
165 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
167 bool operator==(const iterator& x) const {
168 return CurNode == x.CurNode;
170 bool operator!=(const iterator& x) const { return !operator==(x); }
172 const iterator &operator=(const iterator &I) {
177 value_type &operator*() const {
178 assert(CurNode && "Dereferencing AliasSet.end()!");
181 value_type *operator->() const { return &operator*(); }
183 Value *getPointer() const { return CurNode->first; }
184 unsigned getSize() const { return CurNode->second.getSize(); }
186 iterator& operator++() { // Preincrement
187 assert(CurNode && "Advancing past AliasSet.end()!");
188 CurNode = CurNode->second.getNext();
191 iterator operator++(int) { // Postincrement
192 iterator tmp = *this; ++*this; return tmp;
197 // Can only be created by AliasSetTracker
198 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
199 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
202 AliasSet(const AliasSet &AS) {
203 assert(0 && "Copy ctor called!?!?!");
207 HashNodePair *getSomePointer() const {
211 /// getForwardedTarget - Return the real alias set this represents. If this
212 /// has been merged with another set and is forwarding, return the ultimate
213 /// destination set. This also implements the union-find collapsing as well.
214 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
215 if (!Forward) return this;
217 AliasSet *Dest = Forward->getForwardedTarget(AST);
218 if (Dest != Forward) {
220 Forward->dropRef(AST);
226 void removeFromTracker(AliasSetTracker &AST);
228 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size,
229 bool KnownMustAlias = false);
230 void addCallSite(CallSite CS, AliasAnalysis &AA);
231 void setVolatile() { Volatile = true; }
233 /// aliasesPointer - Return true if the specified pointer "may" (or must)
234 /// alias one of the members in the set.
236 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
237 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
240 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
246 class AliasSetTracker {
248 ilist<AliasSet> AliasSets;
250 // Map from pointers to their node
251 hash_map<Value*, AliasSet::PointerRec> PointerMap;
253 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
254 /// the specified alias analysis object to disambiguate load and store
256 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
258 /// add methods - These methods are used to add different types of
259 /// instructions to the alias sets. Adding a new instruction can result in
260 /// one of three actions happening:
262 /// 1. If the instruction doesn't alias any other sets, create a new set.
263 /// 2. If the instruction aliases exactly one set, add it to the set
264 /// 3. If the instruction aliases multiple sets, merge the sets, and add
265 /// the instruction to the result.
267 /// These methods return true if inserting the instruction resulted in the
268 /// addition of a new alias set (i.e., the pointer did not alias anything).
270 bool add(Value *Ptr, unsigned Size); // Add a location
271 bool add(LoadInst *LI);
272 bool add(StoreInst *SI);
273 bool add(FreeInst *FI);
274 bool add(CallSite CS); // Call/Invoke instructions
275 bool add(CallInst *CI) { return add(CallSite(CI)); }
276 bool add(InvokeInst *II) { return add(CallSite(II)); }
277 bool add(Instruction *I); // Dispatch to one of the other add methods...
278 void add(BasicBlock &BB); // Add all instructions in basic block
279 void add(const AliasSetTracker &AST); // Add alias relations from another AST
281 /// remove methods - These methods are used to remove all entries that might
282 /// be aliased by the specified instruction. These methods return true if any
283 /// alias sets were eliminated.
284 bool remove(Value *Ptr, unsigned Size); // Remove a location
285 bool remove(LoadInst *LI);
286 bool remove(StoreInst *SI);
287 bool remove(FreeInst *FI);
288 bool remove(CallSite CS);
289 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
290 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
291 bool remove(Instruction *I);
292 void remove(AliasSet &AS);
294 /// getAliasSets - Return the alias sets that are active.
296 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
298 /// getAliasSetForPointer - Return the alias set that the specified pointer
299 /// lives in. If the New argument is non-null, this method sets the value to
300 /// true if a new alias set is created to contain the pointer (because the
301 /// pointer didn't alias anything).
302 AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
304 /// getAliasSetForPointerIfExists - Return the alias set containing the
305 /// location specified if one exists, otherwise return null.
306 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
307 return findAliasSetForPointer(P, Size);
310 /// containsPointer - Return true if the specified location is represented by
311 /// this alias set, false otherwise. This does not modify the AST object or
313 bool containsPointer(Value *P, unsigned Size) const;
315 /// getAliasAnalysis - Return the underlying alias analysis object used by
317 AliasAnalysis &getAliasAnalysis() const { return AA; }
319 /// deleteValue method - This method is used to remove a pointer value from
320 /// the AliasSetTracker entirely. It should be used when an instruction is
321 /// deleted from the program to update the AST. If you don't use this, you
322 /// would have dangling pointers to deleted instructions.
324 void deleteValue(Value *PtrVal);
326 /// copyValue - This method should be used whenever a preexisting value in the
327 /// program is copied or cloned, introducing a new value. Note that it is ok
328 /// for clients that use this method to introduce the same value multiple
329 /// times: if the tracker already knows about a value, it will ignore the
332 void copyValue(Value *From, Value *To);
335 typedef ilist<AliasSet>::iterator iterator;
336 typedef ilist<AliasSet>::const_iterator const_iterator;
338 const_iterator begin() const { return AliasSets.begin(); }
339 const_iterator end() const { return AliasSets.end(); }
341 iterator begin() { return AliasSets.begin(); }
342 iterator end() { return AliasSets.end(); }
344 void print(std::ostream &OS) const;
348 friend class AliasSet;
349 void removeAliasSet(AliasSet *AS);
351 AliasSet::HashNodePair &getEntryFor(Value *V) {
352 // Standard operator[], except that it returns the whole pair, not just
354 return *PointerMap.insert(AliasSet::HashNodePair(V,
355 AliasSet::PointerRec())).first;
358 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
361 AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
365 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
367 AliasSet *findAliasSetForCallSite(CallSite CS);
370 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
375 } // End llvm namespace