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"
33 class AliasSetTracker;
36 class AliasSet : public ilist_node<AliasSet> {
37 friend class AliasSetTracker;
40 Value *Val; // The pointer this record corresponds to.
41 PointerRec **PrevInList, *NextInList;
44 const MDNode *TBAAInfo;
47 : Val(V), PrevInList(0), NextInList(0), AS(0), Size(0), TBAAInfo(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 updateSizeAndTBAAInfo(unsigned NewSize, const MDNode *NewTBAAInfo) {
60 if (NewSize > Size) Size = NewSize;
63 TBAAInfo = NewTBAAInfo;
64 else if (TBAAInfo != NewTBAAInfo)
65 TBAAInfo = reinterpret_cast<const MDNode *>(-1);
68 unsigned getSize() const { return Size; }
70 const MDNode *getTBAAInfo() const { return TBAAInfo; }
72 AliasSet *getAliasSet(AliasSetTracker &AST) {
73 assert(AS && "No AliasSet yet!");
76 AS = OldAS->getForwardedTarget(AST);
83 void setAliasSet(AliasSet *as) {
84 assert(AS == 0 && "Already have an alias set!");
88 void eraseFromList() {
89 if (NextInList) NextInList->PrevInList = PrevInList;
90 *PrevInList = NextInList;
91 if (AS->PtrListEnd == &NextInList) {
92 AS->PtrListEnd = PrevInList;
93 assert(*AS->PtrListEnd == 0 && "List not terminated right!");
99 PointerRec *PtrList, **PtrListEnd; // Doubly linked list of nodes.
100 AliasSet *Forward; // Forwarding pointer.
101 AliasSet *Next, *Prev; // Doubly linked list of AliasSets.
103 // All calls & invokes in this alias set.
104 std::vector<AssertingVH<Instruction> > CallSites;
106 // RefCount - Number of nodes pointing to this AliasSet plus the number of
107 // AliasSets forwarding to it.
108 unsigned RefCount : 28;
110 /// AccessType - Keep track of whether this alias set merely refers to the
111 /// locations of memory, whether it modifies the memory, or whether it does
112 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
113 /// ModRef as necessary.
116 NoModRef = 0, Refs = 1, // Ref = bit 1
117 Mods = 2, ModRef = 3 // Mod = bit 2
119 unsigned AccessTy : 2;
121 /// AliasType - Keep track the relationships between the pointers in the set.
122 /// Lattice goes from MustAlias to MayAlias.
125 MustAlias = 0, MayAlias = 1
127 unsigned AliasTy : 1;
129 // Volatile - True if this alias set contains volatile loads or stores.
132 void addRef() { ++RefCount; }
133 void dropRef(AliasSetTracker &AST) {
134 assert(RefCount >= 1 && "Invalid reference count detected!");
136 removeFromTracker(AST);
139 CallSite getCallSite(unsigned i) const {
140 assert(i < CallSites.size());
141 return CallSite(CallSites[i]);
146 bool isRef() const { return AccessTy & Refs; }
147 bool isMod() const { return AccessTy & Mods; }
148 bool isMustAlias() const { return AliasTy == MustAlias; }
149 bool isMayAlias() const { return AliasTy == MayAlias; }
151 // isVolatile - Return true if this alias set contains volatile loads or
153 bool isVolatile() const { return Volatile; }
155 /// isForwardingAliasSet - Return true if this alias set should be ignored as
156 /// part of the AliasSetTracker object.
157 bool isForwardingAliasSet() const { return Forward; }
159 /// mergeSetIn - Merge the specified alias set into this alias set...
161 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
163 // Alias Set iteration - Allow access to all of the pointer which are part of
166 iterator begin() const { return iterator(PtrList); }
167 iterator end() const { return iterator(); }
168 bool empty() const { return PtrList == 0; }
170 void print(raw_ostream &OS) const;
173 /// Define an iterator for alias sets... this is just a forward iterator.
174 class iterator : public std::iterator<std::forward_iterator_tag,
175 PointerRec, ptrdiff_t> {
178 explicit iterator(PointerRec *CN = 0) : CurNode(CN) {}
180 bool operator==(const iterator& x) const {
181 return CurNode == x.CurNode;
183 bool operator!=(const iterator& x) const { return !operator==(x); }
185 const iterator &operator=(const iterator &I) {
190 value_type &operator*() const {
191 assert(CurNode && "Dereferencing AliasSet.end()!");
194 value_type *operator->() const { return &operator*(); }
196 Value *getPointer() const { return CurNode->getValue(); }
197 unsigned getSize() const { return CurNode->getSize(); }
198 const MDNode *getTBAAInfo() const { return CurNode->getTBAAInfo(); }
200 iterator& operator++() { // Preincrement
201 assert(CurNode && "Advancing past AliasSet.end()!");
202 CurNode = CurNode->getNext();
205 iterator operator++(int) { // Postincrement
206 iterator tmp = *this; ++*this; return tmp;
211 // Can only be created by AliasSetTracker. Also, ilist creates one
212 // to serve as a sentinel.
213 friend struct ilist_sentinel_traits<AliasSet>;
214 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
215 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
218 AliasSet(const AliasSet &AS); // do not implement
219 void operator=(const AliasSet &AS); // do not implement
221 PointerRec *getSomePointer() const {
225 /// getForwardedTarget - Return the real alias set this represents. If this
226 /// has been merged with another set and is forwarding, return the ultimate
227 /// destination set. This also implements the union-find collapsing as well.
228 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
229 if (!Forward) return this;
231 AliasSet *Dest = Forward->getForwardedTarget(AST);
232 if (Dest != Forward) {
234 Forward->dropRef(AST);
240 void removeFromTracker(AliasSetTracker &AST);
242 void addPointer(AliasSetTracker &AST, PointerRec &Entry, unsigned Size,
243 const MDNode *TBAAInfo,
244 bool KnownMustAlias = false);
245 void addCallSite(CallSite CS, AliasAnalysis &AA);
246 void removeCallSite(CallSite CS) {
247 for (size_t i = 0, e = CallSites.size(); i != e; ++i)
248 if (CallSites[i] == CS.getInstruction()) {
249 CallSites[i] = CallSites.back();
250 CallSites.pop_back();
253 void setVolatile() { Volatile = true; }
255 /// aliasesPointer - Return true if the specified pointer "may" (or must)
256 /// alias one of the members in the set.
258 bool aliasesPointer(const Value *Ptr, unsigned Size, const MDNode *TBAAInfo,
259 AliasAnalysis &AA) const;
260 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
263 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) {
269 class AliasSetTracker {
270 /// CallbackVH - A CallbackVH to arrange for AliasSetTracker to be
271 /// notified whenever a Value is deleted.
272 class ASTCallbackVH : public CallbackVH {
273 AliasSetTracker *AST;
274 virtual void deleted();
276 ASTCallbackVH(Value *V, AliasSetTracker *AST = 0);
277 ASTCallbackVH &operator=(Value *V);
279 /// ASTCallbackVHDenseMapInfo - Traits to tell DenseMap that tell us how to
280 /// compare and hash the value handle.
281 struct ASTCallbackVHDenseMapInfo : public DenseMapInfo<Value *> {};
284 ilist<AliasSet> AliasSets;
286 typedef DenseMap<ASTCallbackVH, AliasSet::PointerRec*,
287 ASTCallbackVHDenseMapInfo>
290 // Map from pointers to their node
291 PointerMapType PointerMap;
294 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
295 /// the specified alias analysis object to disambiguate load and store
297 explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
298 ~AliasSetTracker() { clear(); }
300 /// add methods - These methods are used to add different types of
301 /// instructions to the alias sets. Adding a new instruction can result in
302 /// one of three actions happening:
304 /// 1. If the instruction doesn't alias any other sets, create a new set.
305 /// 2. If the instruction aliases exactly one set, add it to the set
306 /// 3. If the instruction aliases multiple sets, merge the sets, and add
307 /// the instruction to the result.
309 /// These methods return true if inserting the instruction resulted in the
310 /// addition of a new alias set (i.e., the pointer did not alias anything).
312 bool add(Value *Ptr, unsigned Size, const MDNode *TBAAInfo); // Add a location
313 bool add(LoadInst *LI);
314 bool add(StoreInst *SI);
315 bool add(VAArgInst *VAAI);
316 bool add(CallSite CS); // Call/Invoke instructions
317 bool add(CallInst *CI) { return add(CallSite(CI)); }
318 bool add(InvokeInst *II) { return add(CallSite(II)); }
319 bool add(Instruction *I); // Dispatch to one of the other add methods...
320 void add(BasicBlock &BB); // Add all instructions in basic block
321 void add(const AliasSetTracker &AST); // Add alias relations from another AST
323 /// remove methods - These methods are used to remove all entries that might
324 /// be aliased by the specified instruction. These methods return true if any
325 /// alias sets were eliminated.
327 bool remove(Value *Ptr, unsigned Size, const MDNode *TBAAInfo);
328 bool remove(LoadInst *LI);
329 bool remove(StoreInst *SI);
330 bool remove(VAArgInst *VAAI);
331 bool remove(CallSite CS);
332 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
333 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
334 bool remove(Instruction *I);
335 void remove(AliasSet &AS);
339 /// getAliasSets - Return the alias sets that are active.
341 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
343 /// getAliasSetForPointer - Return the alias set that the specified pointer
344 /// lives in. If the New argument is non-null, this method sets the value to
345 /// true if a new alias set is created to contain the pointer (because the
346 /// pointer didn't alias anything).
347 AliasSet &getAliasSetForPointer(Value *P, unsigned Size,
348 const MDNode *TBAAInfo,
351 /// getAliasSetForPointerIfExists - Return the alias set containing the
352 /// location specified if one exists, otherwise return null.
353 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size,
354 const MDNode *TBAAInfo) {
355 return findAliasSetForPointer(P, Size, TBAAInfo);
358 /// containsPointer - Return true if the specified location is represented by
359 /// this alias set, false otherwise. This does not modify the AST object or
361 bool containsPointer(Value *P, unsigned Size, const MDNode *TBAAInfo) const;
363 /// getAliasAnalysis - Return the underlying alias analysis object used by
365 AliasAnalysis &getAliasAnalysis() const { return AA; }
367 /// deleteValue method - This method is used to remove a pointer value from
368 /// the AliasSetTracker entirely. It should be used when an instruction is
369 /// deleted from the program to update the AST. If you don't use this, you
370 /// would have dangling pointers to deleted instructions.
372 void deleteValue(Value *PtrVal);
374 /// copyValue - This method should be used whenever a preexisting value in the
375 /// program is copied or cloned, introducing a new value. Note that it is ok
376 /// for clients that use this method to introduce the same value multiple
377 /// times: if the tracker already knows about a value, it will ignore the
380 void copyValue(Value *From, Value *To);
383 typedef ilist<AliasSet>::iterator iterator;
384 typedef ilist<AliasSet>::const_iterator const_iterator;
386 const_iterator begin() const { return AliasSets.begin(); }
387 const_iterator end() const { return AliasSets.end(); }
389 iterator begin() { return AliasSets.begin(); }
390 iterator end() { return AliasSets.end(); }
392 void print(raw_ostream &OS) const;
396 friend class AliasSet;
397 void removeAliasSet(AliasSet *AS);
399 // getEntryFor - Just like operator[] on the map, except that it creates an
400 // entry for the pointer if it doesn't already exist.
401 AliasSet::PointerRec &getEntryFor(Value *V) {
402 AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)];
404 Entry = new AliasSet::PointerRec(V);
408 AliasSet &addPointer(Value *P, unsigned Size, const MDNode *TBAAInfo,
409 AliasSet::AccessType E,
412 AliasSet &AS = getAliasSetForPointer(P, Size, TBAAInfo, &NewSet);
416 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size,
417 const MDNode *TBAAInfo);
419 AliasSet *findAliasSetForCallSite(CallSite CS);
422 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) {
427 } // End llvm namespace