1 //===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ValueHandle.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/ilist.h"
24 #include "llvm/ADT/ilist_node.h"
34 class AliasSetTracker;
37 class AliasSet : public ilist_node<AliasSet> {
38 friend class AliasSetTracker;
41 Value *Val; // The pointer this record corresponds to.
42 PointerRec **PrevInList, *NextInList;
47 : Val(V), PrevInList(0), NextInList(0), AS(0), Size(0) {}
49 Value *getValue() const { return Val; }
51 PointerRec *getNext() const { return NextInList; }
52 bool hasAliasSet() const { return AS != 0; }
54 PointerRec** setPrevInList(PointerRec **PIL) {
59 void updateSize(unsigned NewSize) {
60 if (NewSize > Size) Size = NewSize;
63 unsigned getSize() const { return Size; }
65 AliasSet *getAliasSet(AliasSetTracker &AST) {
66 assert(AS && "No AliasSet yet!");
69 AS = OldAS->getForwardedTarget(AST);
76 void setAliasSet(AliasSet *as) {
77 assert(AS == 0 && "Already have an alias set!");
81 void eraseFromList() {
82 if (NextInList) NextInList->PrevInList = PrevInList;
83 *PrevInList = NextInList;
84 if (AS->PtrListEnd == &NextInList) {
85 AS->PtrListEnd = PrevInList;
86 assert(*AS->PtrListEnd == 0 && "List not terminated right!");
92 PointerRec *PtrList, **PtrListEnd; // Doubly linked list of nodes.
93 AliasSet *Forward; // Forwarding pointer.
94 AliasSet *Next, *Prev; // Doubly linked list of AliasSets.
96 std::vector<CallSite> CallSites; // All calls & invokes in this alias set.
98 // RefCount - Number of nodes pointing to this AliasSet plus the number of
99 // AliasSets forwarding to it.
100 unsigned RefCount : 28;
102 /// AccessType - Keep track of whether this alias set merely refers to the
103 /// locations of memory, whether it modifies the memory, or whether it does
104 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
105 /// ModRef as necessary.
108 NoModRef = 0, Refs = 1, // Ref = bit 1
109 Mods = 2, ModRef = 3 // Mod = bit 2
111 unsigned AccessTy : 2;
113 /// AliasType - Keep track the relationships between the pointers in the set.
114 /// Lattice goes from MustAlias to MayAlias.
117 MustAlias = 0, MayAlias = 1
119 unsigned AliasTy : 1;
121 // Volatile - True if this alias set contains volatile loads or stores.
124 void addRef() { ++RefCount; }
125 void dropRef(AliasSetTracker &AST) {
126 assert(RefCount >= 1 && "Invalid reference count detected!");
128 removeFromTracker(AST);
133 bool isRef() const { return AccessTy & Refs; }
134 bool isMod() const { return AccessTy & Mods; }
135 bool isMustAlias() const { return AliasTy == MustAlias; }
136 bool isMayAlias() const { return AliasTy == MayAlias; }
138 // isVolatile - Return true if this alias set contains volatile loads or
140 bool isVolatile() const { return Volatile; }
142 /// isForwardingAliasSet - Return true if this alias set should be ignored as
143 /// part of the AliasSetTracker object.
144 bool isForwardingAliasSet() const { return Forward; }
146 /// mergeSetIn - Merge the specified alias set into this alias set...
148 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
150 // Alias Set iteration - Allow access to all of the pointer which are part of
153 iterator begin() const { return iterator(PtrList); }
154 iterator end() const { return iterator(); }
155 bool empty() const { return PtrList == 0; }
157 void print(raw_ostream &OS) const;
160 /// Define an iterator for alias sets... this is just a forward iterator.
161 class iterator : public std::iterator<std::forward_iterator_tag,
162 PointerRec, ptrdiff_t> {
165 explicit iterator(PointerRec *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->getValue(); }
184 unsigned getSize() const { return CurNode->getSize(); }
186 iterator& operator++() { // Preincrement
187 assert(CurNode && "Advancing past AliasSet.end()!");
188 CurNode = CurNode->getNext();
191 iterator operator++(int) { // Postincrement
192 iterator tmp = *this; ++*this; return tmp;
197 // Can only be created by AliasSetTracker. Also, ilist creates one
198 // to serve as a sentinel.
199 friend struct ilist_sentinel_traits<AliasSet>;
200 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
201 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
204 AliasSet(const AliasSet &AS); // do not implement
205 void operator=(const AliasSet &AS); // do not implement
207 PointerRec *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, PointerRec &Entry, unsigned Size,
229 bool KnownMustAlias = false);
230 void addCallSite(CallSite CS, AliasAnalysis &AA);
231 void removeCallSite(CallSite CS) {
232 for (size_t i = 0, e = CallSites.size(); i != e; ++i)
233 if (CallSites[i].getInstruction() == CS.getInstruction()) {
234 CallSites[i] = CallSites.back();
235 CallSites.pop_back();
238 void setVolatile() { Volatile = true; }
240 /// aliasesPointer - Return true if the specified pointer "may" (or must)
241 /// alias one of the members in the set.
243 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
244 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
247 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) {
253 class AliasSetTracker {
254 /// CallbackVH - A CallbackVH to arrange for AliasSetTracker to be
255 /// notified whenever a Value is deleted.
256 class ASTCallbackVH : public CallbackVH {
257 AliasSetTracker *AST;
258 virtual void deleted();
260 ASTCallbackVH(Value *V, AliasSetTracker *AST = 0);
261 ASTCallbackVH &operator=(Value *V);
263 /// ASTCallbackVHDenseMapInfo - Traits to tell DenseMap that ASTCallbackVH
264 /// is not a POD (it needs its destructor called).
265 struct ASTCallbackVHDenseMapInfo : public DenseMapInfo<Value *> {
266 static bool isPod() { return false; }
270 ilist<AliasSet> AliasSets;
272 typedef DenseMap<ASTCallbackVH, AliasSet::PointerRec*,
273 ASTCallbackVHDenseMapInfo>
276 // Map from pointers to their node
277 PointerMapType PointerMap;
280 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
281 /// the specified alias analysis object to disambiguate load and store
283 explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
284 ~AliasSetTracker() { clear(); }
286 /// add methods - These methods are used to add different types of
287 /// instructions to the alias sets. Adding a new instruction can result in
288 /// one of three actions happening:
290 /// 1. If the instruction doesn't alias any other sets, create a new set.
291 /// 2. If the instruction aliases exactly one set, add it to the set
292 /// 3. If the instruction aliases multiple sets, merge the sets, and add
293 /// the instruction to the result.
295 /// These methods return true if inserting the instruction resulted in the
296 /// addition of a new alias set (i.e., the pointer did not alias anything).
298 bool add(Value *Ptr, unsigned Size); // Add a location
299 bool add(LoadInst *LI);
300 bool add(StoreInst *SI);
301 bool add(FreeInst *FI);
302 bool add(VAArgInst *VAAI);
303 bool add(CallSite CS); // Call/Invoke instructions
304 bool add(CallInst *CI) { return add(CallSite(CI)); }
305 bool add(InvokeInst *II) { return add(CallSite(II)); }
306 bool add(Instruction *I); // Dispatch to one of the other add methods...
307 void add(BasicBlock &BB); // Add all instructions in basic block
308 void add(const AliasSetTracker &AST); // Add alias relations from another AST
310 /// remove methods - These methods are used to remove all entries that might
311 /// be aliased by the specified instruction. These methods return true if any
312 /// alias sets were eliminated.
313 bool remove(Value *Ptr, unsigned Size); // Remove a location
314 bool remove(LoadInst *LI);
315 bool remove(StoreInst *SI);
316 bool remove(FreeInst *FI);
317 bool remove(VAArgInst *VAAI);
318 bool remove(CallSite CS);
319 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
320 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
321 bool remove(Instruction *I);
322 void remove(AliasSet &AS);
326 /// getAliasSets - Return the alias sets that are active.
328 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
330 /// getAliasSetForPointer - Return the alias set that the specified pointer
331 /// lives in. If the New argument is non-null, this method sets the value to
332 /// true if a new alias set is created to contain the pointer (because the
333 /// pointer didn't alias anything).
334 AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
336 /// getAliasSetForPointerIfExists - Return the alias set containing the
337 /// location specified if one exists, otherwise return null.
338 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
339 return findAliasSetForPointer(P, Size);
342 /// containsPointer - Return true if the specified location is represented by
343 /// this alias set, false otherwise. This does not modify the AST object or
345 bool containsPointer(Value *P, unsigned Size) const;
347 /// getAliasAnalysis - Return the underlying alias analysis object used by
349 AliasAnalysis &getAliasAnalysis() const { return AA; }
351 /// deleteValue method - This method is used to remove a pointer value from
352 /// the AliasSetTracker entirely. It should be used when an instruction is
353 /// deleted from the program to update the AST. If you don't use this, you
354 /// would have dangling pointers to deleted instructions.
356 void deleteValue(Value *PtrVal);
358 /// copyValue - This method should be used whenever a preexisting value in the
359 /// program is copied or cloned, introducing a new value. Note that it is ok
360 /// for clients that use this method to introduce the same value multiple
361 /// times: if the tracker already knows about a value, it will ignore the
364 void copyValue(Value *From, Value *To);
367 typedef ilist<AliasSet>::iterator iterator;
368 typedef ilist<AliasSet>::const_iterator const_iterator;
370 const_iterator begin() const { return AliasSets.begin(); }
371 const_iterator end() const { return AliasSets.end(); }
373 iterator begin() { return AliasSets.begin(); }
374 iterator end() { return AliasSets.end(); }
376 void print(raw_ostream &OS) const;
380 friend class AliasSet;
381 void removeAliasSet(AliasSet *AS);
383 // getEntryFor - Just like operator[] on the map, except that it creates an
384 // entry for the pointer if it doesn't already exist.
385 AliasSet::PointerRec &getEntryFor(Value *V) {
386 AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)];
388 Entry = new AliasSet::PointerRec(V);
392 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
395 AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
399 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
401 AliasSet *findAliasSetForCallSite(CallSite CS);
404 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) {
409 } // End llvm namespace