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 *NextInList;
44 PointerRec() : NextInList(0), AS(0), Size(0) {}
46 HashNodePair *getNext() const { return NextInList; }
47 bool hasAliasSet() const { return AS != 0; }
49 void updateSize(unsigned NewSize) {
50 if (NewSize > Size) Size = NewSize;
53 unsigned getSize() const { return Size; }
55 AliasSet *getAliasSet(AliasSetTracker &AST) {
56 assert(AS && "No AliasSet yet!");
59 AS = OldAS->getForwardedTarget(AST);
60 if (--OldAS->RefCount == 0)
61 OldAS->removeFromTracker(AST);
67 void setAliasSet(AliasSet *as) {
68 assert(AS == 0 && "Already have an alias set!");
71 void setTail(HashNodePair *T) {
72 assert(NextInList == 0 && "Already have tail!");
77 HashNodePair *PtrListHead, *PtrListTail; // Singly linked list of nodes
78 AliasSet *Forward; // Forwarding pointer
79 AliasSet *Next, *Prev; // Doubly linked list of AliasSets
81 std::vector<CallSite> CallSites; // All calls & invokes in this node
83 // RefCount - Number of nodes pointing to this AliasSet plus the number of
84 // AliasSets forwarding to it.
85 unsigned RefCount : 28;
87 /// AccessType - Keep track of whether this alias set merely refers to the
88 /// locations of memory, whether it modifies the memory, or whether it does
89 /// both. The lattice goes from "NoModRef" to either Refs or Mods, then to
90 /// ModRef as necessary.
93 NoModRef = 0, Refs = 1, // Ref = bit 1
94 Mods = 2, ModRef = 3 // Mod = bit 2
96 unsigned AccessTy : 2;
98 /// AliasType - Keep track the relationships between the pointers in the set.
99 /// Lattice goes from MustAlias to MayAlias.
102 MustAlias = 0, MayAlias = 1
104 unsigned AliasTy : 1;
106 // Volatile - True if this alias set contains volatile loads or stores.
109 friend class ilist_traits<AliasSet>;
110 AliasSet *getPrev() const { return Prev; }
111 AliasSet *getNext() const { return Next; }
112 void setPrev(AliasSet *P) { Prev = P; }
113 void setNext(AliasSet *N) { Next = N; }
117 bool isRef() const { return AccessTy & Refs; }
118 bool isMod() const { return AccessTy & Mods; }
119 bool isMustAlias() const { return AliasTy == MustAlias; }
120 bool isMayAlias() const { return AliasTy == MayAlias; }
122 // isVolatile - Return true if this alias set contains volatile loads or
124 bool isVolatile() const { return Volatile; }
127 /// isForwardingAliasSet - Return true if this alias set should be ignored as
128 /// part of the AliasSetTracker object.
129 bool isForwardingAliasSet() const { return Forward; }
131 /// mergeSetIn - Merge the specified alias set into this alias set...
133 void mergeSetIn(AliasSet &AS);
135 // Alias Set iteration - Allow access to all of the pointer which are part of
138 iterator begin() const { return iterator(PtrListHead); }
139 iterator end() const { return iterator(); }
141 void print(std::ostream &OS) const;
144 /// Define an iterator for alias sets... this is just a forward iterator.
145 class iterator : public forward_iterator<HashNodePair, ptrdiff_t> {
146 HashNodePair *CurNode;
148 iterator(HashNodePair *CN = 0) : CurNode(CN) {}
150 bool operator==(const iterator& x) const {
151 return CurNode == x.CurNode;
153 bool operator!=(const iterator& x) const { return !operator==(x); }
155 const iterator &operator=(const iterator &I) {
160 value_type &operator*() const {
161 assert(CurNode && "Dereferencing AliasSet.end()!");
164 value_type *operator->() const { return &operator*(); }
166 iterator& operator++() { // Preincrement
167 assert(CurNode && "Advancing past AliasSet.end()!");
168 CurNode = CurNode->second.getNext();
171 iterator operator++(int) { // Postincrement
172 iterator tmp = *this; ++*this; return tmp;
177 // Can only be created by AliasSetTracker
178 AliasSet() : PtrListHead(0), PtrListTail(0), Forward(0), RefCount(0),
179 AccessTy(NoModRef), AliasTy(MustAlias), Volatile(false) {
181 HashNodePair *getSomePointer() const {
182 return PtrListHead ? PtrListHead : 0;
185 /// getForwardedTarget - Return the real alias set this represents. If this
186 /// has been merged with another set and is forwarding, return the ultimate
187 /// destination set. This also implements the union-find collapsing as well.
188 AliasSet *getForwardedTarget(AliasSetTracker &AST) {
189 if (!Forward) return this;
191 AliasSet *Dest = Forward->getForwardedTarget(AST);
192 if (Dest != Forward) {
194 if (--Forward->RefCount == 0)
195 Forward->removeFromTracker(AST);
201 void removeFromTracker(AliasSetTracker &AST);
203 void addPointer(AliasSetTracker &AST, HashNodePair &Entry, unsigned Size);
204 void addCallSite(CallSite CS);
205 void setVolatile() { Volatile = true; }
207 /// aliasesPointer - Return true if the specified pointer "may" (or must)
208 /// alias one of the members in the set.
210 bool aliasesPointer(const Value *Ptr, unsigned Size, AliasAnalysis &AA) const;
211 bool aliasesCallSite(CallSite CS, AliasAnalysis &AA) const;
214 inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) {
220 class AliasSetTracker {
222 ilist<AliasSet> AliasSets;
224 // Map from pointers to their node
225 hash_map<Value*, AliasSet::PointerRec> PointerMap;
227 /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use
228 /// the specified alias analysis object to disambiguate load and store
230 AliasSetTracker(AliasAnalysis &aa) : AA(aa) {}
232 /// add methods - These methods are used to add different types of
233 /// instructions to the alias sets. Adding a new instruction can result in
234 /// one of three actions happening:
236 /// 1. If the instruction doesn't alias any other sets, create a new set.
237 /// 2. If the instruction aliases exactly one set, add it to the set
238 /// 3. If the instruction aliases multiple sets, merge the sets, and add
239 /// the instruction to the result.
241 void add(LoadInst *LI);
242 void add(StoreInst *SI);
243 void add(CallSite CS); // Call/Invoke instructions
244 void add(CallInst *CI) { add(CallSite(CI)); }
245 void add(InvokeInst *II) { add(CallSite(II)); }
246 void add(Instruction *I); // Dispatch to one of the other add methods...
247 void add(BasicBlock &BB); // Add all instructions in basic block
248 void add(const AliasSetTracker &AST); // Add alias relations from another AST
250 /// getAliasSets - Return the alias sets that are active.
251 const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
253 /// getAliasSetForPointer - Return the alias set that the specified pointer
255 AliasSet &getAliasSetForPointer(Value *P, unsigned Size);
257 /// getAliasAnalysis - Return the underlying alias analysis object used by
259 AliasAnalysis &getAliasAnalysis() const { return AA; }
261 typedef ilist<AliasSet>::iterator iterator;
262 typedef ilist<AliasSet>::const_iterator const_iterator;
264 const_iterator begin() const { return AliasSets.begin(); }
265 const_iterator end() const { return AliasSets.end(); }
267 iterator begin() { return AliasSets.begin(); }
268 iterator end() { return AliasSets.end(); }
270 void print(std::ostream &OS) const;
274 friend class AliasSet;
275 void removeAliasSet(AliasSet *AS);
277 AliasSet::HashNodePair &getEntryFor(Value *V) {
278 // Standard operator[], except that it returns the whole pair, not just
280 return *PointerMap.insert(AliasSet::HashNodePair(V,
281 AliasSet::PointerRec())).first;
284 AliasSet &addPointer(Value *P, unsigned Size, AliasSet::AccessType E) {
285 AliasSet &AS = getAliasSetForPointer(P, Size);
289 AliasSet *findAliasSetForPointer(const Value *Ptr, unsigned Size);
291 AliasSet *findAliasSetForCallSite(CallSite CS);
294 inline std::ostream& operator<<(std::ostream &OS, const AliasSetTracker &AST) {
299 } // End llvm namespace