blob: 15b4945c59241f2caaa4f37a361413800dcf58cc [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,
Chris Lattnerb66e6482004-09-14 19:15:32 +000070 unsigned Size, bool KnownMustAlias) {
Chris Lattner9971ac42003-02-24 20:37:56 +000071 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
72
73 AliasAnalysis &AA = AST.getAliasAnalysis();
74
Chris Lattnerb66e6482004-09-14 19:15:32 +000075 // Check to see if we have to downgrade to _may_ alias.
76 if (isMustAlias() && !KnownMustAlias)
Chris Lattner31a9d182003-02-26 22:11:00 +000077 if (HashNodePair *P = getSomePointer()) {
78 AliasAnalysis::AliasResult Result =
79 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
80 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000081 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000082 else // First entry of must alias must have maximum size!
83 P->second.updateSize(Size);
84 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
85 }
Chris Lattner9971ac42003-02-24 20:37:56 +000086
87 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000088 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000089
90 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +000091 assert(*PtrListEnd == 0 && "End of list is not null?");
92 *PtrListEnd = &Entry;
93 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000094 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +000095}
96
Chris Lattner5b5f7c12004-03-15 04:08:36 +000097void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +000098 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +000099
100 if (Function *F = CS.getCalledFunction()) {
101 if (AA.doesNotAccessMemory(F))
102 return;
103 else if (AA.onlyReadsMemory(F)) {
104 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000105 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000106 return;
107 }
108 }
109
110 // FIXME: This should use mod/ref information to make this not suck so bad
111 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000112 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000113}
114
115/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000116/// alias one of the members in the set.
117///
Chris Lattner31a9d182003-02-26 22:11:00 +0000118bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
119 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000120 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000121 assert(CallSites.empty() && "Illegal must alias set!");
122
Chris Lattner9971ac42003-02-24 20:37:56 +0000123 // If this is a set of MustAliases, only check to see if the pointer aliases
124 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000125 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000126 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000127 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000128 }
129
130 // If this is a may-alias set, we have to check all of the pointers in the set
131 // to be sure it doesn't alias the set...
132 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000133 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000134 return true;
135
136 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000137 if (!CallSites.empty()) {
138 if (AA.hasNoModRefInfoForCalls())
139 return true;
140
141 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
142 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
143 != AliasAnalysis::NoModRef)
144 return true;
145 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000146
Chris Lattner009cc3d2002-09-26 21:49:07 +0000147 return false;
148}
149
Chris Lattner9971ac42003-02-24 20:37:56 +0000150bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000151 if (Function *F = CS.getCalledFunction())
152 if (AA.doesNotAccessMemory(F))
153 return false;
154
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000155 if (AA.hasNoModRefInfoForCalls())
156 return true;
157
158 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
159 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
160 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
161 return true;
162
163 for (iterator I = begin(), E = end(); I != E; ++I)
164 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
165 AliasAnalysis::NoModRef)
166 return true;
167
168 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000169}
170
171
Chris Lattner009cc3d2002-09-26 21:49:07 +0000172/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
173/// instruction referring to the pointer into. If there are multiple alias sets
174/// that may alias the pointer, merge them together and return the unified set.
175///
Chris Lattner31a9d182003-02-26 22:11:00 +0000176AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
177 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000178 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000179 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000180 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000181 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000182 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000183 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000184 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000185 }
186 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000187
188 return FoundSet;
189}
190
191AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
192 AliasSet *FoundSet = 0;
193 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000194 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000195 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
196 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000197 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000198 FoundSet->mergeSetIn(*I); // Merge in contents...
199 }
200 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000201
202 return FoundSet;
203}
204
205
Chris Lattner009cc3d2002-09-26 21:49:07 +0000206
Chris Lattner9971ac42003-02-24 20:37:56 +0000207
208/// getAliasSetForPointer - Return the alias set that the specified pointer
209/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000210AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
211 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000212 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
213
214 // Check to see if the pointer is already known...
Chris Lattner34a10052004-07-25 18:32:01 +0000215 if (Entry.second.hasAliasSet()) {
216 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000217 // Return the set!
218 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000219 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000220 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000221 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000222 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000223 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000224 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000225 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000226 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000227 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000228 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000229 }
230}
231
Chris Lattner61d46272004-07-26 05:50:23 +0000232bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
233 bool NewPtr;
234 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
235 return NewPtr;
236}
237
238
Chris Lattner12c11552004-07-21 05:18:04 +0000239bool AliasSetTracker::add(LoadInst *LI) {
240 bool NewPtr;
241 AliasSet &AS = addPointer(LI->getOperand(0),
242 AA.getTargetData().getTypeSize(LI->getType()),
243 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000244 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000245 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000246}
247
Chris Lattner12c11552004-07-21 05:18:04 +0000248bool AliasSetTracker::add(StoreInst *SI) {
249 bool NewPtr;
250 Value *Val = SI->getOperand(0);
251 AliasSet &AS = addPointer(SI->getOperand(1),
252 AA.getTargetData().getTypeSize(Val->getType()),
253 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000254 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000255 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000256}
257
Chris Lattner5c882602004-07-25 07:57:37 +0000258bool AliasSetTracker::add(FreeInst *FI) {
259 bool NewPtr;
260 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
261 AliasSet::Mods, NewPtr);
262 return NewPtr;
263}
264
Chris Lattnere2609242003-12-14 04:52:11 +0000265
Chris Lattner12c11552004-07-21 05:18:04 +0000266bool AliasSetTracker::add(CallSite CS) {
267 bool NewPtr;
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000268 if (Function *F = CS.getCalledFunction())
269 if (AA.doesNotAccessMemory(F))
Chris Lattner12c11552004-07-21 05:18:04 +0000270 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000271
Chris Lattner9971ac42003-02-24 20:37:56 +0000272 AliasSet *AS = findAliasSetForCallSite(CS);
273 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000274 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000275 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000276 AS->addCallSite(CS, AA);
277 return true;
278 } else {
279 AS->addCallSite(CS, AA);
280 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000281 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000282}
283
Chris Lattner12c11552004-07-21 05:18:04 +0000284bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000285 // Dispatch to one of the other add methods...
286 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000287 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000288 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000289 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000290 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000291 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000292 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000293 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000294 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
295 return add(FI);
Chris Lattner12c11552004-07-21 05:18:04 +0000296 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000297}
298
Chris Lattner319d05b2003-03-03 23:28:05 +0000299void AliasSetTracker::add(BasicBlock &BB) {
300 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
301 add(I);
302}
303
304void AliasSetTracker::add(const AliasSetTracker &AST) {
305 assert(&AA == &AST.AA &&
306 "Merging AliasSetTracker objects with different Alias Analyses!");
307
308 // Loop over all of the alias sets in AST, adding the pointers contained
309 // therein into the current alias sets. This can cause alias sets to be
310 // merged together in the current AST.
311 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
312 if (!I->Forward) { // Ignore forwarding alias sets
313 AliasSet &AS = const_cast<AliasSet&>(*I);
314
315 // If there are any call sites in the alias set, add them to this AST.
316 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
317 add(AS.CallSites[i]);
318
319 // Loop over all of the pointers in this alias set...
320 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000321 bool X;
Chris Lattner319d05b2003-03-03 23:28:05 +0000322 for (; I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000323 addPointer(I.getPointer(), I.getSize(),
Chris Lattner12c11552004-07-21 05:18:04 +0000324 (AliasSet::AccessType)AS.AccessTy, X);
Chris Lattner319d05b2003-03-03 23:28:05 +0000325 }
326}
327
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000328/// remove - Remove the specified (potentially non-empty) alias set from the
329/// tracker.
330void AliasSetTracker::remove(AliasSet &AS) {
331 bool SetDead;
332 do {
333 AliasSet::iterator I = AS.begin();
334 Value *Ptr = I.getPointer(); ++I;
335
336 // deleteValue will delete the set automatically when the last pointer
337 // reference is destroyed. "Predict" when this will happen.
338 SetDead = I == AS.end();
339 deleteValue(Ptr); // Delete all of the pointers from the set
340 } while (!SetDead);
341}
342
Chris Lattner61d46272004-07-26 05:50:23 +0000343bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
344 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
345 if (!AS) return false;
346 remove(*AS);
347 return true;
348}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000349
350bool AliasSetTracker::remove(LoadInst *LI) {
351 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
352 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
353 if (!AS) return false;
354 remove(*AS);
355 return true;
356}
357
358bool AliasSetTracker::remove(StoreInst *SI) {
359 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
360 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
361 if (!AS) return false;
362 remove(*AS);
363 return true;
364}
365
Chris Lattner5c882602004-07-25 07:57:37 +0000366bool AliasSetTracker::remove(FreeInst *FI) {
367 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
368 if (!AS) return false;
369 remove(*AS);
370 return true;
371}
372
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000373bool AliasSetTracker::remove(CallSite CS) {
374 if (Function *F = CS.getCalledFunction())
375 if (AA.doesNotAccessMemory(F))
376 return false; // doesn't alias anything
377
378 AliasSet *AS = findAliasSetForCallSite(CS);
379 if (!AS) return false;
380 remove(*AS);
381 return true;
382}
383
384bool AliasSetTracker::remove(Instruction *I) {
385 // Dispatch to one of the other remove methods...
386 if (LoadInst *LI = dyn_cast<LoadInst>(I))
387 return remove(LI);
388 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
389 return remove(SI);
390 else if (CallInst *CI = dyn_cast<CallInst>(I))
391 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000392 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
393 return remove(FI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000394 return true;
395}
396
Chris Lattner2cffeec2003-12-18 08:11:56 +0000397
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000398// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000399// AliasSetTracker entirely. It should be used when an instruction is deleted
400// from the program to update the AST. If you don't use this, you would have
401// dangling pointers to deleted instructions.
402//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000403void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000404 // Notify the alias analysis implementation that this value is gone.
405 AA.deleteValue(PtrVal);
406
407 // First, look up the PointerRec for this pointer.
Chris Lattner2cffeec2003-12-18 08:11:56 +0000408 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
409 if (I == PointerMap.end()) return; // Noop
410
411 // If we found one, remove the pointer from the alias set it is in.
412 AliasSet::HashNodePair &PtrValEnt = *I;
413 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
414
415 // Unlink from the list of values...
416 PtrValEnt.second.removeFromList();
417 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000418 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000419 PointerMap.erase(I);
420}
421
Chris Lattnerb66e6482004-09-14 19:15:32 +0000422// copyValue - This method should be used whenever a preexisting value in the
423// program is copied or cloned, introducing a new value. Note that it is ok for
424// clients that use this method to introduce the same value multiple times: if
425// the tracker already knows about a value, it will ignore the request.
426//
427void AliasSetTracker::copyValue(Value *From, Value *To) {
428 // Notify the alias analysis implementation that this value is copied.
429 AA.copyValue(From, To);
430
431 // First, look up the PointerRec for this pointer.
432 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
433 if (I == PointerMap.end() || !I->second.hasAliasSet())
434 return; // Noop
435
436 AliasSet::HashNodePair &Entry = getEntryFor(To);
437 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
438
439 // Add it to the alias set it aliases...
440 AliasSet *AS = I->second.getAliasSet(*this);
441 AS->addPointer(*this, Entry, I->second.getSize(), true);
442}
443
444
Chris Lattner2cffeec2003-12-18 08:11:56 +0000445
Chris Lattner9971ac42003-02-24 20:37:56 +0000446//===----------------------------------------------------------------------===//
447// AliasSet/AliasSetTracker Printing Support
448//===----------------------------------------------------------------------===//
449
450void AliasSet::print(std::ostream &OS) const {
451 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
452 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
453 switch (AccessTy) {
454 case NoModRef: OS << "No access "; break;
455 case Refs : OS << "Ref "; break;
456 case Mods : OS << "Mod "; break;
457 case ModRef : OS << "Mod/Ref "; break;
458 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000459 }
Chris Lattnere2609242003-12-14 04:52:11 +0000460 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000461 if (Forward)
462 OS << " forwarding to " << (void*)Forward;
463
464
465 if (begin() != end()) {
466 OS << "Pointers: ";
467 for (iterator I = begin(), E = end(); I != E; ++I) {
468 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000469 WriteAsOperand(OS << "(", I.getPointer());
470 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000471 }
472 }
473 if (!CallSites.empty()) {
474 OS << "\n " << CallSites.size() << " Call Sites: ";
475 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
476 if (i) OS << ", ";
477 WriteAsOperand(OS, CallSites[i].getCalledValue());
478 }
479 }
480 OS << "\n";
481}
482
483void AliasSetTracker::print(std::ostream &OS) const {
484 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
485 << PointerMap.size() << " pointer values.\n";
486 for (const_iterator I = begin(), E = end(); I != E; ++I)
487 I->print(OS);
488 OS << "\n";
489}
490
491void AliasSet::dump() const { print (std::cerr); }
492void AliasSetTracker::dump() const { print(std::cerr); }
493
Chris Lattner9971ac42003-02-24 20:37:56 +0000494//===----------------------------------------------------------------------===//
495// AliasSetPrinter Pass
496//===----------------------------------------------------------------------===//
497
498namespace {
499 class AliasSetPrinter : public FunctionPass {
500 AliasSetTracker *Tracker;
501 public:
502 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
503 AU.setPreservesAll();
504 AU.addRequired<AliasAnalysis>();
505 }
506
507 virtual bool runOnFunction(Function &F) {
508 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
509
510 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000511 Tracker->add(&*I);
Chris Lattner9971ac42003-02-24 20:37:56 +0000512 return false;
513 }
514
515 /// print - Convert to human readable form
516 virtual void print(std::ostream &OS) const {
517 Tracker->print(OS);
518 }
519
520 virtual void releaseMemory() {
521 delete Tracker;
522 }
523 };
524 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
525 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000526}