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(llvm_ostream &OS) const {
160 if (OS.stream()) print(*OS.stream());
162 void print(std::ostream &OS) const;
165 /// Define an iterator for alias sets... this is just a forward iterator.
166 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
167 HashNodePair *CurNode;
169 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
171 bool operator==(const iterator& x) const {
172 return CurNode == x.CurNode;
174 bool operator!=(const iterator& x) const { return !operator==(x); }
176 const iterator &operator=(const iterator &I) {
181 value_type &operator*() const {
182 assert(CurNode && "Dereferencing AliasSet.end()!");
185 value_type *operator->() const { return &operator*(); }
187 Value *getPointer() const { return CurNode->first; }
188 unsigned getSize() const { return CurNode->second.getSize(); }
190 iterator& operator++() { // Preincrement
191 assert(CurNode && "Advancing past AliasSet.end()!");
192 CurNode = CurNode->second.getNext();
195 iterator operator++(int) { // Postincrement
196 iterator tmp = *this; ++*this; return tmp;
201 // Can only be created by AliasSetTracker
202 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
203 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
206 AliasSet(const AliasSet &AS) {
207 assert(0 && "Copy ctor called!?!?!");
211 HashNodePair *getSomePointer() const {
215 /// getForwardedTarget - Return the real alias set this represents. If this
216 /// has been merged with another set and is forwarding, return the ultimate
217 /// destination set. This also implements the union-find collapsing as well.
218 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
219 if (!Forward) return this;
221 AliasSet *Dest = Forward->getForwardedTarget(AST);
222 if (Dest != Forward) {
224 Forward->dropRef(AST);
230 void removeFromTracker(AliasSetTracker &AST);
232 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size,
233 bool KnownMustAlias = false);
234 void addCallSite(CallSite CS, AliasAnalysis &AA);
235 void removeCallSite(CallSite CS) {
236 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
237 if (CallSites[i].getInstruction() == CS.getInstruction()) {
238 CallSites[i] = CallSites.back();
239 CallSites.pop_back();
242 void setVolatile() { Volatile = true; }
244 /// aliasesPointer - Return true if the specified pointer "may" (or must)
245 /// alias one of the members in the set.
247 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
248 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
251 inline llvm_ostream& operator<<(llvm_ostream &OS, const AliasSet &AS) {
255 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
261 class AliasSetTracker {
263 ilist<AliasSet> AliasSets;
265 // Map from pointers to their node
266 hash_map<Value*, AliasSet::PointerRec> PointerMap;
268 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
269 /// the specified alias analysis object to disambiguate load and store
271 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
273 /// add methods - These methods are used to add different types of
274 /// instructions to the alias sets. Adding a new instruction can result in
275 /// one of three actions happening:
277 /// 1. If the instruction doesn't alias any other sets, create a new set.
278 /// 2. If the instruction aliases exactly one set, add it to the set
279 /// 3. If the instruction aliases multiple sets, merge the sets, and add
280 /// the instruction to the result.
282 /// These methods return true if inserting the instruction resulted in the
283 /// addition of a new alias set (i.e., the pointer did not alias anything).
285 bool add(Value *Ptr, unsigned Size); // Add a location
286 bool add(LoadInst *LI);
287 bool add(StoreInst *SI);
288 bool add(FreeInst *FI);
289 bool add(CallSite CS); // Call/Invoke instructions
290 bool add(CallInst *CI) { return add(CallSite(CI)); }
291 bool add(InvokeInst *II) { return add(CallSite(II)); }
292 bool add(Instruction *I); // Dispatch to one of the other add methods...
293 void add(BasicBlock &BB); // Add all instructions in basic block
294 void add(const AliasSetTracker &AST); // Add alias relations from another AST
296 /// remove methods - These methods are used to remove all entries that might
297 /// be aliased by the specified instruction. These methods return true if any
298 /// alias sets were eliminated.
299 bool remove(Value *Ptr, unsigned Size); // Remove a location
300 bool remove(LoadInst *LI);
301 bool remove(StoreInst *SI);
302 bool remove(FreeInst *FI);
303 bool remove(CallSite CS);
304 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
305 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
306 bool remove(Instruction *I);
307 void remove(AliasSet &AS);
314 /// getAliasSets - Return the alias sets that are active.
316 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
318 /// getAliasSetForPointer - Return the alias set that the specified pointer
319 /// lives in. If the New argument is non-null, this method sets the value to
320 /// true if a new alias set is created to contain the pointer (because the
321 /// pointer didn't alias anything).
322 AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
324 /// getAliasSetForPointerIfExists - Return the alias set containing the
325 /// location specified if one exists, otherwise return null.
326 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
327 return findAliasSetForPointer(P, Size);
330 /// containsPointer - Return true if the specified location is represented by
331 /// this alias set, false otherwise. This does not modify the AST object or
333 bool containsPointer(Value *P, unsigned Size) const;
335 /// getAliasAnalysis - Return the underlying alias analysis object used by
337 AliasAnalysis &getAliasAnalysis() const { return AA; }
339 /// deleteValue method - This method is used to remove a pointer value from
340 /// the AliasSetTracker entirely. It should be used when an instruction is
341 /// deleted from the program to update the AST. If you don't use this, you
342 /// would have dangling pointers to deleted instructions.
344 void deleteValue(Value *PtrVal);
346 /// copyValue - This method should be used whenever a preexisting value in the
347 /// program is copied or cloned, introducing a new value. Note that it is ok
348 /// for clients that use this method to introduce the same value multiple
349 /// times: if the tracker already knows about a value, it will ignore the
352 void copyValue(Value *From, Value *To);
355 typedef ilist<AliasSet>::iterator iterator;
356 typedef ilist<AliasSet>::const_iterator const_iterator;
358 const_iterator begin() const { return AliasSets.begin(); }
359 const_iterator end() const { return AliasSets.end(); }
361 iterator begin() { return AliasSets.begin(); }
362 iterator end() { return AliasSets.end(); }
364 void print(llvm_ostream &OS) const {
365 if (OS.stream()) print(*OS.stream());
367 void print(std::ostream &OS) const;
371 friend class AliasSet;
372 void removeAliasSet(AliasSet *AS);
374 AliasSet::HashNodePair &getEntryFor(Value *V) {
375 // Standard operator[], except that it returns the whole pair, not just
377 return *PointerMap.insert(AliasSet::HashNodePair(V,
378 AliasSet::PointerRec())).first;
381 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
384 AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
388 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
390 AliasSet *findAliasSetForCallSite(CallSite CS);
393 inline llvm_ostream& operator<<(llvm_ostream &OS, const AliasSetTracker &AST) {
397 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
402 } // End llvm namespace