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 "Support/iterator"
22 #include "Support/hash_map"
23 #include "Support/ilist"
30 class AliasSetTracker;
34 friend class AliasSetTracker;
37 typedef std::pair<Value* const, PointerRec> HashNodePair;
40 HashNodePair **PrevInList, *NextInList;
44 PointerRec() : PrevInList(0), NextInList(0), AS(0), Size(0) {}
46 HashNodePair *getNext() const { return NextInList; }
47 bool hasAliasSet() const { return AS != 0; }
49 HashNodePair** setPrevInList(HashNodePair **PIL) {
54 void updateSize(unsigned NewSize) {
55 if (NewSize > Size) Size = NewSize;
58 unsigned getSize() const { return Size; }
60 AliasSet *getAliasSet(AliasSetTracker &AST) {
61 assert(AS && "No AliasSet yet!");
64 AS = OldAS->getForwardedTarget(AST);
65 if (--OldAS->RefCount == 0)
66 OldAS->removeFromTracker(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; }
123 bool isRef() const { return AccessTy & Refs; }
124 bool isMod() const { return AccessTy & Mods; }
125 bool isMustAlias() const { return AliasTy == MustAlias; }
126 bool isMayAlias() const { return AliasTy == MayAlias; }
128 // isVolatile - Return true if this alias set contains volatile loads or
130 bool isVolatile() const { return Volatile; }
132 /// isForwardingAliasSet - Return true if this alias set should be ignored as
133 /// part of the AliasSetTracker object.
134 bool isForwardingAliasSet() const { return Forward; }
136 /// mergeSetIn - Merge the specified alias set into this alias set...
138 void mergeSetIn(AliasSet &AS);
140 // Alias Set iteration - Allow access to all of the pointer which are part of
143 iterator begin() const { return iterator(PtrList); }
144 iterator end() const { return iterator(); }
145 bool empty() const { return PtrList == 0; }
147 void print(std::ostream &OS) const;
150 /// Define an iterator for alias sets... this is just a forward iterator.
151 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
152 HashNodePair *CurNode;
154 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
156 bool operator==(const iterator& x) const {
157 return CurNode == x.CurNode;
159 bool operator!=(const iterator& x) const { return !operator==(x); }
161 const iterator &operator=(const iterator &I) {
166 value_type &operator*() const {
167 assert(CurNode && "Dereferencing AliasSet.end()!");
170 value_type *operator->() const { return &operator*(); }
172 Value *getPointer() const { return CurNode->first; }
173 unsigned getSize() const { return CurNode->second.getSize(); }
175 iterator& operator++() { // Preincrement
176 assert(CurNode && "Advancing past AliasSet.end()!");
177 CurNode = CurNode->second.getNext();
180 iterator operator++(int) { // Postincrement
181 iterator tmp = *this; ++*this; return tmp;
186 // Can only be created by AliasSetTracker
187 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
188 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
190 AliasSet(const AliasSet &AS) {
191 // AliasSet's only get copy constructed in simple circumstances. In
192 // particular, they cannot have any pointers in their list. Despite this,
193 // we have to be sure to update the PtrListEnd to not point to the source
195 assert(AS.PtrList == 0 && "AliasSet has pointers in it!");
196 PtrList = 0; PtrListEnd = &PtrList;
197 Forward = AS.Forward; RefCount = AS.RefCount;
198 AccessTy = AS.AccessTy; AliasTy = AS.AliasTy; Volatile = AS.Volatile;
201 HashNodePair *getSomePointer() const {
205 /// getForwardedTarget - Return the real alias set this represents. If this
206 /// has been merged with another set and is forwarding, return the ultimate
207 /// destination set. This also implements the union-find collapsing as well.
208 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
209 if (!Forward) return this;
211 AliasSet *Dest = Forward->getForwardedTarget(AST);
212 if (Dest != Forward) {
214 if (--Forward->RefCount == 0)
215 Forward->removeFromTracker(AST);
221 void removeFromTracker(AliasSetTracker &AST);
223 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
224 void addCallSite(CallSite CS, AliasAnalysis &AA);
225 void setVolatile() { Volatile = true; }
227 /// aliasesPointer - Return true if the specified pointer "may" (or must)
228 /// alias one of the members in the set.
230 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
231 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
234 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
240 class AliasSetTracker {
242 ilist<AliasSet> AliasSets;
244 // Map from pointers to their node
245 hash_map<Value*, AliasSet::PointerRec> PointerMap;
247 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
248 /// the specified alias analysis object to disambiguate load and store
250 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
252 /// add methods - These methods are used to add different types of
253 /// instructions to the alias sets. Adding a new instruction can result in
254 /// one of three actions happening:
256 /// 1. If the instruction doesn't alias any other sets, create a new set.
257 /// 2. If the instruction aliases exactly one set, add it to the set
258 /// 3. If the instruction aliases multiple sets, merge the sets, and add
259 /// the instruction to the result.
261 /// These methods return true if inserting the instruction resulted in the
262 /// addition of a new alias set (i.e., the pointer did not alias anything).
264 bool add(LoadInst *LI);
265 bool add(StoreInst *SI);
266 bool add(CallSite CS); // Call/Invoke instructions
267 bool add(CallInst *CI) { return add(CallSite(CI)); }
268 bool add(InvokeInst *II) { return add(CallSite(II)); }
269 bool add(Instruction *I); // Dispatch to one of the other add methods...
270 void add(BasicBlock &BB); // Add all instructions in basic block
271 void add(const AliasSetTracker &AST); // Add alias relations from another AST
273 /// remove methods - These methods are used to remove all entries that might
274 /// be aliased by the specified instruction. These methods return true if any
275 /// alias sets were eliminated.
276 bool remove(LoadInst *LI);
277 bool remove(StoreInst *SI);
278 bool remove(CallSite CS);
279 bool remove(CallInst *CI) { return remove(CallSite(CI)); }
280 bool remove(InvokeInst *II) { return remove(CallSite(II)); }
281 bool remove(Instruction *I);
282 void remove(AliasSet &AS);
285 /// deleteValue method - This method is used to remove a pointer value from
286 /// the AliasSetTracker entirely. It should be used when an instruction is
287 /// deleted from the program to update the AST. If you don't use this, you
288 /// would have dangling pointers to deleted instructions.
290 void deleteValue(Value *PtrVal);
292 /// getAliasSets - Return the alias sets that are active.
293 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
295 /// getAliasSetForPointer - Return the alias set that the specified pointer
296 /// lives in. If the New argument is non-null, this method sets the value to
297 /// true if a new alias set is created to contain the pointer (because the
298 /// pointer didn't alias anything).
299 AliasSet &getAliasSetForPointer(Value *P, unsigned Size, bool *New = 0);
301 /// getAliasSetForPointerIfExists - Return the alias set containing the
302 /// location specified if one exists, otherwise return null.
303 AliasSet *getAliasSetForPointerIfExists(Value *P, unsigned Size) {
304 return findAliasSetForPointer(P, Size);
307 /// getAliasAnalysis - Return the underlying alias analysis object used by
309 AliasAnalysis &getAliasAnalysis() const { return AA; }
311 typedef ilist<AliasSet>::iterator iterator;
312 typedef ilist<AliasSet>::const_iterator const_iterator;
314 const_iterator begin() const { return AliasSets.begin(); }
315 const_iterator end() const { return AliasSets.end(); }
317 iterator begin() { return AliasSets.begin(); }
318 iterator end() { return AliasSets.end(); }
320 void print(std::ostream &OS) const;
324 friend class AliasSet;
325 void removeAliasSet(AliasSet *AS);
327 AliasSet::HashNodePair &getEntryFor(Value *V) {
328 // Standard operator[], except that it returns the whole pair, not just
330 return *PointerMap.insert(AliasSet::HashNodePair(V,
331 AliasSet::PointerRec())).first;
334 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E,
337 AliasSet &AS = getAliasSetForPointer(P, Size, &NewSet);
341 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
343 AliasSet *findAliasSetForCallSite(CallSite CS);
346 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
351 } // End llvm namespace