blob: ba2d95b9b071567a8abf8cc6e3e08219c834ae26 [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"
Victor Hernandez43a4c672009-10-26 23:58:56 +000016#include "llvm/Analysis/MallocFreeHelper.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Instructions.h"
Zhou Shengf74f7a82009-03-03 06:02:04 +000018#include "llvm/IntrinsicInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Pass.h"
20#include "llvm/Type.h"
21#include "llvm/Target/TargetData.h"
22#include "llvm/Assembly/Writer.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()) {
157 if (AA.hasNoModRefInfoForCalls())
158 return true;
159
160 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
161 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
162 != AliasAnalysis::NoModRef)
163 return true;
164 }
165
166 return false;
167}
168
169bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sands00b24b52007-12-01 07:51:45 +0000170 if (AA.doesNotAccessMemory(CS))
171 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172
173 if (AA.hasNoModRefInfoForCalls())
174 return true;
175
176 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
177 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
178 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
179 return true;
180
181 for (iterator I = begin(), E = end(); I != E; ++I)
182 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
183 AliasAnalysis::NoModRef)
184 return true;
185
186 return false;
187}
188
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000189void AliasSetTracker::clear() {
190 // Delete all the PointerRec entries.
Dan Gohman63b64a72009-07-30 20:21:41 +0000191 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
192 I != E; ++I)
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000193 I->second->eraseFromList();
194
195 PointerMap.clear();
196
197 // The alias sets should all be clear now.
198 AliasSets.clear();
199}
200
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201
202/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
203/// instruction referring to the pointer into. If there are multiple alias sets
204/// that may alias the pointer, merge them together and return the unified set.
205///
206AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
207 unsigned Size) {
208 AliasSet *FoundSet = 0;
209 for (iterator I = begin(), E = end(); I != E; ++I)
210 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
211 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
212 FoundSet = I; // Remember it.
213 } else { // Otherwise, we must merge the sets.
214 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
215 }
216 }
217
218 return FoundSet;
219}
220
221/// containsPointer - Return true if the specified location is represented by
222/// this alias set, false otherwise. This does not modify the AST object or
223/// alias sets.
224bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
225 for (const_iterator I = begin(), E = end(); I != E; ++I)
226 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
227 return true;
228 return false;
229}
230
231
232
233AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
234 AliasSet *FoundSet = 0;
235 for (iterator I = begin(), E = end(); I != E; ++I)
236 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
237 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
238 FoundSet = I; // Remember it.
239 } else if (!I->Forward) { // Otherwise, we must merge the sets.
240 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
241 }
242 }
243
244 return FoundSet;
245}
246
247
248
249
250/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000251/// lives in.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
253 bool *New) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000254 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255
256 // Check to see if the pointer is already known...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000257 if (Entry.hasAliasSet()) {
258 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 // Return the set!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000260 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
262 // Add it to the alias set it aliases...
263 AS->addPointer(*this, Entry, Size);
264 return *AS;
265 } else {
266 if (New) *New = true;
267 // Otherwise create a new alias set to hold the loaded pointer...
268 AliasSets.push_back(new AliasSet());
269 AliasSets.back().addPointer(*this, Entry, Size);
270 return AliasSets.back();
271 }
272}
273
274bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
275 bool NewPtr;
276 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
277 return NewPtr;
278}
279
280
281bool AliasSetTracker::add(LoadInst *LI) {
282 bool NewPtr;
283 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohman624f9612009-07-25 00:48:42 +0000284 AA.getTypeStoreSize(LI->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 AliasSet::Refs, NewPtr);
286 if (LI->isVolatile()) AS.setVolatile();
287 return NewPtr;
288}
289
290bool AliasSetTracker::add(StoreInst *SI) {
291 bool NewPtr;
292 Value *Val = SI->getOperand(0);
293 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +0000294 AA.getTypeStoreSize(Val->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 AliasSet::Mods, NewPtr);
296 if (SI->isVolatile()) AS.setVolatile();
297 return NewPtr;
298}
299
Dan Gohmanddcc8562008-04-14 18:34:50 +0000300bool AliasSetTracker::add(VAArgInst *VAAI) {
301 bool NewPtr;
Chris Lattner81f40a92008-05-22 03:23:06 +0000302 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000303 return NewPtr;
304}
305
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
307bool AliasSetTracker::add(CallSite CS) {
Victor Hernandezf9a7a332009-10-26 23:43:48 +0000308 Instruction* Inst = CS.getInstruction();
309 if (isFreeCall(Inst)) {
310 bool NewPtr;
311 addPointer(Inst->getOperand(1), ~0, AliasSet::Mods, NewPtr);
312 return NewPtr;
313 }
314
Zhou Shengf74f7a82009-03-03 06:02:04 +0000315 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
316 return true; // Ignore DbgInfo Intrinsics.
Duncan Sands00b24b52007-12-01 07:51:45 +0000317 if (AA.doesNotAccessMemory(CS))
318 return true; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319
320 AliasSet *AS = findAliasSetForCallSite(CS);
321 if (!AS) {
322 AliasSets.push_back(new AliasSet());
323 AS = &AliasSets.back();
324 AS->addCallSite(CS, AA);
325 return true;
326 } else {
327 AS->addCallSite(CS, AA);
328 return false;
329 }
330}
331
332bool AliasSetTracker::add(Instruction *I) {
333 // Dispatch to one of the other add methods...
334 if (LoadInst *LI = dyn_cast<LoadInst>(I))
335 return add(LI);
336 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
337 return add(SI);
338 else if (CallInst *CI = dyn_cast<CallInst>(I))
339 return add(CI);
340 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
341 return add(II);
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
Dan Gohmanddcc8562008-04-14 18:34:50 +0000430bool AliasSetTracker::remove(VAArgInst *VAAI) {
431 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
432 if (!AS) return false;
433 remove(*AS);
434 return true;
435}
436
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437bool AliasSetTracker::remove(CallSite CS) {
Victor Hernandezf9a7a332009-10-26 23:43:48 +0000438 Instruction* Inst = CS.getInstruction();
439 if (isFreeCall(Inst)) {
440 AliasSet *AS = findAliasSetForPointer(Inst->getOperand(1), ~0);
441 if (!AS) return false;
442 remove(*AS);
443 return true;
444 }
445
Duncan Sands00b24b52007-12-01 07:51:45 +0000446 if (AA.doesNotAccessMemory(CS))
447 return false; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448
449 AliasSet *AS = findAliasSetForCallSite(CS);
450 if (!AS) return false;
451 remove(*AS);
452 return true;
453}
454
455bool AliasSetTracker::remove(Instruction *I) {
456 // Dispatch to one of the other remove methods...
457 if (LoadInst *LI = dyn_cast<LoadInst>(I))
458 return remove(LI);
459 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
460 return remove(SI);
461 else if (CallInst *CI = dyn_cast<CallInst>(I))
462 return remove(CI);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000463 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
464 return remove(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 return true;
466}
467
468
469// deleteValue method - This method is used to remove a pointer value from the
470// AliasSetTracker entirely. It should be used when an instruction is deleted
471// from the program to update the AST. If you don't use this, you would have
472// dangling pointers to deleted instructions.
473//
474void AliasSetTracker::deleteValue(Value *PtrVal) {
475 // Notify the alias analysis implementation that this value is gone.
476 AA.deleteValue(PtrVal);
477
478 // If this is a call instruction, remove the callsite from the appropriate
479 // AliasSet.
480 CallSite CS = CallSite::get(PtrVal);
Duncan Sands00b24b52007-12-01 07:51:45 +0000481 if (CS.getInstruction())
482 if (!AA.doesNotAccessMemory(CS))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 if (AliasSet *AS = findAliasSetForCallSite(CS))
484 AS->removeCallSite(CS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485
486 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000487 PointerMapType::iterator I = PointerMap.find(PtrVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 if (I == PointerMap.end()) return; // Noop
489
490 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000491 AliasSet::PointerRec *PtrValEnt = I->second;
492 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000494 // Unlink and delete from the list of values.
495 PtrValEnt->eraseFromList();
496
497 // Stop using the alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 AS->dropRef(*this);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000499
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 PointerMap.erase(I);
501}
502
503// copyValue - This method should be used whenever a preexisting value in the
504// program is copied or cloned, introducing a new value. Note that it is ok for
505// clients that use this method to introduce the same value multiple times: if
506// the tracker already knows about a value, it will ignore the request.
507//
508void AliasSetTracker::copyValue(Value *From, Value *To) {
509 // Notify the alias analysis implementation that this value is copied.
510 AA.copyValue(From, To);
511
512 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000513 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000514 if (I == PointerMap.end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 return; // Noop
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000516 assert(I->second->hasAliasSet() && "Dead entry?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000518 AliasSet::PointerRec &Entry = getEntryFor(To);
519 if (Entry.hasAliasSet()) return; // Already in the tracker!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520
521 // Add it to the alias set it aliases...
Devang Patel5d11d432009-03-30 18:34:47 +0000522 I = PointerMap.find(From);
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
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000533void AliasSet::print(raw_ostream &OS) const {
534 OS << " AliasSet[" << format("0x%p", (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;
Edwin Törökbd448e32009-07-14 16:55:14 +0000541 default: llvm_unreachable("Bad value for AccessTy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 }
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
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000566void AliasSetTracker::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 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
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000574void AliasSet::dump() const { print(errs()); }
575void AliasSetTracker::dump() const { print(errs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576
577//===----------------------------------------------------------------------===//
Dan Gohman63b64a72009-07-30 20:21:41 +0000578// ASTCallbackVH Class Implementation
579//===----------------------------------------------------------------------===//
580
581void AliasSetTracker::ASTCallbackVH::deleted() {
582 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
583 AST->deleteValue(getValPtr());
584 // this now dangles!
585}
586
587AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohman1e5054e2009-07-31 18:21:48 +0000588 : CallbackVH(V), AST(ast) {}
589
590AliasSetTracker::ASTCallbackVH &
591AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
592 return *this = ASTCallbackVH(V, AST);
593}
Dan Gohman63b64a72009-07-30 20:21:41 +0000594
595//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596// AliasSetPrinter Pass
597//===----------------------------------------------------------------------===//
598
599namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +0000600 class AliasSetPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 AliasSetTracker *Tracker;
602 public:
603 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000604 AliasSetPrinter() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605
606 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
607 AU.setPreservesAll();
608 AU.addRequired<AliasAnalysis>();
609 }
610
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 virtual bool runOnFunction(Function &F) {
612 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
613
614 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
615 Tracker->add(&*I);
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000616 Tracker->print(errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 delete Tracker;
618 return false;
619 }
620 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621}
Dan Gohman089efff2008-05-13 00:00:25 +0000622
623char AliasSetPrinter::ID = 0;
624static RegisterPass<AliasSetPrinter>
625X("print-alias-sets", "Alias Set Printer", false, true);