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"
27 class AliasSetTracker;
31 friend class AliasSetTracker;
34 typedef std::pair<Value* const, PointerRec> HashNodePair;
37 HashNodePair *NextInList;
41 PointerRec() : NextInList(0), AS(0), Size(0) {}
43 HashNodePair *getNext() const { return NextInList; }
44 bool hasAliasSet() const { return AS != 0; }
46 void updateSize(unsigned NewSize) {
47 if (NewSize > Size) Size = NewSize;
50 unsigned getSize() const { return Size; }
52 AliasSet *getAliasSet(AliasSetTracker &AST) {
53 assert(AS && "No AliasSet yet!");
56 AS = OldAS->getForwardedTarget(AST);
57 if (--OldAS->RefCount == 0)
58 OldAS->removeFromTracker(AST);
64 void setAliasSet(AliasSet *as) {
65 assert(AS == 0 && "Already have an alias set!");
68 void setTail(HashNodePair *T) {
69 assert(NextInList == 0 && "Already have tail!");
74 HashNodePair *PtrListHead, *PtrListTail; // Singly linked list of nodes
75 AliasSet *Forward; // Forwarding pointer
76 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
78 std::vector<CallSite> CallSites; // All calls & invokes in this node
80 // RefCount - Number of nodes pointing to this AliasSet plus the number of
81 // AliasSets forwarding to it.
82 unsigned RefCount : 29;
84 /// AccessType - Keep track of whether this alias set merely refers to the
85 /// locations of memory, whether it modifies the memory, or whether it does
86 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
87 /// ModRef as necessary.
90 NoModRef = 0, Refs = 1, // Ref = bit 1
91 Mods = 2, ModRef = 3 // Mod = bit 2
93 unsigned AccessTy : 2;
95 /// AliasType - Keep track the relationships between the pointers in the set.
96 /// Lattice goes from MustAlias to MayAlias.
99 MustAlias = 0, MayAlias = 1
101 unsigned AliasTy : 1;
103 friend class ilist_traits<AliasSet>;
104 AliasSet *getPrev() const { return Prev; }
105 AliasSet *getNext() const { return Next; }
106 void setPrev(AliasSet *P) { Prev = P; }
107 void setNext(AliasSet *N) { Next = N; }
111 bool isRef() const { return AccessTy & Refs; }
112 bool isMod() const { return AccessTy & Mods; }
113 bool isMustAlias() const { return AliasTy == MustAlias; }
114 bool isMayAlias() const { return AliasTy == MayAlias; }
116 /// isForwardingAliasSet - Return true if this alias set should be ignored as
117 /// part of the AliasSetTracker object.
118 bool isForwardingAliasSet() const { return Forward; }
120 /// mergeSetIn - Merge the specified alias set into this alias set...
122 void mergeSetIn(AliasSet &AS);
124 // Alias Set iteration - Allow access to all of the pointer which are part of
127 iterator begin() const { return iterator(PtrListHead); }
128 iterator end() const { return iterator(); }
130 void print(std::ostream &OS) const;
133 /// Define an iterator for alias sets... this is just a forward iterator.
134 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
135 HashNodePair *CurNode;
137 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
139 bool operator==(const iterator& x) const {
140 return CurNode == x.CurNode;
142 bool operator!=(const iterator& x) const { return !operator==(x); }
144 const iterator &operator=(const iterator &I) {
149 value_type &operator*() const {
150 assert(CurNode && "Dereferencing AliasSet.end()!");
153 value_type *operator->() const { return &operator*(); }
155 iterator& operator++() { // Preincrement
156 assert(CurNode && "Advancing past AliasSet.end()!");
157 CurNode = CurNode->second.getNext();
160 iterator operator++(int) { // Postincrement
161 iterator tmp = *this; ++*this; return tmp;
166 // Can only be created by AliasSetTracker
167 AliasSet() : PtrListHead(0), PtrListTail(0), Forward(0), RefCount(0),
168 AccessTy(NoModRef), AliasTy(MustAlias) {
170 HashNodePair *getSomePointer() const {
171 return PtrListHead ? PtrListHead : 0;
174 /// getForwardedTarget - Return the real alias set this represents. If this
175 /// has been merged with another set and is forwarding, return the ultimate
176 /// destination set. This also implements the union-find collapsing as well.
177 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
178 if (!Forward) return this;
180 AliasSet *Dest = Forward->getForwardedTarget(AST);
181 if (Dest != Forward) {
183 if (--Forward->RefCount == 0)
184 Forward->removeFromTracker(AST);
190 void removeFromTracker(AliasSetTracker &AST);
192 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
193 void addCallSite(CallSite CS);
195 /// aliasesPointer - Return true if the specified pointer "may" (or must)
196 /// alias one of the members in the set.
198 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
199 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
202 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
208 class AliasSetTracker {
210 ilist<AliasSet> AliasSets;
212 // Map from pointers to their node
213 hash_map<Value*, AliasSet::PointerRec> PointerMap;
215 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
216 /// the specified alias analysis object to disambiguate load and store
218 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
220 /// add methods - These methods are used to add different types of
221 /// instructions to the alias sets. Adding a new instruction can result in
222 /// one of three actions happening:
224 /// 1. If the instruction doesn't alias any other sets, create a new set.
225 /// 2. If the instruction aliases exactly one set, add it to the set
226 /// 3. If the instruction aliases multiple sets, merge the sets, and add
227 /// the instruction to the result.
229 void add(LoadInst *LI);
230 void add(StoreInst *SI);
231 void add(CallSite CS); // Call/Invoke instructions
232 void add(CallInst *CI) { add(CallSite(CI)); }
233 void add(InvokeInst *II) { add(CallSite(II)); }
234 void add(Instruction *I); // Dispatch to one of the other add methods...
235 void add(BasicBlock &BB); // Add all instructions in basic block
236 void add(const AliasSetTracker &AST); // Add alias relations from another AST
238 /// getAliasSets - Return the alias sets that are active.
239 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
241 /// getAliasSetForPointer - Return the alias set that the specified pointer
243 AliasSet &getAliasSetForPointer(Value *P, unsigned Size);
245 /// getAliasAnalysis - Return the underlying alias analysis object used by
247 AliasAnalysis &getAliasAnalysis() const { return AA; }
249 typedef ilist<AliasSet>::iterator iterator;
250 typedef ilist<AliasSet>::const_iterator const_iterator;
252 const_iterator begin() const { return AliasSets.begin(); }
253 const_iterator end() const { return AliasSets.end(); }
255 iterator begin() { return AliasSets.begin(); }
256 iterator end() { return AliasSets.end(); }
258 void print(std::ostream &OS) const;
262 friend class AliasSet;
263 void removeAliasSet(AliasSet *AS);
265 AliasSet::HashNodePair &getEntryFor(Value *V) {
266 // Standard operator[], except that it returns the whole pair, not just
268 return *PointerMap.insert(AliasSet::HashNodePair(V,
269 AliasSet::PointerRec())).first;
272 void addPointer(Value *P, unsigned Size, AliasSet::AccessType E) {
273 AliasSet &AS = getAliasSetForPointer(P, Size);
276 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
278 AliasSet *findAliasSetForCallSite(CallSite CS);
281 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {