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 /// getRawTBAAInfo - Return the raw TBAAInfo member. In addition to
71 /// being null or a pointer to an MDNode, this could be -1, meaning
72 /// there was conflicting information.
73 const MDNode *getRawTBAAInfo() const {
77 /// getTBAAInfo - Return the TBAAInfo, or null if there is no
78 /// information or conflicting information.
79 const MDNode *getTBAAInfo() const {
80 // If we have conflicting TBAAInfo, return null.
81 if (TBAAInfo == reinterpret_cast<const MDNode *>(-1))
86 AliasSet *getAliasSet(AliasSetTracker &AST) {
87 assert(AS && "No AliasSet yet!");
90 AS = OldAS->getForwardedTarget(AST);
97 void setAliasSet(AliasSet *as) {
98 assert(AS == 0 && "Already have an alias set!");
102 void eraseFromList() {
103 if (NextInList) NextInList->PrevInList = PrevInList;
104 *PrevInList = NextInList;
105 if (AS->PtrListEnd == &NextInList) {
106 AS->PtrListEnd = PrevInList;
107 assert(*AS->PtrListEnd == 0 && "List not terminated right!");
113 PointerRec *PtrList, **PtrListEnd; // Doubly linked list of nodes.
114 AliasSet *Forward; // Forwarding pointer.
115 AliasSet *Next, *Prev; // Doubly linked list of AliasSets.
117 // All calls & invokes in this alias set.
118 std::vector<AssertingVH<Instruction> > CallSites;
120 // RefCount - Number of nodes pointing to this AliasSet plus the number of
121 // AliasSets forwarding to it.
122 unsigned RefCount : 28;
124 /// AccessType - Keep track of whether this alias set merely refers to the
125 /// locations of memory, whether it modifies the memory, or whether it does
126 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
127 /// ModRef as necessary.
130 NoModRef = 0, Refs = 1, // Ref = bit 1
131 Mods = 2, ModRef = 3 // Mod = bit 2
133 unsigned AccessTy : 2;
135 /// AliasType - Keep track the relationships between the pointers in the set.
136 /// Lattice goes from MustAlias to MayAlias.
139 MustAlias = 0, MayAlias = 1
141 unsigned AliasTy : 1;
143 // Volatile - True if this alias set contains volatile loads or stores.
146 void addRef() { ++RefCount; }
147 void dropRef(AliasSetTracker &AST) {
148 assert(RefCount >= 1 && "Invalid reference count detected!");
150 removeFromTracker(AST);
153 CallSite getCallSite(unsigned i) const {
154 assert(i < CallSites.size());
155 return CallSite(CallSites[i]);
160 bool isRef() const { return AccessTy & Refs; }
161 bool isMod() const { return AccessTy & Mods; }
162 bool isMustAlias() const { return AliasTy == MustAlias; }
163 bool isMayAlias() const { return AliasTy == MayAlias; }
165 // isVolatile - Return true if this alias set contains volatile loads or
167 bool isVolatile() const { return Volatile; }
169 /// isForwardingAliasSet - Return true if this alias set should be ignored as
170 /// part of the AliasSetTracker object.
171 bool isForwardingAliasSet() const { return Forward; }
173 /// mergeSetIn - Merge the specified alias set into this alias set...
175 void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
177 // Alias Set iteration - Allow access to all of the pointer which are part of
180 iterator begin() const { return iterator(PtrList); }
181 iterator end() const { return iterator(); }
182 bool empty() const { return PtrList == 0; }
184 void print(raw_ostream &OS) const;
187 /// Define an iterator for alias sets... this is just a forward iterator.
188 class iterator : public std::iterator<std::forward_iterator_tag,
189 PointerRec, ptrdiff_t> {
192 explicit iterator(PointerRec *CN = 0) : CurNode(CN) {}
194 bool operator==(const iterator& x) const {
195 return CurNode == x.CurNode;
197 bool operator!=(const iterator& x) const { return !operator==(x); }
199 const iterator &operator=(const iterator &I) {
204 value_type &operator*() const {
205 assert(CurNode && "Dereferencing AliasSet.end()!");
208 value_type *operator->() const { return &operator*(); }
210 Value *getPointer() const { return CurNode->getValue(); }
211 unsigned getSize() const { return CurNode->getSize(); }
212 const MDNode *getRawTBAAInfo() const { return CurNode->getRawTBAAInfo(); }
213 const MDNode *getTBAAInfo() const { return CurNode->getTBAAInfo(); }
215 iterator& operator++() { // Preincrement
216 assert(CurNode && "Advancing past AliasSet.end()!");
217 CurNode = CurNode->getNext();
220 iterator operator++(int) { // Postincrement
221 iterator tmp = *this; ++*this; return tmp;
226 // Can only be created by AliasSetTracker. Also, ilist creates one
227 // to serve as a sentinel.
228 friend struct ilist_sentinel_traits<AliasSet>;
229 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
230 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
233 AliasSet(const AliasSet &AS); // do not implement
234 void operator=(const AliasSet &AS); // do not implement
236 PointerRec *getSomePointer() const {
240 /// getForwardedTarget - Return the real alias set this represents. If this
241 /// has been merged with another set and is forwarding, return the ultimate
242 /// destination set. This also implements the union-find collapsing as well.
243 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
244 if (!Forward) return this;
246 AliasSet *Dest = Forward->getForwardedTarget(AST);
247 if (Dest != Forward) {
249 Forward->dropRef(AST);
255 void removeFromTracker(AliasSetTracker &AST);
257 void addPointer(AliasSetTracker &AST, PointerRec &Entry, unsigned Size,
258 const MDNode *TBAAInfo,
259 bool KnownMustAlias = false);
260 void addCallSite(CallSite CS, AliasAnalysis &AA);
261 void removeCallSite(CallSite CS) {
262 for (size_t i = 0, e = CallSites.size(); i != e; ++i)
263 if (CallSites[i] == CS.getInstruction()) {
264 CallSites[i] = CallSites.back();
265 CallSites.pop_back();
268 void setVolatile() { Volatile = true; }
270 /// aliasesPointer - Return true if the specified pointer "may" (or must)
271 /// alias one of the members in the set.
273 bool aliasesPointer(const Value *Ptr, unsigned Size, const MDNode *TBAAInfo,
274 AliasAnalysis &AA) const;
275 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
278 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) {
284 class AliasSetTracker {
285 /// CallbackVH - A CallbackVH to arrange for AliasSetTracker to be
286 /// notified whenever a Value is deleted.
287 class ASTCallbackVH : public CallbackVH {
288 AliasSetTracker *AST;
289 virtual void deleted();
291 ASTCallbackVH(Value *V, AliasSetTracker *AST = 0);
292 ASTCallbackVH &operator=(Value *V);
294 /// ASTCallbackVHDenseMapInfo - Traits to tell DenseMap that tell us how to
295 /// compare and hash the value handle.
296 struct ASTCallbackVHDenseMapInfo : public DenseMapInfo<Value *> {};
299 ilist<AliasSet> AliasSets;
301 typedef DenseMap<ASTCallbackVH, AliasSet::PointerRec*,
302 ASTCallbackVHDenseMapInfo>
305 // Map from pointers to their node
306 PointerMapType PointerMap;
309 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
310 /// the specified alias analysis object to disambiguate load and store
312 explicit AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
313 ~AliasSetTracker() { clear(); }
315 /// add methods - These methods are used to add different types of
316 /// instructions to the alias sets. Adding a new instruction can result in
317 /// one of three actions happening:
319 /// 1. If the instruction doesn't alias any other sets, create a new set.
320 /// 2. If the instruction aliases exactly one set, add it to the set
321 /// 3. If the instruction aliases multiple sets, merge the sets, and add
322 /// the instruction to the result.
324 /// These methods return true if inserting the instruction resulted in the
325 /// addition of a new alias set (i.e., the pointer did not alias anything).
327 bool add(Value *Ptr, unsigned Size, const MDNode *TBAAInfo); // Add a location
328 bool add(LoadInst *LI);
329 bool add(StoreInst *SI);
330 bool add(VAArgInst *VAAI);
331 bool add(CallSite CS); // Call/Invoke instructions
332 bool add(CallInst *CI) { return add(CallSite(CI)); }
333 bool add(InvokeInst *II) { return add(CallSite(II)); }
334 bool add(Instruction *I); // Dispatch to one of the other add methods...
335 void add(BasicBlock &BB); // Add all instructions in basic block
336 void add(const AliasSetTracker &AST); // Add alias relations from another AST
338 /// remove methods - These methods are used to remove all entries that might
339 /// be aliased by the specified instruction. These methods return true if any
340 /// alias sets were eliminated.
342 bool remove(Value *Ptr, unsigned Size, const MDNode *TBAAInfo);
343 bool remove(LoadInst *LI);
344 bool remove(StoreInst *SI);
345 bool remove(VAArgInst *VAAI);
346 bool remove(CallSite CS);
347 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
348 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
349 bool remove(Instruction *I);
350 void remove(AliasSet &AS);
354 /// getAliasSets - Return the alias sets that are active.
356 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
358 /// getAliasSetForPointer - Return the alias set that the specified pointer
359 /// lives in. If the New argument is non-null, this method sets the value to
360 /// true if a new alias set is created to contain the pointer (because the
361 /// pointer didn't alias anything).
362 AliasSet &getAliasSetForPointer(Value *P, unsigned Size,
363 const MDNode *TBAAInfo,
366 /// getAliasSetForPointerIfExists - Return the alias set containing the
367 /// location specified if one exists, otherwise return null.
368 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size,
369 const MDNode *TBAAInfo) {
370 return findAliasSetForPointer(P, Size, TBAAInfo);
373 /// containsPointer - Return true if the specified location is represented by
374 /// this alias set, false otherwise. This does not modify the AST object or
376 bool containsPointer(Value *P, unsigned Size, const MDNode *TBAAInfo) const;
378 /// getAliasAnalysis - Return the underlying alias analysis object used by
380 AliasAnalysis &getAliasAnalysis() const { return AA; }
382 /// deleteValue method - This method is used to remove a pointer value from
383 /// the AliasSetTracker entirely. It should be used when an instruction is
384 /// deleted from the program to update the AST. If you don't use this, you
385 /// would have dangling pointers to deleted instructions.
387 void deleteValue(Value *PtrVal);
389 /// copyValue - This method should be used whenever a preexisting value in the
390 /// program is copied or cloned, introducing a new value. Note that it is ok
391 /// for clients that use this method to introduce the same value multiple
392 /// times: if the tracker already knows about a value, it will ignore the
395 void copyValue(Value *From, Value *To);
398 typedef ilist<AliasSet>::iterator iterator;
399 typedef ilist<AliasSet>::const_iterator const_iterator;
401 const_iterator begin() const { return AliasSets.begin(); }
402 const_iterator end() const { return AliasSets.end(); }
404 iterator begin() { return AliasSets.begin(); }
405 iterator end() { return AliasSets.end(); }
407 void print(raw_ostream &OS) const;
411 friend class AliasSet;
412 void removeAliasSet(AliasSet *AS);
414 // getEntryFor - Just like operator[] on the map, except that it creates an
415 // entry for the pointer if it doesn't already exist.
416 AliasSet::PointerRec &getEntryFor(Value *V) {
417 AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)];
419 Entry = new AliasSet::PointerRec(V);
423 AliasSet &addPointer(Value *P, unsigned Size, const MDNode *TBAAInfo,
424 AliasSet::AccessType E,
427 AliasSet &AS = getAliasSetForPointer(P, Size, TBAAInfo, &NewSet);
431 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size,
432 const MDNode *TBAAInfo);
434 AliasSet *findAliasSetForCallSite(CallSite CS);
437 inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) {
442 } // End llvm namespace