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;
83 HashNodePair *PtrList, **PtrListEnd; // Doubly linked list of nodes
84 AliasSet *Forward; // Forwarding pointer
85 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
87 std::vector<CallSite> CallSites; // All calls & invokes in this node
89 // RefCount - Number of nodes pointing to this AliasSet plus the number of
90 // AliasSets forwarding to it.
91 unsigned RefCount : 28;
93 /// AccessType - Keep track of whether this alias set merely refers to the
94 /// locations of memory, whether it modifies the memory, or whether it does
95 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
96 /// ModRef as necessary.
99 NoModRef = 0, Refs = 1, // Ref = bit 1
100 Mods = 2, ModRef = 3 // Mod = bit 2
102 unsigned AccessTy : 2;
104 /// AliasType - Keep track the relationships between the pointers in the set.
105 /// Lattice goes from MustAlias to MayAlias.
108 MustAlias = 0, MayAlias = 1
110 unsigned AliasTy : 1;
112 // Volatile - True if this alias set contains volatile loads or stores.
115 friend class ilist_traits<AliasSet>;
116 AliasSet *getPrev() const { return Prev; }
117 AliasSet *getNext() const { return Next; }
118 void setPrev(AliasSet *P) { Prev = P; }
119 void setNext(AliasSet *N) { Next = N; }
121 void addRef() { ++RefCount; }
122 void dropRef(AliasSetTracker &AST) {
123 assert(RefCount >= 1 && "Invalid reference count detected!");
125 removeFromTracker(AST);
130 bool isRef() const { return AccessTy & Refs; }
131 bool isMod() const { return AccessTy & Mods; }
132 bool isMustAlias() const { return AliasTy == MustAlias; }
133 bool isMayAlias() const { return AliasTy == MayAlias; }
135 // isVolatile - Return true if this alias set contains volatile loads or
137 bool isVolatile() const { return Volatile; }
139 /// isForwardingAliasSet - Return true if this alias set should be ignored as
140 /// part of the AliasSetTracker object.
141 bool isForwardingAliasSet() const { return Forward; }
143 /// mergeSetIn - Merge the specified alias set into this alias set...
145 void mergeSetIn(AliasSet &AS);
147 // Alias Set iteration - Allow access to all of the pointer which are part of
150 iterator begin() const { return iterator(PtrList); }
151 iterator end() const { return iterator(); }
152 bool empty() const { return PtrList == 0; }
154 void print(std::ostream &OS) const;
157 /// Define an iterator for alias sets... this is just a forward iterator.
158 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
159 HashNodePair *CurNode;
161 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
163 bool operator==(const iterator& x) const {
164 return CurNode == x.CurNode;
166 bool operator!=(const iterator& x) const { return !operator==(x); }
168 const iterator &operator=(const iterator &I) {
173 value_type &operator*() const {
174 assert(CurNode && "Dereferencing AliasSet.end()!");
177 value_type *operator->() const { return &operator*(); }
179 Value *getPointer() const { return CurNode->first; }
180 unsigned getSize() const { return CurNode->second.getSize(); }
182 iterator& operator++() { // Preincrement
183 assert(CurNode && "Advancing past AliasSet.end()!");
184 CurNode = CurNode->second.getNext();
187 iterator operator++(int) { // Postincrement
188 iterator tmp = *this; ++*this; return tmp;
193 // Can only be created by AliasSetTracker
194 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
195 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
198 AliasSet(const AliasSet &AS) {
199 assert(0 && "Copy ctor called!?!?!");
203 HashNodePair *getSomePointer() const {
207 /// getForwardedTarget - Return the real alias set this represents. If this
208 /// has been merged with another set and is forwarding, return the ultimate
209 /// destination set. This also implements the union-find collapsing as well.
210 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
211 if (!Forward) return this;
213 AliasSet *Dest = Forward->getForwardedTarget(AST);
214 if (Dest != Forward) {
216 Forward->dropRef(AST);
222 void removeFromTracker(AliasSetTracker &AST);
224 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size,
225 bool KnownMustAlias = false);
226 void addCallSite(CallSite CS, AliasAnalysis &AA);
227 void setVolatile() { Volatile = true; }
229 /// aliasesPointer - Return true if the specified pointer "may" (or must)
230 /// alias one of the members in the set.
232 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
233 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
236 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
242 class AliasSetTracker {
244 ilist<AliasSet> AliasSets;
246 // Map from pointers to their node
247 hash_map<Value*, AliasSet::PointerRec> PointerMap;
249 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
250 /// the specified alias analysis object to disambiguate load and store
252 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
254 /// add methods - These methods are used to add different types of
255 /// instructions to the alias sets. Adding a new instruction can result in
256 /// one of three actions happening:
258 /// 1. If the instruction doesn't alias any other sets, create a new set.
259 /// 2. If the instruction aliases exactly one set, add it to the set
260 /// 3. If the instruction aliases multiple sets, merge the sets, and add
261 /// the instruction to the result.
263 /// These methods return true if inserting the instruction resulted in the
264 /// addition of a new alias set (i.e., the pointer did not alias anything).
266 bool add(Value *Ptr, unsigned Size); // Add a location
267 bool add(LoadInst *LI);
268 bool add(StoreInst *SI);
269 bool add(FreeInst *FI);
270 bool add(CallSite CS); // Call/Invoke instructions
271 bool add(CallInst *CI) { return add(CallSite(CI)); }
272 bool add(InvokeInst *II) { return add(CallSite(II)); }
273 bool add(Instruction *I); // Dispatch to one of the other add methods...
274 void add(BasicBlock &BB); // Add all instructions in basic block
275 void add(const AliasSetTracker &AST); // Add alias relations from another AST
277 /// remove methods - These methods are used to remove all entries that might
278 /// be aliased by the specified instruction. These methods return true if any
279 /// alias sets were eliminated.
280 bool remove(Value *Ptr, unsigned Size); // Remove a location
281 bool remove(LoadInst *LI);
282 bool remove(StoreInst *SI);
283 bool remove(FreeInst *FI);
284 bool remove(CallSite CS);
285 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
286 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
287 bool remove(Instruction *I);
288 void remove(AliasSet &AS);
290 /// getAliasSets - Return the alias sets that are active.
292 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
294 /// getAliasSetForPointer - Return the alias set that the specified pointer
295 /// lives in. If the New argument is non-null, this method sets the value to
296 /// true if a new alias set is created to contain the pointer (because the
297 /// pointer didn't alias anything).
298 AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
300 /// getAliasSetForPointerIfExists - Return the alias set containing the
301 /// location specified if one exists, otherwise return null.
302 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
303 return findAliasSetForPointer(P, Size);
306 /// getAliasAnalysis - Return the underlying alias analysis object used by
308 AliasAnalysis &getAliasAnalysis() const { return AA; }
310 /// deleteValue method - This method is used to remove a pointer value from
311 /// the AliasSetTracker entirely. It should be used when an instruction is
312 /// deleted from the program to update the AST. If you don't use this, you
313 /// would have dangling pointers to deleted instructions.
315 void deleteValue(Value *PtrVal);
317 /// copyValue - This method should be used whenever a preexisting value in the
318 /// program is copied or cloned, introducing a new value. Note that it is ok
319 /// for clients that use this method to introduce the same value multiple
320 /// times: if the tracker already knows about a value, it will ignore the
323 void copyValue(Value *From, Value *To);
326 typedef ilist<AliasSet>::iterator iterator;
327 typedef ilist<AliasSet>::const_iterator const_iterator;
329 const_iterator begin() const { return AliasSets.begin(); }
330 const_iterator end() const { return AliasSets.end(); }
332 iterator begin() { return AliasSets.begin(); }
333 iterator end() { return AliasSets.end(); }
335 void print(std::ostream &OS) const;
339 friend class AliasSet;
340 void removeAliasSet(AliasSet *AS);
342 AliasSet::HashNodePair &getEntryFor(Value *V) {
343 // Standard operator[], except that it returns the whole pair, not just
345 return *PointerMap.insert(AliasSet::HashNodePair(V,
346 AliasSet::PointerRec())).first;
349 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
352 AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
356 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
358 AliasSet *findAliasSetForCallSite(CallSite CS);
361 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
366 } // End llvm namespace