blob: 760964c806b9ac5e7bc4d89791a4df4c81679151 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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/Instructions.h"
Zhou Shengf74f7a82009-03-03 06:02:04 +000017#include "llvm/IntrinsicInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Pass.h"
19#include "llvm/Type.h"
20#include "llvm/Target/TargetData.h"
21#include "llvm/Assembly/Writer.h"
22#include "llvm/Support/Compiler.h"
Edwin Török675d5622009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Support/InstIterator.h"
25#include "llvm/Support/Streams.h"
26using namespace llvm;
27
28/// mergeSetIn - Merge the specified alias set into this alias set.
29///
30void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
31 assert(!AS.Forward && "Alias set is already forwarding!");
32 assert(!Forward && "This set is a forwarding set!!");
33
34 // Update the alias and access types of this set...
35 AccessTy |= AS.AccessTy;
36 AliasTy |= AS.AliasTy;
37
38 if (AliasTy == MustAlias) {
39 // Check that these two merged sets really are must aliases. Since both
40 // used to be must-alias sets, we can just check any pointer from each set
41 // for aliasing.
42 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner47b3b8c2009-03-09 05:11:09 +000043 PointerRec *L = getSomePointer();
44 PointerRec *R = AS.getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
46 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattner47b3b8c2009-03-09 05:11:09 +000047 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 != AliasAnalysis::MustAlias)
49 AliasTy = MayAlias;
50 }
51
52 if (CallSites.empty()) { // Merge call sites...
53 if (!AS.CallSites.empty())
54 std::swap(CallSites, AS.CallSites);
55 } else if (!AS.CallSites.empty()) {
56 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
57 AS.CallSites.clear();
58 }
59
60 AS.Forward = this; // Forward across AS now...
61 addRef(); // AS is now pointing to us...
62
63 // Merge the list of constituent pointers...
64 if (AS.PtrList) {
65 *PtrListEnd = AS.PtrList;
Chris Lattner47b3b8c2009-03-09 05:11:09 +000066 AS.PtrList->setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 PtrListEnd = AS.PtrListEnd;
68
69 AS.PtrList = 0;
70 AS.PtrListEnd = &AS.PtrList;
71 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
72 }
73}
74
75void AliasSetTracker::removeAliasSet(AliasSet *AS) {
76 if (AliasSet *Fwd = AS->Forward) {
77 Fwd->dropRef(*this);
78 AS->Forward = 0;
79 }
80 AliasSets.erase(AS);
81}
82
83void AliasSet::removeFromTracker(AliasSetTracker &AST) {
84 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
85 AST.removeAliasSet(this);
86}
87
Chris Lattner47b3b8c2009-03-09 05:11:09 +000088void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 unsigned Size, bool KnownMustAlias) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +000090 assert(!Entry.hasAliasSet() && "Entry already in set!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091
92 // Check to see if we have to downgrade to _may_ alias.
93 if (isMustAlias() && !KnownMustAlias)
Chris Lattner47b3b8c2009-03-09 05:11:09 +000094 if (PointerRec *P = getSomePointer()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 AliasAnalysis &AA = AST.getAliasAnalysis();
96 AliasAnalysis::AliasResult Result =
Chris Lattner47b3b8c2009-03-09 05:11:09 +000097 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 if (Result == AliasAnalysis::MayAlias)
99 AliasTy = MayAlias;
100 else // First entry of must alias must have maximum size!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000101 P->updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
103 }
104
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000105 Entry.setAliasSet(this);
106 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107
108 // Add it to the end of the list...
109 assert(*PtrListEnd == 0 && "End of list is not null?");
110 *PtrListEnd = &Entry;
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000111 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 assert(*PtrListEnd == 0 && "End of list is not null?");
113 addRef(); // Entry points to alias set...
114}
115
116void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
117 CallSites.push_back(CS);
118
Duncan Sands00b24b52007-12-01 07:51:45 +0000119 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
120 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
121 return;
122 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
123 AliasTy = MayAlias;
124 AccessTy |= Refs;
125 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 }
127
128 // FIXME: This should use mod/ref information to make this not suck so bad
129 AliasTy = MayAlias;
130 AccessTy = ModRef;
131}
132
133/// aliasesPointer - Return true if the specified pointer "may" (or must)
134/// alias one of the members in the set.
135///
136bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
137 AliasAnalysis &AA) const {
138 if (AliasTy == MustAlias) {
139 assert(CallSites.empty() && "Illegal must alias set!");
140
141 // If this is a set of MustAliases, only check to see if the pointer aliases
142 // SOME value in the set...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000143 PointerRec *SomePtr = getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 assert(SomePtr && "Empty must-alias set??");
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000145 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 }
147
148 // If this is a may-alias set, we have to check all of the pointers in the set
149 // to be sure it doesn't alias the set...
150 for (iterator I = begin(), E = end(); I != E; ++I)
151 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
152 return true;
153
154 // Check the call sites list and invoke list...
155 if (!CallSites.empty()) {
156 if (AA.hasNoModRefInfoForCalls())
157 return true;
158
159 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
160 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
161 != AliasAnalysis::NoModRef)
162 return true;
163 }
164
165 return false;
166}
167
168bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sands00b24b52007-12-01 07:51:45 +0000169 if (AA.doesNotAccessMemory(CS))
170 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171
172 if (AA.hasNoModRefInfoForCalls())
173 return true;
174
175 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
176 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
177 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
178 return true;
179
180 for (iterator I = begin(), E = end(); I != E; ++I)
181 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
182 AliasAnalysis::NoModRef)
183 return true;
184
185 return false;
186}
187
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000188void AliasSetTracker::clear() {
189 // Delete all the PointerRec entries.
Dan Gohman63b64a72009-07-30 20:21:41 +0000190 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
191 I != E; ++I)
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000192 I->second->eraseFromList();
193
194 PointerMap.clear();
195
196 // The alias sets should all be clear now.
197 AliasSets.clear();
198}
199
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
201/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
202/// instruction referring to the pointer into. If there are multiple alias sets
203/// that may alias the pointer, merge them together and return the unified set.
204///
205AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
206 unsigned Size) {
207 AliasSet *FoundSet = 0;
208 for (iterator I = begin(), E = end(); I != E; ++I)
209 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
210 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
211 FoundSet = I; // Remember it.
212 } else { // Otherwise, we must merge the sets.
213 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
214 }
215 }
216
217 return FoundSet;
218}
219
220/// containsPointer - Return true if the specified location is represented by
221/// this alias set, false otherwise. This does not modify the AST object or
222/// alias sets.
223bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
224 for (const_iterator I = begin(), E = end(); I != E; ++I)
225 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
226 return true;
227 return false;
228}
229
230
231
232AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
233 AliasSet *FoundSet = 0;
234 for (iterator I = begin(), E = end(); I != E; ++I)
235 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
236 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
237 FoundSet = I; // Remember it.
238 } else if (!I->Forward) { // Otherwise, we must merge the sets.
239 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
240 }
241 }
242
243 return FoundSet;
244}
245
246
247
248
249/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000250/// lives in.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
252 bool *New) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000253 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254
255 // Check to see if the pointer is already known...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000256 if (Entry.hasAliasSet()) {
257 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 // Return the set!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000259 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
261 // Add it to the alias set it aliases...
262 AS->addPointer(*this, Entry, Size);
263 return *AS;
264 } else {
265 if (New) *New = true;
266 // Otherwise create a new alias set to hold the loaded pointer...
267 AliasSets.push_back(new AliasSet());
268 AliasSets.back().addPointer(*this, Entry, Size);
269 return AliasSets.back();
270 }
271}
272
273bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
274 bool NewPtr;
275 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
276 return NewPtr;
277}
278
279
280bool AliasSetTracker::add(LoadInst *LI) {
281 bool NewPtr;
282 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohman624f9612009-07-25 00:48:42 +0000283 AA.getTypeStoreSize(LI->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 AliasSet::Refs, NewPtr);
285 if (LI->isVolatile()) AS.setVolatile();
286 return NewPtr;
287}
288
289bool AliasSetTracker::add(StoreInst *SI) {
290 bool NewPtr;
291 Value *Val = SI->getOperand(0);
292 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +0000293 AA.getTypeStoreSize(Val->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 AliasSet::Mods, NewPtr);
295 if (SI->isVolatile()) AS.setVolatile();
296 return NewPtr;
297}
298
299bool AliasSetTracker::add(FreeInst *FI) {
300 bool NewPtr;
Chris Lattner81f40a92008-05-22 03:23:06 +0000301 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 return NewPtr;
303}
304
Dan Gohmanddcc8562008-04-14 18:34:50 +0000305bool AliasSetTracker::add(VAArgInst *VAAI) {
306 bool NewPtr;
Chris Lattner81f40a92008-05-22 03:23:06 +0000307 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000308 return NewPtr;
309}
310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311
312bool AliasSetTracker::add(CallSite CS) {
Zhou Shengf74f7a82009-03-03 06:02:04 +0000313 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
314 return true; // Ignore DbgInfo Intrinsics.
Duncan Sands00b24b52007-12-01 07:51:45 +0000315 if (AA.doesNotAccessMemory(CS))
316 return true; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317
318 AliasSet *AS = findAliasSetForCallSite(CS);
319 if (!AS) {
320 AliasSets.push_back(new AliasSet());
321 AS = &AliasSets.back();
322 AS->addCallSite(CS, AA);
323 return true;
324 } else {
325 AS->addCallSite(CS, AA);
326 return false;
327 }
328}
329
330bool AliasSetTracker::add(Instruction *I) {
331 // Dispatch to one of the other add methods...
332 if (LoadInst *LI = dyn_cast<LoadInst>(I))
333 return add(LI);
334 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
335 return add(SI);
336 else if (CallInst *CI = dyn_cast<CallInst>(I))
337 return add(CI);
338 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
339 return add(II);
340 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
341 return add(FI);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000342 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
343 return add(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 return true;
345}
346
347void AliasSetTracker::add(BasicBlock &BB) {
348 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
349 add(I);
350}
351
352void AliasSetTracker::add(const AliasSetTracker &AST) {
353 assert(&AA == &AST.AA &&
354 "Merging AliasSetTracker objects with different Alias Analyses!");
355
356 // Loop over all of the alias sets in AST, adding the pointers contained
357 // therein into the current alias sets. This can cause alias sets to be
358 // merged together in the current AST.
359 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
360 if (!I->Forward) { // Ignore forwarding alias sets
361 AliasSet &AS = const_cast<AliasSet&>(*I);
362
363 // If there are any call sites in the alias set, add them to this AST.
364 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
365 add(AS.CallSites[i]);
366
367 // Loop over all of the pointers in this alias set...
368 AliasSet::iterator I = AS.begin(), E = AS.end();
369 bool X;
370 for (; I != E; ++I) {
371 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
372 (AliasSet::AccessType)AS.AccessTy, X);
373 if (AS.isVolatile()) NewAS.setVolatile();
374 }
375 }
376}
377
378/// remove - Remove the specified (potentially non-empty) alias set from the
379/// tracker.
380void AliasSetTracker::remove(AliasSet &AS) {
381 // Drop all call sites.
382 AS.CallSites.clear();
383
384 // Clear the alias set.
385 unsigned NumRefs = 0;
386 while (!AS.empty()) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000387 AliasSet::PointerRec *P = AS.PtrList;
388
389 Value *ValToRemove = P->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000391 // Unlink and delete entry from the list of values.
392 P->eraseFromList();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393
394 // Remember how many references need to be dropped.
395 ++NumRefs;
396
397 // Finally, remove the entry.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000398 PointerMap.erase(ValToRemove);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 }
400
401 // Stop using the alias set, removing it.
402 AS.RefCount -= NumRefs;
403 if (AS.RefCount == 0)
404 AS.removeFromTracker(*this);
405}
406
407bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
408 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
409 if (!AS) return false;
410 remove(*AS);
411 return true;
412}
413
414bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000415 unsigned Size = AA.getTypeStoreSize(LI->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
417 if (!AS) return false;
418 remove(*AS);
419 return true;
420}
421
422bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000423 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
425 if (!AS) return false;
426 remove(*AS);
427 return true;
428}
429
430bool AliasSetTracker::remove(FreeInst *FI) {
431 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
432 if (!AS) return false;
433 remove(*AS);
434 return true;
435}
436
Dan Gohmanddcc8562008-04-14 18:34:50 +0000437bool AliasSetTracker::remove(VAArgInst *VAAI) {
438 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
439 if (!AS) return false;
440 remove(*AS);
441 return true;
442}
443
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444bool AliasSetTracker::remove(CallSite CS) {
Duncan Sands00b24b52007-12-01 07:51:45 +0000445 if (AA.doesNotAccessMemory(CS))
446 return false; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447
448 AliasSet *AS = findAliasSetForCallSite(CS);
449 if (!AS) return false;
450 remove(*AS);
451 return true;
452}
453
454bool AliasSetTracker::remove(Instruction *I) {
455 // Dispatch to one of the other remove methods...
456 if (LoadInst *LI = dyn_cast<LoadInst>(I))
457 return remove(LI);
458 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
459 return remove(SI);
460 else if (CallInst *CI = dyn_cast<CallInst>(I))
461 return remove(CI);
462 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
463 return remove(FI);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000464 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
465 return remove(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 return true;
467}
468
469
470// deleteValue method - This method is used to remove a pointer value from the
471// AliasSetTracker entirely. It should be used when an instruction is deleted
472// from the program to update the AST. If you don't use this, you would have
473// dangling pointers to deleted instructions.
474//
475void AliasSetTracker::deleteValue(Value *PtrVal) {
476 // Notify the alias analysis implementation that this value is gone.
477 AA.deleteValue(PtrVal);
478
479 // If this is a call instruction, remove the callsite from the appropriate
480 // AliasSet.
481 CallSite CS = CallSite::get(PtrVal);
Duncan Sands00b24b52007-12-01 07:51:45 +0000482 if (CS.getInstruction())
483 if (!AA.doesNotAccessMemory(CS))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 if (AliasSet *AS = findAliasSetForCallSite(CS))
485 AS->removeCallSite(CS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486
487 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000488 PointerMapType::iterator I = PointerMap.find(PtrVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 if (I == PointerMap.end()) return; // Noop
490
491 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000492 AliasSet::PointerRec *PtrValEnt = I->second;
493 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000495 // Unlink and delete from the list of values.
496 PtrValEnt->eraseFromList();
497
498 // Stop using the alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 AS->dropRef(*this);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000500
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 PointerMap.erase(I);
502}
503
504// copyValue - This method should be used whenever a preexisting value in the
505// program is copied or cloned, introducing a new value. Note that it is ok for
506// clients that use this method to introduce the same value multiple times: if
507// the tracker already knows about a value, it will ignore the request.
508//
509void AliasSetTracker::copyValue(Value *From, Value *To) {
510 // Notify the alias analysis implementation that this value is copied.
511 AA.copyValue(From, To);
512
513 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000514 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000515 if (I == PointerMap.end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 return; // Noop
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000517 assert(I->second->hasAliasSet() && "Dead entry?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000519 AliasSet::PointerRec &Entry = getEntryFor(To);
520 if (Entry.hasAliasSet()) return; // Already in the tracker!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521
522 // Add it to the alias set it aliases...
Devang Patel5d11d432009-03-30 18:34:47 +0000523 I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000524 AliasSet *AS = I->second->getAliasSet(*this);
525 AS->addPointer(*this, Entry, I->second->getSize(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526}
527
528
529
530//===----------------------------------------------------------------------===//
531// AliasSet/AliasSetTracker Printing Support
532//===----------------------------------------------------------------------===//
533
534void AliasSet::print(std::ostream &OS) const {
535 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
Dan Gohmancbec0762008-04-21 19:48:48 +0000536 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 switch (AccessTy) {
538 case NoModRef: OS << "No access "; break;
539 case Refs : OS << "Ref "; break;
540 case Mods : OS << "Mod "; break;
541 case ModRef : OS << "Mod/Ref "; break;
Edwin Törökbd448e32009-07-14 16:55:14 +0000542 default: llvm_unreachable("Bad value for AccessTy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 }
544 if (isVolatile()) OS << "[volatile] ";
545 if (Forward)
546 OS << " forwarding to " << (void*)Forward;
547
548
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000549 if (!empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 OS << "Pointers: ";
551 for (iterator I = begin(), E = end(); I != E; ++I) {
552 if (I != begin()) OS << ", ";
553 WriteAsOperand(OS << "(", I.getPointer());
554 OS << ", " << I.getSize() << ")";
555 }
556 }
557 if (!CallSites.empty()) {
558 OS << "\n " << CallSites.size() << " Call Sites: ";
559 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
560 if (i) OS << ", ";
561 WriteAsOperand(OS, CallSites[i].getCalledValue());
562 }
563 }
564 OS << "\n";
565}
566
567void AliasSetTracker::print(std::ostream &OS) const {
568 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
569 << PointerMap.size() << " pointer values.\n";
570 for (const_iterator I = begin(), E = end(); I != E; ++I)
571 I->print(OS);
572 OS << "\n";
573}
574
575void AliasSet::dump() const { print (cerr); }
576void AliasSetTracker::dump() const { print(cerr); }
577
578//===----------------------------------------------------------------------===//
Dan Gohman63b64a72009-07-30 20:21:41 +0000579// ASTCallbackVH Class Implementation
580//===----------------------------------------------------------------------===//
581
582void AliasSetTracker::ASTCallbackVH::deleted() {
583 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
584 AST->deleteValue(getValPtr());
585 // this now dangles!
586}
587
588AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohman1e5054e2009-07-31 18:21:48 +0000589 : CallbackVH(V), AST(ast) {}
590
591AliasSetTracker::ASTCallbackVH &
592AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
593 return *this = ASTCallbackVH(V, AST);
594}
Dan Gohman63b64a72009-07-30 20:21:41 +0000595
596//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597// AliasSetPrinter Pass
598//===----------------------------------------------------------------------===//
599
600namespace {
601 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
602 AliasSetTracker *Tracker;
603 public:
604 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000605 AliasSetPrinter() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606
607 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
608 AU.setPreservesAll();
609 AU.addRequired<AliasAnalysis>();
610 }
611
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 virtual bool runOnFunction(Function &F) {
613 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
614
615 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
616 Tracker->add(&*I);
617 Tracker->print(cerr);
618 delete Tracker;
619 return false;
620 }
621 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622}
Dan Gohman089efff2008-05-13 00:00:25 +0000623
624char AliasSetPrinter::ID = 0;
625static RegisterPass<AliasSetPrinter>
626X("print-alias-sets", "Alias Set Printer", false, true);