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; }
133 /// isForwardingAliasSet - Return true if this alias set should be ignored as
134 /// part of the AliasSetTracker object.
135 bool isForwardingAliasSet() const { return Forward; }
137 /// mergeSetIn - Merge the specified alias set into this alias set...
139 void mergeSetIn(AliasSet &AS);
141 // Alias Set iteration - Allow access to all of the pointer which are part of
144 iterator begin() const { return iterator(PtrList); }
145 iterator end() const { return iterator(); }
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 iterator& operator++() { // Preincrement
173 assert(CurNode && "Advancing past AliasSet.end()!");
174 CurNode = CurNode->second.getNext();
177 iterator operator++(int) { // Postincrement
178 iterator tmp = *this; ++*this; return tmp;
183 // Can only be created by AliasSetTracker
184 AliasSet() : PtrList(0), PtrListEnd(&PtrList), Forward(0), RefCount(0),
185 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
187 AliasSet(const AliasSet &AS) {
188 // AliasSet's only get copy constructed in simple circumstances. In
189 // particular, they cannot have any pointers in their list. Despite this,
190 // we have to be sure to update the PtrListEnd to not point to the source
192 assert(AS.PtrList == 0 && "AliasSet has pointers in it!");
193 PtrList = 0; PtrListEnd = &PtrList;
194 Forward = AS.Forward; RefCount = AS.RefCount;
195 AccessTy = AS.AccessTy; AliasTy = AS.AliasTy; Volatile = AS.Volatile;
198 HashNodePair *getSomePointer() const {
202 /// getForwardedTarget - Return the real alias set this represents. If this
203 /// has been merged with another set and is forwarding, return the ultimate
204 /// destination set. This also implements the union-find collapsing as well.
205 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
206 if (!Forward) return this;
208 AliasSet *Dest = Forward->getForwardedTarget(AST);
209 if (Dest != Forward) {
211 if (--Forward->RefCount == 0)
212 Forward->removeFromTracker(AST);
218 void removeFromTracker(AliasSetTracker &AST);
220 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
221 void addCallSite(CallSite CS);
222 void setVolatile() { Volatile = true; }
224 /// aliasesPointer - Return true if the specified pointer "may" (or must)
225 /// alias one of the members in the set.
227 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
228 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
231 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
237 class AliasSetTracker {
239 ilist<AliasSet> AliasSets;
241 // Map from pointers to their node
242 hash_map<Value*, AliasSet::PointerRec> PointerMap;
244 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
245 /// the specified alias analysis object to disambiguate load and store
247 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
249 /// add methods - These methods are used to add different types of
250 /// instructions to the alias sets. Adding a new instruction can result in
251 /// one of three actions happening:
253 /// 1. If the instruction doesn't alias any other sets, create a new set.
254 /// 2. If the instruction aliases exactly one set, add it to the set
255 /// 3. If the instruction aliases multiple sets, merge the sets, and add
256 /// the instruction to the result.
258 void add(LoadInst *LI);
259 void add(StoreInst *SI);
260 void add(CallSite CS); // Call/Invoke instructions
261 void add(CallInst *CI) { add(CallSite(CI)); }
262 void add(InvokeInst *II) { add(CallSite(II)); }
263 void add(Instruction *I); // Dispatch to one of the other add methods...
264 void add(BasicBlock &BB); // Add all instructions in basic block
265 void add(const AliasSetTracker &AST); // Add alias relations from another AST
267 /// remove method - This method is used to remove a pointer value from the
268 /// AliasSetTracker entirely. It should be used when an instruction is
269 /// deleted from the program to update the AST. If you don't use this, you
270 /// would have dangling pointers to deleted instructions.
272 void remove(Value *PtrVal);
274 /// getAliasSets - Return the alias sets that are active.
275 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
277 /// getAliasSetForPointer - Return the alias set that the specified pointer
279 AliasSet &getAliasSetForPointer(Value *P, unsigned Size);
281 /// getAliasAnalysis - Return the underlying alias analysis object used by
283 AliasAnalysis &getAliasAnalysis() const { return AA; }
285 typedef ilist<AliasSet>::iterator iterator;
286 typedef ilist<AliasSet>::const_iterator const_iterator;
288 const_iterator begin() const { return AliasSets.begin(); }
289 const_iterator end() const { return AliasSets.end(); }
291 iterator begin() { return AliasSets.begin(); }
292 iterator end() { return AliasSets.end(); }
294 void print(std::ostream &OS) const;
298 friend class AliasSet;
299 void removeAliasSet(AliasSet *AS);
301 AliasSet::HashNodePair &getEntryFor(Value *V) {
302 // Standard operator[], except that it returns the whole pair, not just
304 return *PointerMap.insert(AliasSet::HashNodePair(V,
305 AliasSet::PointerRec())).first;
308 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E) {
309 AliasSet &AS = getAliasSetForPointer(P, Size);
313 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
315 AliasSet *findAliasSetForCallSite(CallSite CS);
318 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
323 } // End llvm namespace