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
15 #include "llvm/Support/CallSite.h"
16 #include "Support/iterator"
17 #include "Support/hash_map"
18 #include "Support/ilist"
22 class AliasSetTracker;
26 friend class AliasSetTracker;
29 typedef std::pair<Value* const, PointerRec> HashNodePair;
32 HashNodePair *NextInList;
36 PointerRec() : NextInList(0), AS(0), Size(0) {}
38 HashNodePair *getNext() const { return NextInList; }
39 bool hasAliasSet() const { return AS != 0; }
41 void updateSize(unsigned NewSize) {
42 if (NewSize > Size) Size = NewSize;
45 unsigned getSize() const { return Size; }
47 AliasSet *getAliasSet(AliasSetTracker &AST) {
48 assert(AS && "No AliasSet yet!");
51 AS = OldAS->getForwardedTarget(AST);
52 if (--OldAS->RefCount == 0)
53 OldAS->removeFromTracker(AST);
59 void setAliasSet(AliasSet *as) {
60 assert(AS == 0 && "Already have an alias set!");
63 void setTail(HashNodePair *T) {
64 assert(NextInList == 0 && "Already have tail!");
69 HashNodePair *PtrListHead, *PtrListTail; // Singly linked list of nodes
70 AliasSet *Forward; // Forwarding pointer
71 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
73 std::vector<CallSite> CallSites; // All calls & invokes in this node
75 // RefCount - Number of nodes pointing to this AliasSet plus the number of
76 // AliasSets forwarding to it.
77 unsigned RefCount : 29;
79 /// AccessType - Keep track of whether this alias set merely refers to the
80 /// locations of memory, whether it modifies the memory, or whether it does
81 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
82 /// ModRef as neccesary.
85 NoModRef = 0, Refs = 1, // Ref = bit 1
86 Mods = 2, ModRef = 3 // Mod = bit 2
88 unsigned AccessTy : 2;
90 /// AliasType - Keep track the relationships between the pointers in the set.
91 /// Lattice goes from MustAlias to MayAlias.
94 MustAlias = 0, MayAlias = 1
98 friend class ilist_traits<AliasSet>;
99 AliasSet *getPrev() const { return Prev; }
100 AliasSet *getNext() const { return Next; }
101 void setPrev(AliasSet *P) { Prev = P; }
102 void setNext(AliasSet *N) { Next = N; }
106 bool isRef() const { return AccessTy & Refs; }
107 bool isMod() const { return AccessTy & Mods; }
108 bool isMustAlias() const { return AliasTy == MustAlias; }
109 bool isMayAlias() const { return AliasTy == MayAlias; }
111 /// isForwardingAliasSet - Return true if this alias set should be ignored as
112 /// part of the AliasSetTracker object.
113 bool isForwardingAliasSet() const { return Forward; }
115 /// mergeSetIn - Merge the specified alias set into this alias set...
117 void mergeSetIn(AliasSet &AS);
119 // Alias Set iteration - Allow access to all of the pointer which are part of
122 iterator begin() const { return iterator(PtrListHead); }
123 iterator end() const { return iterator(); }
125 void print(std::ostream &OS) const;
128 /// Define an iterator for alias sets... this is just a forward iterator.
129 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
130 HashNodePair *CurNode;
132 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
134 bool operator==(const iterator& x) const {
135 return CurNode == x.CurNode;
137 bool operator!=(const iterator& x) const { return !operator==(x); }
139 const iterator &operator=(const iterator &I) {
144 value_type &operator*() const {
145 assert(CurNode && "Dereferencing AliasSet.end()!");
148 value_type *operator->() const { return &operator*(); }
150 iterator& operator++() { // Preincrement
151 assert(CurNode && "Advancing past AliasSet.end()!");
152 CurNode = CurNode->second.getNext();
155 iterator operator++(int) { // Postincrement
156 iterator tmp = *this; ++*this; return tmp;
161 // Can only be created by AliasSetTracker
162 AliasSet() : PtrListHead(0), PtrListTail(0), Forward(0), RefCount(0),
163 AccessTy(NoModRef), AliasTy(MustAlias) {
165 HashNodePair *getSomePointer() const {
166 return PtrListHead ? PtrListHead : 0;
169 /// getForwardedTarget - Return the real alias set this represents. If this
170 /// has been merged with another set and is forwarding, return the ultimate
171 /// destination set. This also implements the union-find collapsing as well.
172 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
173 if (!Forward) return this;
175 AliasSet *Dest = Forward->getForwardedTarget(AST);
176 if (Dest != Forward) {
178 if (--Forward->RefCount == 0)
179 Forward->removeFromTracker(AST);
185 void removeFromTracker(AliasSetTracker &AST);
187 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
188 void addCallSite(CallSite CS);
190 /// aliasesPointer - Return true if the specified pointer "may" (or must)
191 /// alias one of the members in the set.
193 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
194 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
197 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
203 class AliasSetTracker {
205 ilist<AliasSet> AliasSets;
207 // Map from pointers to their node
208 hash_map<Value*, AliasSet::PointerRec> PointerMap;
210 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
211 /// the specified alias analysis object to disambiguate load and store
213 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
215 /// add methods - These methods are used to add different types of
216 /// instructions to the alias sets. Adding a new instruction can result in
217 /// one of three actions happening:
219 /// 1. If the instruction doesn't alias any other sets, create a new set.
220 /// 2. If the instruction aliases exactly one set, add it to the set
221 /// 3. If the instruction aliases multiple sets, merge the sets, and add
222 /// the instruction to the result.
224 void add(LoadInst *LI);
225 void add(StoreInst *SI);
226 void add(CallSite CS); // Call/Invoke instructions
227 void add(CallInst *CI) { add(CallSite(CI)); }
228 void add(InvokeInst *II) { add(CallSite(II)); }
229 void add(Instruction *I); // Dispatch to one of the other add methods...
230 void add(BasicBlock &BB); // Add all instructions in basic block
231 void add(const AliasSetTracker &AST); // Add alias relations from another AST
233 /// getAliasSets - Return the alias sets that are active.
234 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
236 /// getAliasSetForPointer - Return the alias set that the specified pointer
238 AliasSet &getAliasSetForPointer(Value *P, unsigned Size);
240 /// getAliasAnalysis - Return the underlying alias analysis object used by
242 AliasAnalysis &getAliasAnalysis() const { return AA; }
244 typedef ilist<AliasSet>::iterator iterator;
245 typedef ilist<AliasSet>::const_iterator const_iterator;
247 const_iterator begin() const { return AliasSets.begin(); }
248 const_iterator end() const { return AliasSets.end(); }
250 iterator begin() { return AliasSets.begin(); }
251 iterator end() { return AliasSets.end(); }
253 void print(std::ostream &OS) const;
257 friend class AliasSet;
258 void removeAliasSet(AliasSet *AS);
260 AliasSet::HashNodePair &getEntryFor(Value *V) {
261 // Standard operator[], except that it returns the whole pair, not just
263 return *PointerMap.insert(AliasSet::HashNodePair(V,
264 AliasSet::PointerRec())).first;
267 void addPointer(Value *P, unsigned Size, AliasSet::AccessType E) {
268 AliasSet &AS = getAliasSetForPointer(P, Size);
271 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
273 AliasSet *findAliasSetForCallSite(CallSite CS);
276 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {