blob: a386a494fca020240cd6be84b6955b6c635a957e [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"
23#include "llvm/Support/InstIterator.h"
24#include "llvm/Support/Streams.h"
25using namespace llvm;
26
27/// mergeSetIn - Merge the specified alias set into this alias set.
28///
29void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
30 assert(!AS.Forward && "Alias set is already forwarding!");
31 assert(!Forward && "This set is a forwarding set!!");
32
33 // Update the alias and access types of this set...
34 AccessTy |= AS.AccessTy;
35 AliasTy |= AS.AliasTy;
36
37 if (AliasTy == MustAlias) {
38 // Check that these two merged sets really are must aliases. Since both
39 // used to be must-alias sets, we can just check any pointer from each set
40 // for aliasing.
41 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner47b3b8c2009-03-09 05:11:09 +000042 PointerRec *L = getSomePointer();
43 PointerRec *R = AS.getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
45 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattner47b3b8c2009-03-09 05:11:09 +000046 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 != AliasAnalysis::MustAlias)
48 AliasTy = MayAlias;
49 }
50
51 if (CallSites.empty()) { // Merge call sites...
52 if (!AS.CallSites.empty())
53 std::swap(CallSites, AS.CallSites);
54 } else if (!AS.CallSites.empty()) {
55 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
56 AS.CallSites.clear();
57 }
58
59 AS.Forward = this; // Forward across AS now...
60 addRef(); // AS is now pointing to us...
61
62 // Merge the list of constituent pointers...
63 if (AS.PtrList) {
64 *PtrListEnd = AS.PtrList;
Chris Lattner47b3b8c2009-03-09 05:11:09 +000065 AS.PtrList->setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 PtrListEnd = AS.PtrListEnd;
67
68 AS.PtrList = 0;
69 AS.PtrListEnd = &AS.PtrList;
70 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
71 }
72}
73
74void AliasSetTracker::removeAliasSet(AliasSet *AS) {
75 if (AliasSet *Fwd = AS->Forward) {
76 Fwd->dropRef(*this);
77 AS->Forward = 0;
78 }
79 AliasSets.erase(AS);
80}
81
82void AliasSet::removeFromTracker(AliasSetTracker &AST) {
83 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
84 AST.removeAliasSet(this);
85}
86
Chris Lattner47b3b8c2009-03-09 05:11:09 +000087void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 unsigned Size, bool KnownMustAlias) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +000089 assert(!Entry.hasAliasSet() && "Entry already in set!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090
91 // Check to see if we have to downgrade to _may_ alias.
92 if (isMustAlias() && !KnownMustAlias)
Chris Lattner47b3b8c2009-03-09 05:11:09 +000093 if (PointerRec *P = getSomePointer()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 AliasAnalysis &AA = AST.getAliasAnalysis();
95 AliasAnalysis::AliasResult Result =
Chris Lattner47b3b8c2009-03-09 05:11:09 +000096 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 if (Result == AliasAnalysis::MayAlias)
98 AliasTy = MayAlias;
99 else // First entry of must alias must have maximum size!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000100 P->updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
102 }
103
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000104 Entry.setAliasSet(this);
105 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 // Add it to the end of the list...
108 assert(*PtrListEnd == 0 && "End of list is not null?");
109 *PtrListEnd = &Entry;
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000110 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 assert(*PtrListEnd == 0 && "End of list is not null?");
112 addRef(); // Entry points to alias set...
113}
114
115void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
116 CallSites.push_back(CS);
117
Duncan Sands00b24b52007-12-01 07:51:45 +0000118 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
119 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
120 return;
121 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
122 AliasTy = MayAlias;
123 AccessTy |= Refs;
124 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 }
126
127 // FIXME: This should use mod/ref information to make this not suck so bad
128 AliasTy = MayAlias;
129 AccessTy = ModRef;
130}
131
132/// aliasesPointer - Return true if the specified pointer "may" (or must)
133/// alias one of the members in the set.
134///
135bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
136 AliasAnalysis &AA) const {
137 if (AliasTy == MustAlias) {
138 assert(CallSites.empty() && "Illegal must alias set!");
139
140 // If this is a set of MustAliases, only check to see if the pointer aliases
141 // SOME value in the set...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000142 PointerRec *SomePtr = getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 assert(SomePtr && "Empty must-alias set??");
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000144 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 }
146
147 // If this is a may-alias set, we have to check all of the pointers in the set
148 // to be sure it doesn't alias the set...
149 for (iterator I = begin(), E = end(); I != E; ++I)
150 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
151 return true;
152
153 // Check the call sites list and invoke list...
154 if (!CallSites.empty()) {
155 if (AA.hasNoModRefInfoForCalls())
156 return true;
157
158 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
159 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
160 != AliasAnalysis::NoModRef)
161 return true;
162 }
163
164 return false;
165}
166
167bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sands00b24b52007-12-01 07:51:45 +0000168 if (AA.doesNotAccessMemory(CS))
169 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170
171 if (AA.hasNoModRefInfoForCalls())
172 return true;
173
174 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
175 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
176 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
177 return true;
178
179 for (iterator I = begin(), E = end(); I != E; ++I)
180 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
181 AliasAnalysis::NoModRef)
182 return true;
183
184 return false;
185}
186
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000187void AliasSetTracker::clear() {
188 // Delete all the PointerRec entries.
189 for (DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.begin(),
190 E = PointerMap.end(); I != E; ++I)
191 I->second->eraseFromList();
192
193 PointerMap.clear();
194
195 // The alias sets should all be clear now.
196 AliasSets.clear();
197}
198
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199
200/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
201/// instruction referring to the pointer into. If there are multiple alias sets
202/// that may alias the pointer, merge them together and return the unified set.
203///
204AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
205 unsigned Size) {
206 AliasSet *FoundSet = 0;
207 for (iterator I = begin(), E = end(); I != E; ++I)
208 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
209 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
210 FoundSet = I; // Remember it.
211 } else { // Otherwise, we must merge the sets.
212 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
213 }
214 }
215
216 return FoundSet;
217}
218
219/// containsPointer - Return true if the specified location is represented by
220/// this alias set, false otherwise. This does not modify the AST object or
221/// alias sets.
222bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
223 for (const_iterator I = begin(), E = end(); I != E; ++I)
224 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
225 return true;
226 return false;
227}
228
229
230
231AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
232 AliasSet *FoundSet = 0;
233 for (iterator I = begin(), E = end(); I != E; ++I)
234 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
235 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
236 FoundSet = I; // Remember it.
237 } else if (!I->Forward) { // Otherwise, we must merge the sets.
238 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
239 }
240 }
241
242 return FoundSet;
243}
244
245
246
247
248/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000249/// lives in.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
251 bool *New) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000252 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253
254 // Check to see if the pointer is already known...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000255 if (Entry.hasAliasSet()) {
256 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 // Return the set!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000258 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
260 // Add it to the alias set it aliases...
261 AS->addPointer(*this, Entry, Size);
262 return *AS;
263 } else {
264 if (New) *New = true;
265 // Otherwise create a new alias set to hold the loaded pointer...
266 AliasSets.push_back(new AliasSet());
267 AliasSets.back().addPointer(*this, Entry, Size);
268 return AliasSets.back();
269 }
270}
271
272bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
273 bool NewPtr;
274 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
275 return NewPtr;
276}
277
278
279bool AliasSetTracker::add(LoadInst *LI) {
280 bool NewPtr;
281 AliasSet &AS = addPointer(LI->getOperand(0),
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000282 AA.getTargetData().getTypeStoreSize(LI->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 AliasSet::Refs, NewPtr);
284 if (LI->isVolatile()) AS.setVolatile();
285 return NewPtr;
286}
287
288bool AliasSetTracker::add(StoreInst *SI) {
289 bool NewPtr;
290 Value *Val = SI->getOperand(0);
291 AliasSet &AS = addPointer(SI->getOperand(1),
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000292 AA.getTargetData().getTypeStoreSize(Val->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 AliasSet::Mods, NewPtr);
294 if (SI->isVolatile()) AS.setVolatile();
295 return NewPtr;
296}
297
298bool AliasSetTracker::add(FreeInst *FI) {
299 bool NewPtr;
Chris Lattner81f40a92008-05-22 03:23:06 +0000300 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 return NewPtr;
302}
303
Dan Gohmanddcc8562008-04-14 18:34:50 +0000304bool AliasSetTracker::add(VAArgInst *VAAI) {
305 bool NewPtr;
Chris Lattner81f40a92008-05-22 03:23:06 +0000306 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000307 return NewPtr;
308}
309
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310
311bool AliasSetTracker::add(CallSite CS) {
Zhou Shengf74f7a82009-03-03 06:02:04 +0000312 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
313 return true; // Ignore DbgInfo Intrinsics.
Duncan Sands00b24b52007-12-01 07:51:45 +0000314 if (AA.doesNotAccessMemory(CS))
315 return true; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316
317 AliasSet *AS = findAliasSetForCallSite(CS);
318 if (!AS) {
319 AliasSets.push_back(new AliasSet());
320 AS = &AliasSets.back();
321 AS->addCallSite(CS, AA);
322 return true;
323 } else {
324 AS->addCallSite(CS, AA);
325 return false;
326 }
327}
328
329bool AliasSetTracker::add(Instruction *I) {
330 // Dispatch to one of the other add methods...
331 if (LoadInst *LI = dyn_cast<LoadInst>(I))
332 return add(LI);
333 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
334 return add(SI);
335 else if (CallInst *CI = dyn_cast<CallInst>(I))
336 return add(CI);
337 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
338 return add(II);
339 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
340 return add(FI);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000341 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
342 return add(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 return true;
344}
345
346void AliasSetTracker::add(BasicBlock &BB) {
347 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
348 add(I);
349}
350
351void AliasSetTracker::add(const AliasSetTracker &AST) {
352 assert(&AA == &AST.AA &&
353 "Merging AliasSetTracker objects with different Alias Analyses!");
354
355 // Loop over all of the alias sets in AST, adding the pointers contained
356 // therein into the current alias sets. This can cause alias sets to be
357 // merged together in the current AST.
358 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
359 if (!I->Forward) { // Ignore forwarding alias sets
360 AliasSet &AS = const_cast<AliasSet&>(*I);
361
362 // If there are any call sites in the alias set, add them to this AST.
363 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
364 add(AS.CallSites[i]);
365
366 // Loop over all of the pointers in this alias set...
367 AliasSet::iterator I = AS.begin(), E = AS.end();
368 bool X;
369 for (; I != E; ++I) {
370 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
371 (AliasSet::AccessType)AS.AccessTy, X);
372 if (AS.isVolatile()) NewAS.setVolatile();
373 }
374 }
375}
376
377/// remove - Remove the specified (potentially non-empty) alias set from the
378/// tracker.
379void AliasSetTracker::remove(AliasSet &AS) {
380 // Drop all call sites.
381 AS.CallSites.clear();
382
383 // Clear the alias set.
384 unsigned NumRefs = 0;
385 while (!AS.empty()) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000386 AliasSet::PointerRec *P = AS.PtrList;
387
388 Value *ValToRemove = P->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000390 // Unlink and delete entry from the list of values.
391 P->eraseFromList();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392
393 // Remember how many references need to be dropped.
394 ++NumRefs;
395
396 // Finally, remove the entry.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000397 PointerMap.erase(ValToRemove);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 }
399
400 // Stop using the alias set, removing it.
401 AS.RefCount -= NumRefs;
402 if (AS.RefCount == 0)
403 AS.removeFromTracker(*this);
404}
405
406bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
407 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
408 if (!AS) return false;
409 remove(*AS);
410 return true;
411}
412
413bool AliasSetTracker::remove(LoadInst *LI) {
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000414 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
416 if (!AS) return false;
417 remove(*AS);
418 return true;
419}
420
421bool AliasSetTracker::remove(StoreInst *SI) {
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000422 unsigned Size =
423 AA.getTargetData().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.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000488 DenseMap<Value*, AliasSet::PointerRec*>::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.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000514 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(From);
515 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...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000523 AliasSet *AS = I->second->getAliasSet(*this);
524 AS->addPointer(*this, Entry, I->second->getSize(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525}
526
527
528
529//===----------------------------------------------------------------------===//
530// AliasSet/AliasSetTracker Printing Support
531//===----------------------------------------------------------------------===//
532
533void AliasSet::print(std::ostream &OS) const {
534 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
Dan Gohmancbec0762008-04-21 19:48:48 +0000535 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 switch (AccessTy) {
537 case NoModRef: OS << "No access "; break;
538 case Refs : OS << "Ref "; break;
539 case Mods : OS << "Mod "; break;
540 case ModRef : OS << "Mod/Ref "; break;
541 default: assert(0 && "Bad value for AccessTy!");
542 }
543 if (isVolatile()) OS << "[volatile] ";
544 if (Forward)
545 OS << " forwarding to " << (void*)Forward;
546
547
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000548 if (!empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 OS << "Pointers: ";
550 for (iterator I = begin(), E = end(); I != E; ++I) {
551 if (I != begin()) OS << ", ";
552 WriteAsOperand(OS << "(", I.getPointer());
553 OS << ", " << I.getSize() << ")";
554 }
555 }
556 if (!CallSites.empty()) {
557 OS << "\n " << CallSites.size() << " Call Sites: ";
558 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
559 if (i) OS << ", ";
560 WriteAsOperand(OS, CallSites[i].getCalledValue());
561 }
562 }
563 OS << "\n";
564}
565
566void AliasSetTracker::print(std::ostream &OS) const {
567 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
568 << PointerMap.size() << " pointer values.\n";
569 for (const_iterator I = begin(), E = end(); I != E; ++I)
570 I->print(OS);
571 OS << "\n";
572}
573
574void AliasSet::dump() const { print (cerr); }
575void AliasSetTracker::dump() const { print(cerr); }
576
577//===----------------------------------------------------------------------===//
578// AliasSetPrinter Pass
579//===----------------------------------------------------------------------===//
580
581namespace {
582 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
583 AliasSetTracker *Tracker;
584 public:
585 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000586 AliasSetPrinter() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587
588 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
589 AU.setPreservesAll();
590 AU.addRequired<AliasAnalysis>();
591 }
592
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 virtual bool runOnFunction(Function &F) {
594 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
595
596 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
597 Tracker->add(&*I);
598 Tracker->print(cerr);
599 delete Tracker;
600 return false;
601 }
602 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603}
Dan Gohman089efff2008-05-13 00:00:25 +0000604
605char AliasSetPrinter::ID = 0;
606static RegisterPass<AliasSetPrinter>
607X("print-alias-sets", "Alias Set Printer", false, true);