blob: e5f7f32f9c7f2ed94aa65e03cb4459a96d50a328 [file] [log] [blame]
Chris Lattner009cc3d2002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner009cc3d2002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/AliasSetTracker.h"
15#include "llvm/Analysis/AliasAnalysis.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000016#include "llvm/Instructions.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000017#include "llvm/Pass.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000018#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/Support/InstIterator.h"
Reid Spencer954da372004-07-04 12:19:56 +000021#include <iostream>
Chris Lattnere2609242003-12-14 04:52:11 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner009cc3d2002-09-26 21:49:07 +000024/// mergeSetIn - Merge the specified alias set into this alias set...
25///
Chris Lattner9971ac42003-02-24 20:37:56 +000026void AliasSet::mergeSetIn(AliasSet &AS) {
27 assert(!AS.Forward && "Alias set is already forwarding!");
28 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000029
30 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000031 AccessTy |= AS.AccessTy;
32 AliasTy |= AS.AliasTy;
33
34 if (CallSites.empty()) { // Merge call sites...
35 if (!AS.CallSites.empty())
36 std::swap(CallSites, AS.CallSites);
37 } else if (!AS.CallSites.empty()) {
38 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
39 AS.CallSites.clear();
40 }
41
Chris Lattner9971ac42003-02-24 20:37:56 +000042 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000043 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000044
45 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000046 if (AS.PtrList) {
47 *PtrListEnd = AS.PtrList;
48 AS.PtrList->second.setPrevInList(PtrListEnd);
49 PtrListEnd = AS.PtrListEnd;
50
51 AS.PtrList = 0;
52 AS.PtrListEnd = &AS.PtrList;
53 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000054}
55
Chris Lattner9971ac42003-02-24 20:37:56 +000056void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000057 if (AliasSet *Fwd = AS->Forward) {
58 Fwd->dropRef(*this);
59 AS->Forward = 0;
60 }
Chris Lattner9971ac42003-02-24 20:37:56 +000061 AliasSets.erase(AS);
62}
63
64void AliasSet::removeFromTracker(AliasSetTracker &AST) {
65 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
66 AST.removeAliasSet(this);
67}
68
Chris Lattner31a9d182003-02-26 22:11:00 +000069void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
70 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000071 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
72
73 AliasAnalysis &AA = AST.getAliasAnalysis();
74
75 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000076 if (HashNodePair *P = getSomePointer()) {
77 AliasAnalysis::AliasResult Result =
78 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
79 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000080 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000081 else // First entry of must alias must have maximum size!
82 P->second.updateSize(Size);
83 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
84 }
Chris Lattner9971ac42003-02-24 20:37:56 +000085
86 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000087 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000088
89 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +000090 assert(*PtrListEnd == 0 && "End of list is not null?");
91 *PtrListEnd = &Entry;
92 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000093 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +000094}
95
Chris Lattner5b5f7c12004-03-15 04:08:36 +000096void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +000097 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +000098
99 if (Function *F = CS.getCalledFunction()) {
100 if (AA.doesNotAccessMemory(F))
101 return;
102 else if (AA.onlyReadsMemory(F)) {
103 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000104 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000105 return;
106 }
107 }
108
109 // FIXME: This should use mod/ref information to make this not suck so bad
110 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000111 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000112}
113
114/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000115/// alias one of the members in the set.
116///
Chris Lattner31a9d182003-02-26 22:11:00 +0000117bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
118 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000119 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000120 assert(CallSites.empty() && "Illegal must alias set!");
121
Chris Lattner9971ac42003-02-24 20:37:56 +0000122 // If this is a set of MustAliases, only check to see if the pointer aliases
123 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000124 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000125 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000126 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000127 }
128
129 // If this is a may-alias set, we have to check all of the pointers in the set
130 // to be sure it doesn't alias the set...
131 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000132 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000133 return true;
134
135 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000136 if (!CallSites.empty()) {
137 if (AA.hasNoModRefInfoForCalls())
138 return true;
139
140 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
141 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
142 != AliasAnalysis::NoModRef)
143 return true;
144 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000145
Chris Lattner009cc3d2002-09-26 21:49:07 +0000146 return false;
147}
148
Chris Lattner9971ac42003-02-24 20:37:56 +0000149bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000150 if (Function *F = CS.getCalledFunction())
151 if (AA.doesNotAccessMemory(F))
152 return false;
153
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000154 if (AA.hasNoModRefInfoForCalls())
155 return true;
156
157 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
158 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
159 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
160 return true;
161
162 for (iterator I = begin(), E = end(); I != E; ++I)
163 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
164 AliasAnalysis::NoModRef)
165 return true;
166
167 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000168}
169
170
Chris Lattner009cc3d2002-09-26 21:49:07 +0000171/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
172/// instruction referring to the pointer into. If there are multiple alias sets
173/// that may alias the pointer, merge them together and return the unified set.
174///
Chris Lattner31a9d182003-02-26 22:11:00 +0000175AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
176 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000177 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000178 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000179 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000180 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000181 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000182 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000183 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000184 }
185 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000186
187 return FoundSet;
188}
189
190AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
191 AliasSet *FoundSet = 0;
192 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000193 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000194 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
195 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000196 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000197 FoundSet->mergeSetIn(*I); // Merge in contents...
198 }
199 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000200
201 return FoundSet;
202}
203
204
Chris Lattner009cc3d2002-09-26 21:49:07 +0000205
Chris Lattner9971ac42003-02-24 20:37:56 +0000206
207/// getAliasSetForPointer - Return the alias set that the specified pointer
208/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000209AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
210 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000211 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
212
213 // Check to see if the pointer is already known...
Chris Lattner34a10052004-07-25 18:32:01 +0000214 if (Entry.second.hasAliasSet()) {
215 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000216 // Return the set!
217 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000218 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000219 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000220 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000221 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000222 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000223 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000224 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000225 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000226 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000227 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000228 }
229}
230
Chris Lattner61d46272004-07-26 05:50:23 +0000231bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
232 bool NewPtr;
233 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
234 return NewPtr;
235}
236
237
Chris Lattner12c11552004-07-21 05:18:04 +0000238bool AliasSetTracker::add(LoadInst *LI) {
239 bool NewPtr;
240 AliasSet &AS = addPointer(LI->getOperand(0),
241 AA.getTargetData().getTypeSize(LI->getType()),
242 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000243 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000244 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000245}
246
Chris Lattner12c11552004-07-21 05:18:04 +0000247bool AliasSetTracker::add(StoreInst *SI) {
248 bool NewPtr;
249 Value *Val = SI->getOperand(0);
250 AliasSet &AS = addPointer(SI->getOperand(1),
251 AA.getTargetData().getTypeSize(Val->getType()),
252 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000253 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000254 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000255}
256
Chris Lattner5c882602004-07-25 07:57:37 +0000257bool AliasSetTracker::add(FreeInst *FI) {
258 bool NewPtr;
259 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
260 AliasSet::Mods, NewPtr);
261 return NewPtr;
262}
263
Chris Lattnere2609242003-12-14 04:52:11 +0000264
Chris Lattner12c11552004-07-21 05:18:04 +0000265bool AliasSetTracker::add(CallSite CS) {
266 bool NewPtr;
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000267 if (Function *F = CS.getCalledFunction())
268 if (AA.doesNotAccessMemory(F))
Chris Lattner12c11552004-07-21 05:18:04 +0000269 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000270
Chris Lattner9971ac42003-02-24 20:37:56 +0000271 AliasSet *AS = findAliasSetForCallSite(CS);
272 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000273 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000274 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000275 AS->addCallSite(CS, AA);
276 return true;
277 } else {
278 AS->addCallSite(CS, AA);
279 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000280 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000281}
282
Chris Lattner12c11552004-07-21 05:18:04 +0000283bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000284 // Dispatch to one of the other add methods...
285 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000286 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000287 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000288 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000289 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000290 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000291 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000292 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000293 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
294 return add(FI);
Chris Lattner12c11552004-07-21 05:18:04 +0000295 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000296}
297
Chris Lattner319d05b2003-03-03 23:28:05 +0000298void AliasSetTracker::add(BasicBlock &BB) {
299 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
300 add(I);
301}
302
303void AliasSetTracker::add(const AliasSetTracker &AST) {
304 assert(&AA == &AST.AA &&
305 "Merging AliasSetTracker objects with different Alias Analyses!");
306
307 // Loop over all of the alias sets in AST, adding the pointers contained
308 // therein into the current alias sets. This can cause alias sets to be
309 // merged together in the current AST.
310 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
311 if (!I->Forward) { // Ignore forwarding alias sets
312 AliasSet &AS = const_cast<AliasSet&>(*I);
313
314 // If there are any call sites in the alias set, add them to this AST.
315 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
316 add(AS.CallSites[i]);
317
318 // Loop over all of the pointers in this alias set...
319 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000320 bool X;
Chris Lattner319d05b2003-03-03 23:28:05 +0000321 for (; I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000322 addPointer(I.getPointer(), I.getSize(),
Chris Lattner12c11552004-07-21 05:18:04 +0000323 (AliasSet::AccessType)AS.AccessTy, X);
Chris Lattner319d05b2003-03-03 23:28:05 +0000324 }
325}
326
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000327/// remove - Remove the specified (potentially non-empty) alias set from the
328/// tracker.
329void AliasSetTracker::remove(AliasSet &AS) {
330 bool SetDead;
331 do {
332 AliasSet::iterator I = AS.begin();
333 Value *Ptr = I.getPointer(); ++I;
334
335 // deleteValue will delete the set automatically when the last pointer
336 // reference is destroyed. "Predict" when this will happen.
337 SetDead = I == AS.end();
338 deleteValue(Ptr); // Delete all of the pointers from the set
339 } while (!SetDead);
340}
341
Chris Lattner61d46272004-07-26 05:50:23 +0000342bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
343 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
344 if (!AS) return false;
345 remove(*AS);
346 return true;
347}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000348
349bool AliasSetTracker::remove(LoadInst *LI) {
350 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
351 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
352 if (!AS) return false;
353 remove(*AS);
354 return true;
355}
356
357bool AliasSetTracker::remove(StoreInst *SI) {
358 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
359 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
360 if (!AS) return false;
361 remove(*AS);
362 return true;
363}
364
Chris Lattner5c882602004-07-25 07:57:37 +0000365bool AliasSetTracker::remove(FreeInst *FI) {
366 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
367 if (!AS) return false;
368 remove(*AS);
369 return true;
370}
371
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000372bool AliasSetTracker::remove(CallSite CS) {
373 if (Function *F = CS.getCalledFunction())
374 if (AA.doesNotAccessMemory(F))
375 return false; // doesn't alias anything
376
377 AliasSet *AS = findAliasSetForCallSite(CS);
378 if (!AS) return false;
379 remove(*AS);
380 return true;
381}
382
383bool AliasSetTracker::remove(Instruction *I) {
384 // Dispatch to one of the other remove methods...
385 if (LoadInst *LI = dyn_cast<LoadInst>(I))
386 return remove(LI);
387 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
388 return remove(SI);
389 else if (CallInst *CI = dyn_cast<CallInst>(I))
390 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000391 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
392 return remove(FI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000393 return true;
394}
395
Chris Lattner2cffeec2003-12-18 08:11:56 +0000396
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000397// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000398// AliasSetTracker entirely. It should be used when an instruction is deleted
399// from the program to update the AST. If you don't use this, you would have
400// dangling pointers to deleted instructions.
401//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000402void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattner2cffeec2003-12-18 08:11:56 +0000403 // First, look up the PointerRec for this pointer...
404 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
405 if (I == PointerMap.end()) return; // Noop
406
407 // If we found one, remove the pointer from the alias set it is in.
408 AliasSet::HashNodePair &PtrValEnt = *I;
409 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
410
411 // Unlink from the list of values...
412 PtrValEnt.second.removeFromList();
413 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000414 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000415 PointerMap.erase(I);
416}
417
418
Chris Lattner9971ac42003-02-24 20:37:56 +0000419//===----------------------------------------------------------------------===//
420// AliasSet/AliasSetTracker Printing Support
421//===----------------------------------------------------------------------===//
422
423void AliasSet::print(std::ostream &OS) const {
424 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
425 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
426 switch (AccessTy) {
427 case NoModRef: OS << "No access "; break;
428 case Refs : OS << "Ref "; break;
429 case Mods : OS << "Mod "; break;
430 case ModRef : OS << "Mod/Ref "; break;
431 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000432 }
Chris Lattnere2609242003-12-14 04:52:11 +0000433 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000434 if (Forward)
435 OS << " forwarding to " << (void*)Forward;
436
437
438 if (begin() != end()) {
439 OS << "Pointers: ";
440 for (iterator I = begin(), E = end(); I != E; ++I) {
441 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000442 WriteAsOperand(OS << "(", I.getPointer());
443 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000444 }
445 }
446 if (!CallSites.empty()) {
447 OS << "\n " << CallSites.size() << " Call Sites: ";
448 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
449 if (i) OS << ", ";
450 WriteAsOperand(OS, CallSites[i].getCalledValue());
451 }
452 }
453 OS << "\n";
454}
455
456void AliasSetTracker::print(std::ostream &OS) const {
457 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
458 << PointerMap.size() << " pointer values.\n";
459 for (const_iterator I = begin(), E = end(); I != E; ++I)
460 I->print(OS);
461 OS << "\n";
462}
463
464void AliasSet::dump() const { print (std::cerr); }
465void AliasSetTracker::dump() const { print(std::cerr); }
466
Chris Lattner9971ac42003-02-24 20:37:56 +0000467//===----------------------------------------------------------------------===//
468// AliasSetPrinter Pass
469//===----------------------------------------------------------------------===//
470
471namespace {
472 class AliasSetPrinter : public FunctionPass {
473 AliasSetTracker *Tracker;
474 public:
475 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
476 AU.setPreservesAll();
477 AU.addRequired<AliasAnalysis>();
478 }
479
480 virtual bool runOnFunction(Function &F) {
481 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
482
483 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000484 Tracker->add(&*I);
Chris Lattner9971ac42003-02-24 20:37:56 +0000485 return false;
486 }
487
488 /// print - Convert to human readable form
489 virtual void print(std::ostream &OS) const {
490 Tracker->print(OS);
491 }
492
493 virtual void releaseMemory() {
494 delete Tracker;
495 }
496 };
497 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
498 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000499}