1 //===- llvm/Analysis/AliasSetTracker.h - Build Alias Sets -------*- C++ -*-===//
3 // This file defines two classes: AliasSetTracker and AliasSet. These interface
4 // are used to classify a collection of pointer references into a maximal number
5 // of disjoint sets. Each AliasSet object constructed by the AliasSetTracker
6 // object refers to memory disjoint from the other sets.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
11 #define LLVM_ANALYSIS_ALIASSETTRACKER_H
13 #include "llvm/Support/CallSite.h"
14 #include "Support/iterator"
15 #include "Support/hash_map"
16 #include "Support/ilist"
20 class AliasSetTracker;
24 friend class AliasSetTracker;
27 typedef std::pair<Value* const, PointerRec> HashNodePair;
30 HashNodePair *NextInList;
33 PointerRec() : NextInList(0), AS(0) {}
35 HashNodePair *getNext() const { return NextInList; }
36 bool hasAliasSet() const { return AS != 0; }
38 AliasSet *getAliasSet(AliasSetTracker &AST) {
39 assert(AS && "No AliasSet yet!");
42 AS = OldAS->getForwardedTarget(AST);
43 if (--OldAS->RefCount == 0)
44 OldAS->removeFromTracker(AST);
50 void setAliasSet(AliasSet *as) {
51 assert(AS == 0 && "Already have an alias set!");
54 void setTail(HashNodePair *T) {
55 assert(NextInList == 0 && "Already have tail!");
60 HashNodePair *PtrListHead, *PtrListTail; // Singly linked list of nodes
61 AliasSet *Forward; // Forwarding pointer
62 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
64 std::vector<CallSite> CallSites; // All calls & invokes in this node
66 // RefCount - Number of nodes pointing to this AliasSet plus the number of
67 // AliasSets forwarding to it.
68 unsigned RefCount : 29;
70 /// AccessType - Keep track of whether this alias set merely refers to the
71 /// locations of memory, whether it modifies the memory, or whether it does
72 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
73 /// ModRef as neccesary.
76 NoModRef = 0, Refs = 1, // Ref = bit 1
77 Mods = 2, ModRef = 3 // Mod = bit 2
79 unsigned AccessTy : 2;
81 /// AliasType - Keep track the relationships between the pointers in the set.
82 /// Lattice goes from MustAlias to MayAlias.
85 MustAlias = 0, MayAlias = 1
89 /// Define an iterator for alias sets... this is just a forward iterator.
90 class iterator : public forward_iterator<Value*, ptrdiff_t> {
91 HashNodePair *CurNode;
93 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
95 bool operator==(const iterator& x) const {
96 return CurNode == x.CurNode;
98 bool operator!=(const iterator& x) const { return !operator==(x); }
100 const iterator &operator=(const iterator &I) {
105 value_type operator*() const {
106 assert(CurNode && "Dereferencing AliasSet.end()!");
107 return CurNode->first;
109 value_type operator->() const { return operator*(); }
111 iterator& operator++() { // Preincrement
112 assert(CurNode && "Advancing past AliasSet.end()!");
113 CurNode = CurNode->second.getNext();
116 iterator operator++(int) { // Postincrement
117 iterator tmp = *this; ++*this; return tmp;
121 friend class ilist_traits<AliasSet>;
122 AliasSet *getPrev() const { return Prev; }
123 AliasSet *getNext() const { return Next; }
124 void setPrev(AliasSet *P) { Prev = P; }
125 void setNext(AliasSet *N) { Next = N; }
129 bool isRef() const { return AccessTy & Refs; }
130 bool isMod() const { return AccessTy & Mods; }
131 bool isMustAlias() const { return AliasTy == MustAlias; }
132 bool isMayAlias() const { return AliasTy == MayAlias; }
134 /// mergeSetIn - Merge the specified alias set into this alias set...
136 void mergeSetIn(AliasSet &AS);
138 // Alias Set iteration - Allow access to all of the pointer which are part of
140 iterator begin() const { return iterator(PtrListHead); }
141 iterator end() const { return iterator(); }
143 void print(std::ostream &OS) const;
147 // Can only be created by AliasSetTracker
148 AliasSet() : PtrListHead(0), PtrListTail(0), Forward(0), RefCount(0),
149 AccessTy(NoModRef), AliasTy(MustAlias) {
151 Value *getSomePointer() const {
152 return PtrListHead ? PtrListHead->first : 0;
155 /// getForwardedTarget - Return the real alias set this represents. If this
156 /// has been merged with another set and is forwarding, return the ultimate
157 /// destination set. This also implements the union-find collapsing as well.
158 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
159 if (!Forward) return this;
161 AliasSet *Dest = Forward->getForwardedTarget(AST);
162 if (Dest != Forward) {
164 if (--Forward->RefCount == 0)
165 Forward->removeFromTracker(AST);
171 void removeFromTracker(AliasSetTracker &AST);
173 void addPointer(AliasSetTracker &AST, HashNodePair &Entry);
174 void addCallSite(CallSite CS);
176 /// aliasesPointer - Return true if the specified pointer "may" (or must)
177 /// alias one of the members in the set.
179 bool aliasesPointer(const Value *Ptr, AliasAnalysis &AA) const;
180 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
183 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
189 class AliasSetTracker {
191 ilist<AliasSet> AliasSets;
193 // Map from pointers to their node
194 hash_map<Value*, AliasSet::PointerRec> PointerMap;
196 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
197 /// the specified alias analysis object to disambiguate load and store
199 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
201 /// add methods - These methods are used to add different types of
202 /// instructions to the alias sets. Adding a new instruction can result in
203 /// one of three actions happening:
205 /// 1. If the instruction doesn't alias any other sets, create a new set.
206 /// 2. If the instruction aliases exactly one set, add it to the set
207 /// 3. If the instruction aliases multiple sets, merge the sets, and add
208 /// the instruction to the result.
210 void add(LoadInst *LI);
211 void add(StoreInst *SI);
212 void add(CallSite CS); // Call/Invoke instructions
213 void add(CallInst *CI) { add(CallSite(CI)); }
214 void add(InvokeInst *II) { add(CallSite(II)); }
215 void add(Instruction *I); // Dispatch to one of the other add methods...
217 /// getAliasSets - Return the alias sets that are active.
218 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
220 /// getAliasSetForPointer - Return the alias set that the specified pointer
222 AliasSet &getAliasSetForPointer(Value *P);
224 /// getAliasAnalysis - Return the underlying alias analysis object used by
226 AliasAnalysis &getAliasAnalysis() const { return AA; }
228 typedef ilist<AliasSet>::iterator iterator;
229 typedef ilist<AliasSet>::const_iterator const_iterator;
231 const_iterator begin() const { return AliasSets.begin(); }
232 const_iterator end() const { return AliasSets.end(); }
234 iterator begin() { return AliasSets.begin(); }
235 iterator end() { return AliasSets.end(); }
237 void print(std::ostream &OS) const;
241 friend class AliasSet;
242 void removeAliasSet(AliasSet *AS);
244 AliasSet::HashNodePair &getEntryFor(Value *V) {
245 // Standard operator[], except that it returns the whole pair, not just
247 return *PointerMap.insert(AliasSet::HashNodePair(V,
248 AliasSet::PointerRec())).first;
251 void addPointer(Value *P, AliasSet::AccessType E) {
252 AliasSet &AS = getAliasSetForPointer(P);
255 AliasSet *findAliasSetForPointer(const Value *Ptr);
257 AliasSet *findAliasSetForCallSite(CallSite CS);
260 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {