blob: 02aff50d8a13a3738c87ead43caf915743c5d8df [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"
David Greenee7ef6a92009-12-23 19:27:59 +000022#include "llvm/Support/Debug.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"
Chris Lattnera7a9daa2009-08-23 05:17:37 +000025#include "llvm/Support/Format.h"
26#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28
29/// mergeSetIn - Merge the specified alias set into this alias set.
30///
31void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
32 assert(!AS.Forward && "Alias set is already forwarding!");
33 assert(!Forward && "This set is a forwarding set!!");
34
35 // Update the alias and access types of this set...
36 AccessTy |= AS.AccessTy;
37 AliasTy |= AS.AliasTy;
38
39 if (AliasTy == MustAlias) {
40 // Check that these two merged sets really are must aliases. Since both
41 // used to be must-alias sets, we can just check any pointer from each set
42 // for aliasing.
43 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner47b3b8c2009-03-09 05:11:09 +000044 PointerRec *L = getSomePointer();
45 PointerRec *R = AS.getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattner47b3b8c2009-03-09 05:11:09 +000048 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 != AliasAnalysis::MustAlias)
50 AliasTy = MayAlias;
51 }
52
53 if (CallSites.empty()) { // Merge call sites...
54 if (!AS.CallSites.empty())
55 std::swap(CallSites, AS.CallSites);
56 } else if (!AS.CallSites.empty()) {
57 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
58 AS.CallSites.clear();
59 }
60
61 AS.Forward = this; // Forward across AS now...
62 addRef(); // AS is now pointing to us...
63
64 // Merge the list of constituent pointers...
65 if (AS.PtrList) {
66 *PtrListEnd = AS.PtrList;
Chris Lattner47b3b8c2009-03-09 05:11:09 +000067 AS.PtrList->setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 PtrListEnd = AS.PtrListEnd;
69
70 AS.PtrList = 0;
71 AS.PtrListEnd = &AS.PtrList;
72 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
73 }
74}
75
76void AliasSetTracker::removeAliasSet(AliasSet *AS) {
77 if (AliasSet *Fwd = AS->Forward) {
78 Fwd->dropRef(*this);
79 AS->Forward = 0;
80 }
81 AliasSets.erase(AS);
82}
83
84void AliasSet::removeFromTracker(AliasSetTracker &AST) {
85 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
86 AST.removeAliasSet(this);
87}
88
Chris Lattner47b3b8c2009-03-09 05:11:09 +000089void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 unsigned Size, bool KnownMustAlias) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +000091 assert(!Entry.hasAliasSet() && "Entry already in set!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93 // Check to see if we have to downgrade to _may_ alias.
94 if (isMustAlias() && !KnownMustAlias)
Chris Lattner47b3b8c2009-03-09 05:11:09 +000095 if (PointerRec *P = getSomePointer()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 AliasAnalysis &AA = AST.getAliasAnalysis();
97 AliasAnalysis::AliasResult Result =
Chris Lattner47b3b8c2009-03-09 05:11:09 +000098 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 if (Result == AliasAnalysis::MayAlias)
100 AliasTy = MayAlias;
101 else // First entry of must alias must have maximum size!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000102 P->updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
104 }
105
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000106 Entry.setAliasSet(this);
107 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
109 // Add it to the end of the list...
110 assert(*PtrListEnd == 0 && "End of list is not null?");
111 *PtrListEnd = &Entry;
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000112 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 assert(*PtrListEnd == 0 && "End of list is not null?");
114 addRef(); // Entry points to alias set...
115}
116
117void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
118 CallSites.push_back(CS);
119
Duncan Sands00b24b52007-12-01 07:51:45 +0000120 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
121 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
122 return;
123 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
124 AliasTy = MayAlias;
125 AccessTy |= Refs;
126 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 }
128
129 // FIXME: This should use mod/ref information to make this not suck so bad
130 AliasTy = MayAlias;
131 AccessTy = ModRef;
132}
133
134/// aliasesPointer - Return true if the specified pointer "may" (or must)
135/// alias one of the members in the set.
136///
137bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
138 AliasAnalysis &AA) const {
139 if (AliasTy == MustAlias) {
140 assert(CallSites.empty() && "Illegal must alias set!");
141
142 // If this is a set of MustAliases, only check to see if the pointer aliases
143 // SOME value in the set...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000144 PointerRec *SomePtr = getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 assert(SomePtr && "Empty must-alias set??");
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000146 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 }
148
149 // If this is a may-alias set, we have to check all of the pointers in the set
150 // to be sure it doesn't alias the set...
151 for (iterator I = begin(), E = end(); I != E; ++I)
152 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
153 return true;
154
155 // Check the call sites list and invoke list...
156 if (!CallSites.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
158 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
159 != AliasAnalysis::NoModRef)
160 return true;
161 }
162
163 return false;
164}
165
166bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sands00b24b52007-12-01 07:51:45 +0000167 if (AA.doesNotAccessMemory(CS))
168 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
171 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
172 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
173 return true;
174
175 for (iterator I = begin(), E = end(); I != E; ++I)
176 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
177 AliasAnalysis::NoModRef)
178 return true;
179
180 return false;
181}
182
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000183void AliasSetTracker::clear() {
184 // Delete all the PointerRec entries.
Dan Gohman63b64a72009-07-30 20:21:41 +0000185 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
186 I != E; ++I)
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000187 I->second->eraseFromList();
188
189 PointerMap.clear();
190
191 // The alias sets should all be clear now.
192 AliasSets.clear();
193}
194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195
196/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
197/// instruction referring to the pointer into. If there are multiple alias sets
198/// that may alias the pointer, merge them together and return the unified set.
199///
200AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
201 unsigned Size) {
202 AliasSet *FoundSet = 0;
203 for (iterator I = begin(), E = end(); I != E; ++I)
204 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
205 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
206 FoundSet = I; // Remember it.
207 } else { // Otherwise, we must merge the sets.
208 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
209 }
210 }
211
212 return FoundSet;
213}
214
215/// containsPointer - Return true if the specified location is represented by
216/// this alias set, false otherwise. This does not modify the AST object or
217/// alias sets.
218bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
219 for (const_iterator I = begin(), E = end(); I != E; ++I)
220 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
221 return true;
222 return false;
223}
224
225
226
227AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
228 AliasSet *FoundSet = 0;
229 for (iterator I = begin(), E = end(); I != E; ++I)
230 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
231 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
232 FoundSet = I; // Remember it.
233 } else if (!I->Forward) { // Otherwise, we must merge the sets.
234 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
235 }
236 }
237
238 return FoundSet;
239}
240
241
242
243
244/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000245/// lives in.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
247 bool *New) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000248 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249
250 // Check to see if the pointer is already known...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000251 if (Entry.hasAliasSet()) {
252 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 // Return the set!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000254 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
256 // Add it to the alias set it aliases...
257 AS->addPointer(*this, Entry, Size);
258 return *AS;
259 } else {
260 if (New) *New = true;
261 // Otherwise create a new alias set to hold the loaded pointer...
262 AliasSets.push_back(new AliasSet());
263 AliasSets.back().addPointer(*this, Entry, Size);
264 return AliasSets.back();
265 }
266}
267
268bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
269 bool NewPtr;
270 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
271 return NewPtr;
272}
273
274
275bool AliasSetTracker::add(LoadInst *LI) {
276 bool NewPtr;
277 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohman624f9612009-07-25 00:48:42 +0000278 AA.getTypeStoreSize(LI->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 AliasSet::Refs, NewPtr);
280 if (LI->isVolatile()) AS.setVolatile();
281 return NewPtr;
282}
283
284bool AliasSetTracker::add(StoreInst *SI) {
285 bool NewPtr;
286 Value *Val = SI->getOperand(0);
287 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +0000288 AA.getTypeStoreSize(Val->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 AliasSet::Mods, NewPtr);
290 if (SI->isVolatile()) AS.setVolatile();
291 return NewPtr;
292}
293
Dan Gohmanddcc8562008-04-14 18:34:50 +0000294bool AliasSetTracker::add(VAArgInst *VAAI) {
295 bool NewPtr;
Chris Lattner81f40a92008-05-22 03:23:06 +0000296 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000297 return NewPtr;
298}
299
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300
301bool AliasSetTracker::add(CallSite CS) {
Zhou Shengf74f7a82009-03-03 06:02:04 +0000302 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
303 return true; // Ignore DbgInfo Intrinsics.
Duncan Sands00b24b52007-12-01 07:51:45 +0000304 if (AA.doesNotAccessMemory(CS))
305 return true; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
307 AliasSet *AS = findAliasSetForCallSite(CS);
308 if (!AS) {
309 AliasSets.push_back(new AliasSet());
310 AS = &AliasSets.back();
311 AS->addCallSite(CS, AA);
312 return true;
313 } else {
314 AS->addCallSite(CS, AA);
315 return false;
316 }
317}
318
319bool AliasSetTracker::add(Instruction *I) {
320 // Dispatch to one of the other add methods...
321 if (LoadInst *LI = dyn_cast<LoadInst>(I))
322 return add(LI);
323 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
324 return add(SI);
325 else if (CallInst *CI = dyn_cast<CallInst>(I))
326 return add(CI);
327 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
328 return add(II);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000329 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
330 return add(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 return true;
332}
333
334void AliasSetTracker::add(BasicBlock &BB) {
335 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
336 add(I);
337}
338
339void AliasSetTracker::add(const AliasSetTracker &AST) {
340 assert(&AA == &AST.AA &&
341 "Merging AliasSetTracker objects with different Alias Analyses!");
342
343 // Loop over all of the alias sets in AST, adding the pointers contained
344 // therein into the current alias sets. This can cause alias sets to be
345 // merged together in the current AST.
346 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
347 if (!I->Forward) { // Ignore forwarding alias sets
348 AliasSet &AS = const_cast<AliasSet&>(*I);
349
350 // If there are any call sites in the alias set, add them to this AST.
351 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
352 add(AS.CallSites[i]);
353
354 // Loop over all of the pointers in this alias set...
355 AliasSet::iterator I = AS.begin(), E = AS.end();
356 bool X;
357 for (; I != E; ++I) {
358 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
359 (AliasSet::AccessType)AS.AccessTy, X);
360 if (AS.isVolatile()) NewAS.setVolatile();
361 }
362 }
363}
364
365/// remove - Remove the specified (potentially non-empty) alias set from the
366/// tracker.
367void AliasSetTracker::remove(AliasSet &AS) {
368 // Drop all call sites.
369 AS.CallSites.clear();
370
371 // Clear the alias set.
372 unsigned NumRefs = 0;
373 while (!AS.empty()) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000374 AliasSet::PointerRec *P = AS.PtrList;
375
376 Value *ValToRemove = P->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000378 // Unlink and delete entry from the list of values.
379 P->eraseFromList();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380
381 // Remember how many references need to be dropped.
382 ++NumRefs;
383
384 // Finally, remove the entry.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000385 PointerMap.erase(ValToRemove);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 }
387
388 // Stop using the alias set, removing it.
389 AS.RefCount -= NumRefs;
390 if (AS.RefCount == 0)
391 AS.removeFromTracker(*this);
392}
393
394bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
395 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
396 if (!AS) return false;
397 remove(*AS);
398 return true;
399}
400
401bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000402 unsigned Size = AA.getTypeStoreSize(LI->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
404 if (!AS) return false;
405 remove(*AS);
406 return true;
407}
408
409bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000410 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
412 if (!AS) return false;
413 remove(*AS);
414 return true;
415}
416
Dan Gohmanddcc8562008-04-14 18:34:50 +0000417bool AliasSetTracker::remove(VAArgInst *VAAI) {
418 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
419 if (!AS) return false;
420 remove(*AS);
421 return true;
422}
423
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424bool AliasSetTracker::remove(CallSite CS) {
Duncan Sands00b24b52007-12-01 07:51:45 +0000425 if (AA.doesNotAccessMemory(CS))
426 return false; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427
428 AliasSet *AS = findAliasSetForCallSite(CS);
429 if (!AS) return false;
430 remove(*AS);
431 return true;
432}
433
434bool AliasSetTracker::remove(Instruction *I) {
435 // Dispatch to one of the other remove methods...
436 if (LoadInst *LI = dyn_cast<LoadInst>(I))
437 return remove(LI);
438 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
439 return remove(SI);
440 else if (CallInst *CI = dyn_cast<CallInst>(I))
441 return remove(CI);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000442 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
443 return remove(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 return true;
445}
446
447
448// deleteValue method - This method is used to remove a pointer value from the
449// AliasSetTracker entirely. It should be used when an instruction is deleted
450// from the program to update the AST. If you don't use this, you would have
451// dangling pointers to deleted instructions.
452//
453void AliasSetTracker::deleteValue(Value *PtrVal) {
454 // Notify the alias analysis implementation that this value is gone.
455 AA.deleteValue(PtrVal);
456
457 // If this is a call instruction, remove the callsite from the appropriate
458 // AliasSet.
459 CallSite CS = CallSite::get(PtrVal);
Duncan Sands00b24b52007-12-01 07:51:45 +0000460 if (CS.getInstruction())
461 if (!AA.doesNotAccessMemory(CS))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 if (AliasSet *AS = findAliasSetForCallSite(CS))
463 AS->removeCallSite(CS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464
465 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000466 PointerMapType::iterator I = PointerMap.find(PtrVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 if (I == PointerMap.end()) return; // Noop
468
469 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000470 AliasSet::PointerRec *PtrValEnt = I->second;
471 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000473 // Unlink and delete from the list of values.
474 PtrValEnt->eraseFromList();
475
476 // Stop using the alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 AS->dropRef(*this);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000478
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 PointerMap.erase(I);
480}
481
482// copyValue - This method should be used whenever a preexisting value in the
483// program is copied or cloned, introducing a new value. Note that it is ok for
484// clients that use this method to introduce the same value multiple times: if
485// the tracker already knows about a value, it will ignore the request.
486//
487void AliasSetTracker::copyValue(Value *From, Value *To) {
488 // Notify the alias analysis implementation that this value is copied.
489 AA.copyValue(From, To);
490
491 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000492 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000493 if (I == PointerMap.end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 return; // Noop
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000495 assert(I->second->hasAliasSet() && "Dead entry?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000497 AliasSet::PointerRec &Entry = getEntryFor(To);
498 if (Entry.hasAliasSet()) return; // Already in the tracker!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499
500 // Add it to the alias set it aliases...
Devang Patel5d11d432009-03-30 18:34:47 +0000501 I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000502 AliasSet *AS = I->second->getAliasSet(*this);
503 AS->addPointer(*this, Entry, I->second->getSize(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504}
505
506
507
508//===----------------------------------------------------------------------===//
509// AliasSet/AliasSetTracker Printing Support
510//===----------------------------------------------------------------------===//
511
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000512void AliasSet::print(raw_ostream &OS) const {
513 OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] ";
Dan Gohmancbec0762008-04-21 19:48:48 +0000514 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 switch (AccessTy) {
516 case NoModRef: OS << "No access "; break;
517 case Refs : OS << "Ref "; break;
518 case Mods : OS << "Mod "; break;
519 case ModRef : OS << "Mod/Ref "; break;
Edwin Törökbd448e32009-07-14 16:55:14 +0000520 default: llvm_unreachable("Bad value for AccessTy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 }
522 if (isVolatile()) OS << "[volatile] ";
523 if (Forward)
524 OS << " forwarding to " << (void*)Forward;
525
526
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000527 if (!empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 OS << "Pointers: ";
529 for (iterator I = begin(), E = end(); I != E; ++I) {
530 if (I != begin()) OS << ", ";
531 WriteAsOperand(OS << "(", I.getPointer());
532 OS << ", " << I.getSize() << ")";
533 }
534 }
535 if (!CallSites.empty()) {
536 OS << "\n " << CallSites.size() << " Call Sites: ";
537 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
538 if (i) OS << ", ";
539 WriteAsOperand(OS, CallSites[i].getCalledValue());
540 }
541 }
542 OS << "\n";
543}
544
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000545void AliasSetTracker::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
547 << PointerMap.size() << " pointer values.\n";
548 for (const_iterator I = begin(), E = end(); I != E; ++I)
549 I->print(OS);
550 OS << "\n";
551}
552
David Greenee7ef6a92009-12-23 19:27:59 +0000553void AliasSet::dump() const { print(dbgs()); }
554void AliasSetTracker::dump() const { print(dbgs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555
556//===----------------------------------------------------------------------===//
Dan Gohman63b64a72009-07-30 20:21:41 +0000557// ASTCallbackVH Class Implementation
558//===----------------------------------------------------------------------===//
559
560void AliasSetTracker::ASTCallbackVH::deleted() {
561 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
562 AST->deleteValue(getValPtr());
563 // this now dangles!
564}
565
566AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohman1e5054e2009-07-31 18:21:48 +0000567 : CallbackVH(V), AST(ast) {}
568
569AliasSetTracker::ASTCallbackVH &
570AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
571 return *this = ASTCallbackVH(V, AST);
572}
Dan Gohman63b64a72009-07-30 20:21:41 +0000573
574//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575// AliasSetPrinter Pass
576//===----------------------------------------------------------------------===//
577
578namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +0000579 class AliasSetPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 AliasSetTracker *Tracker;
581 public:
582 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000583 AliasSetPrinter() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584
585 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
586 AU.setPreservesAll();
587 AU.addRequired<AliasAnalysis>();
588 }
589
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 virtual bool runOnFunction(Function &F) {
591 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
592
593 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
594 Tracker->add(&*I);
David Greene092316b2009-12-23 22:49:57 +0000595 Tracker->print(errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 delete Tracker;
597 return false;
598 }
599 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600}
Dan Gohman089efff2008-05-13 00:00:25 +0000601
602char AliasSetPrinter::ID = 0;
603static RegisterPass<AliasSetPrinter>
604X("print-alias-sets", "Alias Set Printer", false, true);