blob: fedc27907443fcfa1c2c0f0c1600e5224bbde041 [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"
16#include "llvm/iMemory.h"
17#include "llvm/iOther.h"
18#include "llvm/iTerminators.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000019#include "llvm/Pass.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000020#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000021#include "llvm/Assembly/Writer.h"
22#include "llvm/Support/InstIterator.h"
Reid Spencer954da372004-07-04 12:19:56 +000023#include <iostream>
Chris Lattnere2609242003-12-14 04:52:11 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner009cc3d2002-09-26 21:49:07 +000026/// mergeSetIn - Merge the specified alias set into this alias set...
27///
Chris Lattner9971ac42003-02-24 20:37:56 +000028void AliasSet::mergeSetIn(AliasSet &AS) {
29 assert(!AS.Forward && "Alias set is already forwarding!");
30 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000031
32 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000033 AccessTy |= AS.AccessTy;
34 AliasTy |= AS.AliasTy;
35
36 if (CallSites.empty()) { // Merge call sites...
37 if (!AS.CallSites.empty())
38 std::swap(CallSites, AS.CallSites);
39 } else if (!AS.CallSites.empty()) {
40 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
41 AS.CallSites.clear();
42 }
43
44 // FIXME: If AS's refcount is zero, nuke it now...
45 assert(RefCount != 0);
46
47 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000048 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000049
50 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000051 if (AS.PtrList) {
52 *PtrListEnd = AS.PtrList;
53 AS.PtrList->second.setPrevInList(PtrListEnd);
54 PtrListEnd = AS.PtrListEnd;
55
56 AS.PtrList = 0;
57 AS.PtrListEnd = &AS.PtrList;
58 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000059}
60
Chris Lattner9971ac42003-02-24 20:37:56 +000061void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000062 if (AliasSet *Fwd = AS->Forward) {
63 Fwd->dropRef(*this);
64 AS->Forward = 0;
65 }
Chris Lattner9971ac42003-02-24 20:37:56 +000066 AliasSets.erase(AS);
67}
68
69void AliasSet::removeFromTracker(AliasSetTracker &AST) {
70 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
71 AST.removeAliasSet(this);
72}
73
Chris Lattner31a9d182003-02-26 22:11:00 +000074void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
75 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000076 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
77
78 AliasAnalysis &AA = AST.getAliasAnalysis();
79
80 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000081 if (HashNodePair *P = getSomePointer()) {
82 AliasAnalysis::AliasResult Result =
83 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
84 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000085 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000086 else // First entry of must alias must have maximum size!
87 P->second.updateSize(Size);
88 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
89 }
Chris Lattner9971ac42003-02-24 20:37:56 +000090
91 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000092 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000093
94 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +000095 assert(*PtrListEnd == 0 && "End of list is not null?");
96 *PtrListEnd = &Entry;
97 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000098 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +000099}
100
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000101void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000102 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000103
104 if (Function *F = CS.getCalledFunction()) {
105 if (AA.doesNotAccessMemory(F))
106 return;
107 else if (AA.onlyReadsMemory(F)) {
108 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000109 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000110 return;
111 }
112 }
113
114 // FIXME: This should use mod/ref information to make this not suck so bad
115 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000116 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000117}
118
119/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000120/// alias one of the members in the set.
121///
Chris Lattner31a9d182003-02-26 22:11:00 +0000122bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
123 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000124 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000125 assert(CallSites.empty() && "Illegal must alias set!");
126
Chris Lattner9971ac42003-02-24 20:37:56 +0000127 // If this is a set of MustAliases, only check to see if the pointer aliases
128 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000129 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000130 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000131 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000132 }
133
134 // If this is a may-alias set, we have to check all of the pointers in the set
135 // to be sure it doesn't alias the set...
136 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000137 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000138 return true;
139
140 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000141 if (!CallSites.empty()) {
142 if (AA.hasNoModRefInfoForCalls())
143 return true;
144
145 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
146 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
147 != AliasAnalysis::NoModRef)
148 return true;
149 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000150
Chris Lattner009cc3d2002-09-26 21:49:07 +0000151 return false;
152}
153
Chris Lattner9971ac42003-02-24 20:37:56 +0000154bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000155 if (Function *F = CS.getCalledFunction())
156 if (AA.doesNotAccessMemory(F))
157 return false;
158
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000159 if (AA.hasNoModRefInfoForCalls())
160 return true;
161
162 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
163 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
164 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
165 return true;
166
167 for (iterator I = begin(), E = end(); I != E; ++I)
168 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
169 AliasAnalysis::NoModRef)
170 return true;
171
172 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000173}
174
175
Chris Lattner009cc3d2002-09-26 21:49:07 +0000176/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
177/// instruction referring to the pointer into. If there are multiple alias sets
178/// that may alias the pointer, merge them together and return the unified set.
179///
Chris Lattner31a9d182003-02-26 22:11:00 +0000180AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
181 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000182 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000183 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000184 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000185 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000186 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000187 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000188 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000189 }
190 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000191
192 return FoundSet;
193}
194
195AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
196 AliasSet *FoundSet = 0;
197 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000198 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000199 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
200 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000201 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000202 FoundSet->mergeSetIn(*I); // Merge in contents...
203 }
204 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000205
206 return FoundSet;
207}
208
209
Chris Lattner009cc3d2002-09-26 21:49:07 +0000210
Chris Lattner9971ac42003-02-24 20:37:56 +0000211
212/// getAliasSetForPointer - Return the alias set that the specified pointer
213/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000214AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
215 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000216 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
217
218 // Check to see if the pointer is already known...
Chris Lattner34a10052004-07-25 18:32:01 +0000219 if (Entry.second.hasAliasSet()) {
220 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000221 // Return the set!
222 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000223 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000224 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000225 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000226 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000227 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000228 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000229 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000230 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000231 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000232 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000233 }
234}
235
Chris Lattner61d46272004-07-26 05:50:23 +0000236bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
237 bool NewPtr;
238 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
239 return NewPtr;
240}
241
242
Chris Lattner12c11552004-07-21 05:18:04 +0000243bool AliasSetTracker::add(LoadInst *LI) {
244 bool NewPtr;
245 AliasSet &AS = addPointer(LI->getOperand(0),
246 AA.getTargetData().getTypeSize(LI->getType()),
247 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000248 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000249 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000250}
251
Chris Lattner12c11552004-07-21 05:18:04 +0000252bool AliasSetTracker::add(StoreInst *SI) {
253 bool NewPtr;
254 Value *Val = SI->getOperand(0);
255 AliasSet &AS = addPointer(SI->getOperand(1),
256 AA.getTargetData().getTypeSize(Val->getType()),
257 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000258 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000259 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000260}
261
Chris Lattner5c882602004-07-25 07:57:37 +0000262bool AliasSetTracker::add(FreeInst *FI) {
263 bool NewPtr;
264 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
265 AliasSet::Mods, NewPtr);
266 return NewPtr;
267}
268
Chris Lattnere2609242003-12-14 04:52:11 +0000269
Chris Lattner12c11552004-07-21 05:18:04 +0000270bool AliasSetTracker::add(CallSite CS) {
271 bool NewPtr;
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000272 if (Function *F = CS.getCalledFunction())
273 if (AA.doesNotAccessMemory(F))
Chris Lattner12c11552004-07-21 05:18:04 +0000274 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000275
Chris Lattner9971ac42003-02-24 20:37:56 +0000276 AliasSet *AS = findAliasSetForCallSite(CS);
277 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000278 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000279 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000280 AS->addCallSite(CS, AA);
281 return true;
282 } else {
283 AS->addCallSite(CS, AA);
284 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000285 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000286}
287
Chris Lattner12c11552004-07-21 05:18:04 +0000288bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000289 // Dispatch to one of the other add methods...
290 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000291 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000292 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000293 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000294 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000295 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000296 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000297 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000298 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
299 return add(FI);
Chris Lattner12c11552004-07-21 05:18:04 +0000300 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000301}
302
Chris Lattner319d05b2003-03-03 23:28:05 +0000303void AliasSetTracker::add(BasicBlock &BB) {
304 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
305 add(I);
306}
307
308void AliasSetTracker::add(const AliasSetTracker &AST) {
309 assert(&AA == &AST.AA &&
310 "Merging AliasSetTracker objects with different Alias Analyses!");
311
312 // Loop over all of the alias sets in AST, adding the pointers contained
313 // therein into the current alias sets. This can cause alias sets to be
314 // merged together in the current AST.
315 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
316 if (!I->Forward) { // Ignore forwarding alias sets
317 AliasSet &AS = const_cast<AliasSet&>(*I);
318
319 // If there are any call sites in the alias set, add them to this AST.
320 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
321 add(AS.CallSites[i]);
322
323 // Loop over all of the pointers in this alias set...
324 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000325 bool X;
Chris Lattner319d05b2003-03-03 23:28:05 +0000326 for (; I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000327 addPointer(I.getPointer(), I.getSize(),
Chris Lattner12c11552004-07-21 05:18:04 +0000328 (AliasSet::AccessType)AS.AccessTy, X);
Chris Lattner319d05b2003-03-03 23:28:05 +0000329 }
330}
331
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000332/// remove - Remove the specified (potentially non-empty) alias set from the
333/// tracker.
334void AliasSetTracker::remove(AliasSet &AS) {
335 bool SetDead;
336 do {
337 AliasSet::iterator I = AS.begin();
338 Value *Ptr = I.getPointer(); ++I;
339
340 // deleteValue will delete the set automatically when the last pointer
341 // reference is destroyed. "Predict" when this will happen.
342 SetDead = I == AS.end();
343 deleteValue(Ptr); // Delete all of the pointers from the set
344 } while (!SetDead);
345}
346
Chris Lattner61d46272004-07-26 05:50:23 +0000347bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
348 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
349 if (!AS) return false;
350 remove(*AS);
351 return true;
352}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000353
354bool AliasSetTracker::remove(LoadInst *LI) {
355 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
356 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
357 if (!AS) return false;
358 remove(*AS);
359 return true;
360}
361
362bool AliasSetTracker::remove(StoreInst *SI) {
363 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
364 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
365 if (!AS) return false;
366 remove(*AS);
367 return true;
368}
369
Chris Lattner5c882602004-07-25 07:57:37 +0000370bool AliasSetTracker::remove(FreeInst *FI) {
371 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
372 if (!AS) return false;
373 remove(*AS);
374 return true;
375}
376
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000377bool AliasSetTracker::remove(CallSite CS) {
378 if (Function *F = CS.getCalledFunction())
379 if (AA.doesNotAccessMemory(F))
380 return false; // doesn't alias anything
381
382 AliasSet *AS = findAliasSetForCallSite(CS);
383 if (!AS) return false;
384 remove(*AS);
385 return true;
386}
387
388bool AliasSetTracker::remove(Instruction *I) {
389 // Dispatch to one of the other remove methods...
390 if (LoadInst *LI = dyn_cast<LoadInst>(I))
391 return remove(LI);
392 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
393 return remove(SI);
394 else if (CallInst *CI = dyn_cast<CallInst>(I))
395 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000396 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
397 return remove(FI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000398 return true;
399}
400
Chris Lattner2cffeec2003-12-18 08:11:56 +0000401
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000402// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000403// AliasSetTracker entirely. It should be used when an instruction is deleted
404// from the program to update the AST. If you don't use this, you would have
405// dangling pointers to deleted instructions.
406//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000407void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattner2cffeec2003-12-18 08:11:56 +0000408 // First, look up the PointerRec for this pointer...
409 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
410 if (I == PointerMap.end()) return; // Noop
411
412 // If we found one, remove the pointer from the alias set it is in.
413 AliasSet::HashNodePair &PtrValEnt = *I;
414 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
415
416 // Unlink from the list of values...
417 PtrValEnt.second.removeFromList();
418 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000419 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000420 PointerMap.erase(I);
421}
422
423
Chris Lattner9971ac42003-02-24 20:37:56 +0000424//===----------------------------------------------------------------------===//
425// AliasSet/AliasSetTracker Printing Support
426//===----------------------------------------------------------------------===//
427
428void AliasSet::print(std::ostream &OS) const {
429 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
430 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
431 switch (AccessTy) {
432 case NoModRef: OS << "No access "; break;
433 case Refs : OS << "Ref "; break;
434 case Mods : OS << "Mod "; break;
435 case ModRef : OS << "Mod/Ref "; break;
436 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000437 }
Chris Lattnere2609242003-12-14 04:52:11 +0000438 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000439 if (Forward)
440 OS << " forwarding to " << (void*)Forward;
441
442
443 if (begin() != end()) {
444 OS << "Pointers: ";
445 for (iterator I = begin(), E = end(); I != E; ++I) {
446 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000447 WriteAsOperand(OS << "(", I.getPointer());
448 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000449 }
450 }
451 if (!CallSites.empty()) {
452 OS << "\n " << CallSites.size() << " Call Sites: ";
453 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
454 if (i) OS << ", ";
455 WriteAsOperand(OS, CallSites[i].getCalledValue());
456 }
457 }
458 OS << "\n";
459}
460
461void AliasSetTracker::print(std::ostream &OS) const {
462 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
463 << PointerMap.size() << " pointer values.\n";
464 for (const_iterator I = begin(), E = end(); I != E; ++I)
465 I->print(OS);
466 OS << "\n";
467}
468
469void AliasSet::dump() const { print (std::cerr); }
470void AliasSetTracker::dump() const { print(std::cerr); }
471
Chris Lattner9971ac42003-02-24 20:37:56 +0000472//===----------------------------------------------------------------------===//
473// AliasSetPrinter Pass
474//===----------------------------------------------------------------------===//
475
476namespace {
477 class AliasSetPrinter : public FunctionPass {
478 AliasSetTracker *Tracker;
479 public:
480 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
481 AU.setPreservesAll();
482 AU.addRequired<AliasAnalysis>();
483 }
484
485 virtual bool runOnFunction(Function &F) {
486 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
487
488 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000489 Tracker->add(&*I);
Chris Lattner9971ac42003-02-24 20:37:56 +0000490 return false;
491 }
492
493 /// print - Convert to human readable form
494 virtual void print(std::ostream &OS) const {
495 Tracker->print(OS);
496 }
497
498 virtual void releaseMemory() {
499 delete Tracker;
500 }
501 };
502 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
503 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000504}