blob: 66346003739209cc82a623f140a8789d5aef2bc0 [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"
Edwin Török675d5622009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Support/InstIterator.h"
Chris Lattnera7a9daa2009-08-23 05:17:37 +000024#include "llvm/Support/Format.h"
25#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026using 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()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
157 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
158 != AliasAnalysis::NoModRef)
159 return true;
160 }
161
162 return false;
163}
164
165bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sands00b24b52007-12-01 07:51:45 +0000166 if (AA.doesNotAccessMemory(CS))
167 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
170 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
171 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
172 return true;
173
174 for (iterator I = begin(), E = end(); I != E; ++I)
175 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
176 AliasAnalysis::NoModRef)
177 return true;
178
179 return false;
180}
181
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000182void AliasSetTracker::clear() {
183 // Delete all the PointerRec entries.
Dan Gohman63b64a72009-07-30 20:21:41 +0000184 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
185 I != E; ++I)
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000186 I->second->eraseFromList();
187
188 PointerMap.clear();
189
190 // The alias sets should all be clear now.
191 AliasSets.clear();
192}
193
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
195/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
196/// instruction referring to the pointer into. If there are multiple alias sets
197/// that may alias the pointer, merge them together and return the unified set.
198///
199AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
200 unsigned Size) {
201 AliasSet *FoundSet = 0;
202 for (iterator I = begin(), E = end(); I != E; ++I)
203 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
204 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
205 FoundSet = I; // Remember it.
206 } else { // Otherwise, we must merge the sets.
207 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
208 }
209 }
210
211 return FoundSet;
212}
213
214/// containsPointer - Return true if the specified location is represented by
215/// this alias set, false otherwise. This does not modify the AST object or
216/// alias sets.
217bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
218 for (const_iterator I = begin(), E = end(); I != E; ++I)
219 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
220 return true;
221 return false;
222}
223
224
225
226AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
227 AliasSet *FoundSet = 0;
228 for (iterator I = begin(), E = end(); I != E; ++I)
229 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
230 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
231 FoundSet = I; // Remember it.
232 } else if (!I->Forward) { // Otherwise, we must merge the sets.
233 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
234 }
235 }
236
237 return FoundSet;
238}
239
240
241
242
243/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000244/// lives in.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
246 bool *New) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000247 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248
249 // Check to see if the pointer is already known...
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000250 if (Entry.hasAliasSet()) {
251 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 // Return the set!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000253 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
255 // Add it to the alias set it aliases...
256 AS->addPointer(*this, Entry, Size);
257 return *AS;
258 } else {
259 if (New) *New = true;
260 // Otherwise create a new alias set to hold the loaded pointer...
261 AliasSets.push_back(new AliasSet());
262 AliasSets.back().addPointer(*this, Entry, Size);
263 return AliasSets.back();
264 }
265}
266
267bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
268 bool NewPtr;
269 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
270 return NewPtr;
271}
272
273
274bool AliasSetTracker::add(LoadInst *LI) {
275 bool NewPtr;
276 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohman624f9612009-07-25 00:48:42 +0000277 AA.getTypeStoreSize(LI->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 AliasSet::Refs, NewPtr);
279 if (LI->isVolatile()) AS.setVolatile();
280 return NewPtr;
281}
282
283bool AliasSetTracker::add(StoreInst *SI) {
284 bool NewPtr;
285 Value *Val = SI->getOperand(0);
286 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +0000287 AA.getTypeStoreSize(Val->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 AliasSet::Mods, NewPtr);
289 if (SI->isVolatile()) AS.setVolatile();
290 return NewPtr;
291}
292
Dan Gohmanddcc8562008-04-14 18:34:50 +0000293bool AliasSetTracker::add(VAArgInst *VAAI) {
294 bool NewPtr;
Chris Lattner81f40a92008-05-22 03:23:06 +0000295 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000296 return NewPtr;
297}
298
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299
300bool AliasSetTracker::add(CallSite CS) {
Zhou Shengf74f7a82009-03-03 06:02:04 +0000301 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
302 return true; // Ignore DbgInfo Intrinsics.
Duncan Sands00b24b52007-12-01 07:51:45 +0000303 if (AA.doesNotAccessMemory(CS))
304 return true; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305
306 AliasSet *AS = findAliasSetForCallSite(CS);
307 if (!AS) {
308 AliasSets.push_back(new AliasSet());
309 AS = &AliasSets.back();
310 AS->addCallSite(CS, AA);
311 return true;
312 } else {
313 AS->addCallSite(CS, AA);
314 return false;
315 }
316}
317
318bool AliasSetTracker::add(Instruction *I) {
319 // Dispatch to one of the other add methods...
320 if (LoadInst *LI = dyn_cast<LoadInst>(I))
321 return add(LI);
322 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
323 return add(SI);
324 else if (CallInst *CI = dyn_cast<CallInst>(I))
325 return add(CI);
326 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
327 return add(II);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000328 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
329 return add(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 return true;
331}
332
333void AliasSetTracker::add(BasicBlock &BB) {
334 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
335 add(I);
336}
337
338void AliasSetTracker::add(const AliasSetTracker &AST) {
339 assert(&AA == &AST.AA &&
340 "Merging AliasSetTracker objects with different Alias Analyses!");
341
342 // Loop over all of the alias sets in AST, adding the pointers contained
343 // therein into the current alias sets. This can cause alias sets to be
344 // merged together in the current AST.
345 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
346 if (!I->Forward) { // Ignore forwarding alias sets
347 AliasSet &AS = const_cast<AliasSet&>(*I);
348
349 // If there are any call sites in the alias set, add them to this AST.
350 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
351 add(AS.CallSites[i]);
352
353 // Loop over all of the pointers in this alias set...
354 AliasSet::iterator I = AS.begin(), E = AS.end();
355 bool X;
356 for (; I != E; ++I) {
357 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
358 (AliasSet::AccessType)AS.AccessTy, X);
359 if (AS.isVolatile()) NewAS.setVolatile();
360 }
361 }
362}
363
364/// remove - Remove the specified (potentially non-empty) alias set from the
365/// tracker.
366void AliasSetTracker::remove(AliasSet &AS) {
367 // Drop all call sites.
368 AS.CallSites.clear();
369
370 // Clear the alias set.
371 unsigned NumRefs = 0;
372 while (!AS.empty()) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000373 AliasSet::PointerRec *P = AS.PtrList;
374
375 Value *ValToRemove = P->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000377 // Unlink and delete entry from the list of values.
378 P->eraseFromList();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379
380 // Remember how many references need to be dropped.
381 ++NumRefs;
382
383 // Finally, remove the entry.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000384 PointerMap.erase(ValToRemove);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 }
386
387 // Stop using the alias set, removing it.
388 AS.RefCount -= NumRefs;
389 if (AS.RefCount == 0)
390 AS.removeFromTracker(*this);
391}
392
393bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
394 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
395 if (!AS) return false;
396 remove(*AS);
397 return true;
398}
399
400bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000401 unsigned Size = AA.getTypeStoreSize(LI->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
403 if (!AS) return false;
404 remove(*AS);
405 return true;
406}
407
408bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000409 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
411 if (!AS) return false;
412 remove(*AS);
413 return true;
414}
415
Dan Gohmanddcc8562008-04-14 18:34:50 +0000416bool AliasSetTracker::remove(VAArgInst *VAAI) {
417 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
418 if (!AS) return false;
419 remove(*AS);
420 return true;
421}
422
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423bool AliasSetTracker::remove(CallSite CS) {
Duncan Sands00b24b52007-12-01 07:51:45 +0000424 if (AA.doesNotAccessMemory(CS))
425 return false; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426
427 AliasSet *AS = findAliasSetForCallSite(CS);
428 if (!AS) return false;
429 remove(*AS);
430 return true;
431}
432
433bool AliasSetTracker::remove(Instruction *I) {
434 // Dispatch to one of the other remove methods...
435 if (LoadInst *LI = dyn_cast<LoadInst>(I))
436 return remove(LI);
437 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
438 return remove(SI);
439 else if (CallInst *CI = dyn_cast<CallInst>(I))
440 return remove(CI);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000441 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
442 return remove(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 return true;
444}
445
446
447// deleteValue method - This method is used to remove a pointer value from the
448// AliasSetTracker entirely. It should be used when an instruction is deleted
449// from the program to update the AST. If you don't use this, you would have
450// dangling pointers to deleted instructions.
451//
452void AliasSetTracker::deleteValue(Value *PtrVal) {
453 // Notify the alias analysis implementation that this value is gone.
454 AA.deleteValue(PtrVal);
455
456 // If this is a call instruction, remove the callsite from the appropriate
457 // AliasSet.
458 CallSite CS = CallSite::get(PtrVal);
Duncan Sands00b24b52007-12-01 07:51:45 +0000459 if (CS.getInstruction())
460 if (!AA.doesNotAccessMemory(CS))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 if (AliasSet *AS = findAliasSetForCallSite(CS))
462 AS->removeCallSite(CS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463
464 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000465 PointerMapType::iterator I = PointerMap.find(PtrVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 if (I == PointerMap.end()) return; // Noop
467
468 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000469 AliasSet::PointerRec *PtrValEnt = I->second;
470 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000472 // Unlink and delete from the list of values.
473 PtrValEnt->eraseFromList();
474
475 // Stop using the alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 AS->dropRef(*this);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000477
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 PointerMap.erase(I);
479}
480
481// copyValue - This method should be used whenever a preexisting value in the
482// program is copied or cloned, introducing a new value. Note that it is ok for
483// clients that use this method to introduce the same value multiple times: if
484// the tracker already knows about a value, it will ignore the request.
485//
486void AliasSetTracker::copyValue(Value *From, Value *To) {
487 // Notify the alias analysis implementation that this value is copied.
488 AA.copyValue(From, To);
489
490 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000491 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000492 if (I == PointerMap.end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 return; // Noop
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000494 assert(I->second->hasAliasSet() && "Dead entry?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000496 AliasSet::PointerRec &Entry = getEntryFor(To);
497 if (Entry.hasAliasSet()) return; // Already in the tracker!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498
499 // Add it to the alias set it aliases...
Devang Patel5d11d432009-03-30 18:34:47 +0000500 I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000501 AliasSet *AS = I->second->getAliasSet(*this);
502 AS->addPointer(*this, Entry, I->second->getSize(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503}
504
505
506
507//===----------------------------------------------------------------------===//
508// AliasSet/AliasSetTracker Printing Support
509//===----------------------------------------------------------------------===//
510
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000511void AliasSet::print(raw_ostream &OS) const {
512 OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] ";
Dan Gohmancbec0762008-04-21 19:48:48 +0000513 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 switch (AccessTy) {
515 case NoModRef: OS << "No access "; break;
516 case Refs : OS << "Ref "; break;
517 case Mods : OS << "Mod "; break;
518 case ModRef : OS << "Mod/Ref "; break;
Edwin Törökbd448e32009-07-14 16:55:14 +0000519 default: llvm_unreachable("Bad value for AccessTy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 }
521 if (isVolatile()) OS << "[volatile] ";
522 if (Forward)
523 OS << " forwarding to " << (void*)Forward;
524
525
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000526 if (!empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 OS << "Pointers: ";
528 for (iterator I = begin(), E = end(); I != E; ++I) {
529 if (I != begin()) OS << ", ";
530 WriteAsOperand(OS << "(", I.getPointer());
531 OS << ", " << I.getSize() << ")";
532 }
533 }
534 if (!CallSites.empty()) {
535 OS << "\n " << CallSites.size() << " Call Sites: ";
536 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
537 if (i) OS << ", ";
538 WriteAsOperand(OS, CallSites[i].getCalledValue());
539 }
540 }
541 OS << "\n";
542}
543
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000544void AliasSetTracker::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
546 << PointerMap.size() << " pointer values.\n";
547 for (const_iterator I = begin(), E = end(); I != E; ++I)
548 I->print(OS);
549 OS << "\n";
550}
551
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000552void AliasSet::dump() const { print(errs()); }
553void AliasSetTracker::dump() const { print(errs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554
555//===----------------------------------------------------------------------===//
Dan Gohman63b64a72009-07-30 20:21:41 +0000556// ASTCallbackVH Class Implementation
557//===----------------------------------------------------------------------===//
558
559void AliasSetTracker::ASTCallbackVH::deleted() {
560 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
561 AST->deleteValue(getValPtr());
562 // this now dangles!
563}
564
565AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohman1e5054e2009-07-31 18:21:48 +0000566 : CallbackVH(V), AST(ast) {}
567
568AliasSetTracker::ASTCallbackVH &
569AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
570 return *this = ASTCallbackVH(V, AST);
571}
Dan Gohman63b64a72009-07-30 20:21:41 +0000572
573//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574// AliasSetPrinter Pass
575//===----------------------------------------------------------------------===//
576
577namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +0000578 class AliasSetPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 AliasSetTracker *Tracker;
580 public:
581 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000582 AliasSetPrinter() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583
584 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
585 AU.setPreservesAll();
586 AU.addRequired<AliasAnalysis>();
587 }
588
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 virtual bool runOnFunction(Function &F) {
590 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
591
592 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
593 Tracker->add(&*I);
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000594 Tracker->print(errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 delete Tracker;
596 return false;
597 }
598 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599}
Dan Gohman089efff2008-05-13 00:00:25 +0000600
601char AliasSetPrinter::ID = 0;
602static RegisterPass<AliasSetPrinter>
603X("print-alias-sets", "Alias Set Printer", false, true);