1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
17 #include "llvm/IntrinsicInst.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Type.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/InstIterator.h"
26 #include "llvm/Support/raw_ostream.h"
27 using namespace llvm;
28
29 /// mergeSetIn - Merge the specified alias set into this alias set.
30 ///
mergeSetIn(AliasSet & AS,AliasSetTracker & AST)31 void 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 Volatile |= AS.Volatile;
39
40 if (AliasTy == MustAlias) {
41 // Check that these two merged sets really are must aliases. Since both
42 // used to be must-alias sets, we can just check any pointer from each set
43 // for aliasing.
44 AliasAnalysis &AA = AST.getAliasAnalysis();
45 PointerRec *L = getSomePointer();
46 PointerRec *R = AS.getSomePointer();
47
48 // If the pointers are not a must-alias pair, this set becomes a may alias.
49 if (AA.alias(AliasAnalysis::Location(L->getValue(),
50 L->getSize(),
51 L->getTBAAInfo()),
52 AliasAnalysis::Location(R->getValue(),
53 R->getSize(),
54 R->getTBAAInfo()))
55 != AliasAnalysis::MustAlias)
56 AliasTy = MayAlias;
57 }
58
59 if (UnknownInsts.empty()) { // Merge call sites...
60 if (!AS.UnknownInsts.empty())
61 std::swap(UnknownInsts, AS.UnknownInsts);
62 } else if (!AS.UnknownInsts.empty()) {
63 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
64 AS.UnknownInsts.clear();
65 }
66
67 AS.Forward = this; // Forward across AS now...
68 addRef(); // AS is now pointing to us...
69
70 // Merge the list of constituent pointers...
71 if (AS.PtrList) {
72 *PtrListEnd = AS.PtrList;
73 AS.PtrList->setPrevInList(PtrListEnd);
74 PtrListEnd = AS.PtrListEnd;
75
76 AS.PtrList = 0;
77 AS.PtrListEnd = &AS.PtrList;
78 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
79 }
80 }
81
removeAliasSet(AliasSet * AS)82 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
83 if (AliasSet *Fwd = AS->Forward) {
84 Fwd->dropRef(*this);
85 AS->Forward = 0;
86 }
87 AliasSets.erase(AS);
88 }
89
removeFromTracker(AliasSetTracker & AST)90 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
91 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
92 AST.removeAliasSet(this);
93 }
94
addPointer(AliasSetTracker & AST,PointerRec & Entry,uint64_t Size,const MDNode * TBAAInfo,bool KnownMustAlias)95 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
96 uint64_t Size, const MDNode *TBAAInfo,
97 bool KnownMustAlias) {
98 assert(!Entry.hasAliasSet() && "Entry already in set!");
99
100 // Check to see if we have to downgrade to _may_ alias.
101 if (isMustAlias() && !KnownMustAlias)
102 if (PointerRec *P = getSomePointer()) {
103 AliasAnalysis &AA = AST.getAliasAnalysis();
104 AliasAnalysis::AliasResult Result =
105 AA.alias(AliasAnalysis::Location(P->getValue(), P->getSize(),
106 P->getTBAAInfo()),
107 AliasAnalysis::Location(Entry.getValue(), Size, TBAAInfo));
108 if (Result != AliasAnalysis::MustAlias)
109 AliasTy = MayAlias;
110 else // First entry of must alias must have maximum size!
111 P->updateSizeAndTBAAInfo(Size, TBAAInfo);
112 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
113 }
114
115 Entry.setAliasSet(this);
116 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
117
118 // Add it to the end of the list...
119 assert(*PtrListEnd == 0 && "End of list is not null?");
120 *PtrListEnd = &Entry;
121 PtrListEnd = Entry.setPrevInList(PtrListEnd);
122 assert(*PtrListEnd == 0 && "End of list is not null?");
123 addRef(); // Entry points to alias set.
124 }
125
addUnknownInst(Instruction * I,AliasAnalysis & AA)126 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
127 UnknownInsts.push_back(I);
128
129 if (!I->mayWriteToMemory()) {
130 AliasTy = MayAlias;
131 AccessTy |= Refs;
132 return;
133 }
134
135 // FIXME: This should use mod/ref information to make this not suck so bad
136 AliasTy = MayAlias;
137 AccessTy = ModRef;
138 }
139
140 /// aliasesPointer - Return true if the specified pointer "may" (or must)
141 /// alias one of the members in the set.
142 ///
aliasesPointer(const Value * Ptr,uint64_t Size,const MDNode * TBAAInfo,AliasAnalysis & AA) const143 bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
144 const MDNode *TBAAInfo,
145 AliasAnalysis &AA) const {
146 if (AliasTy == MustAlias) {
147 assert(UnknownInsts.empty() && "Illegal must alias set!");
148
149 // If this is a set of MustAliases, only check to see if the pointer aliases
150 // SOME value in the set.
151 PointerRec *SomePtr = getSomePointer();
152 assert(SomePtr && "Empty must-alias set??");
153 return AA.alias(AliasAnalysis::Location(SomePtr->getValue(),
154 SomePtr->getSize(),
155 SomePtr->getTBAAInfo()),
156 AliasAnalysis::Location(Ptr, Size, TBAAInfo));
157 }
158
159 // If this is a may-alias set, we have to check all of the pointers in the set
160 // to be sure it doesn't alias the set...
161 for (iterator I = begin(), E = end(); I != E; ++I)
162 if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
163 AliasAnalysis::Location(I.getPointer(), I.getSize(),
164 I.getTBAAInfo())))
165 return true;
166
167 // Check the unknown instructions...
168 if (!UnknownInsts.empty()) {
169 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
170 if (AA.getModRefInfo(UnknownInsts[i],
171 AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
172 AliasAnalysis::NoModRef)
173 return true;
174 }
175
176 return false;
177 }
178
aliasesUnknownInst(Instruction * Inst,AliasAnalysis & AA) const179 bool AliasSet::aliasesUnknownInst(Instruction *Inst, AliasAnalysis &AA) const {
180 if (!Inst->mayReadOrWriteMemory())
181 return false;
182
183 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
184 CallSite C1 = getUnknownInst(i), C2 = Inst;
185 if (!C1 || !C2 ||
186 AA.getModRefInfo(C1, C2) != AliasAnalysis::NoModRef ||
187 AA.getModRefInfo(C2, C1) != AliasAnalysis::NoModRef)
188 return true;
189 }
190
191 for (iterator I = begin(), E = end(); I != E; ++I)
192 if (AA.getModRefInfo(Inst, I.getPointer(), I.getSize()) !=
193 AliasAnalysis::NoModRef)
194 return true;
195
196 return false;
197 }
198
clear()199 void AliasSetTracker::clear() {
200 // Delete all the PointerRec entries.
201 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
202 I != E; ++I)
203 I->second->eraseFromList();
204
205 PointerMap.clear();
206
207 // The alias sets should all be clear now.
208 AliasSets.clear();
209 }
210
211
212 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
213 /// instruction referring to the pointer into. If there are multiple alias sets
214 /// that may alias the pointer, merge them together and return the unified set.
215 ///
findAliasSetForPointer(const Value * Ptr,uint64_t Size,const MDNode * TBAAInfo)216 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
217 uint64_t Size,
218 const MDNode *TBAAInfo) {
219 AliasSet *FoundSet = 0;
220 for (iterator I = begin(), E = end(); I != E; ++I) {
221 if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
222
223 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
224 FoundSet = I; // Remember it.
225 } else { // Otherwise, we must merge the sets.
226 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
227 }
228 }
229
230 return FoundSet;
231 }
232
233 /// containsPointer - Return true if the specified location is represented by
234 /// this alias set, false otherwise. This does not modify the AST object or
235 /// alias sets.
containsPointer(Value * Ptr,uint64_t Size,const MDNode * TBAAInfo) const236 bool AliasSetTracker::containsPointer(Value *Ptr, uint64_t Size,
237 const MDNode *TBAAInfo) const {
238 for (const_iterator I = begin(), E = end(); I != E; ++I)
239 if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
240 return true;
241 return false;
242 }
243
244
245
findAliasSetForUnknownInst(Instruction * Inst)246 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
247 AliasSet *FoundSet = 0;
248 for (iterator I = begin(), E = end(); I != E; ++I) {
249 if (I->Forward || !I->aliasesUnknownInst(Inst, AA))
250 continue;
251
252 if (FoundSet == 0) // If this is the first alias set ptr can go into.
253 FoundSet = I; // Remember it.
254 else if (!I->Forward) // Otherwise, we must merge the sets.
255 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
256 }
257 return FoundSet;
258 }
259
260
261
262
263 /// getAliasSetForPointer - Return the alias set that the specified pointer
264 /// lives in.
getAliasSetForPointer(Value * Pointer,uint64_t Size,const MDNode * TBAAInfo,bool * New)265 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
266 const MDNode *TBAAInfo,
267 bool *New) {
268 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
269
270 // Check to see if the pointer is already known.
271 if (Entry.hasAliasSet()) {
272 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
273 // Return the set!
274 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
275 }
276
277 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
278 // Add it to the alias set it aliases.
279 AS->addPointer(*this, Entry, Size, TBAAInfo);
280 return *AS;
281 }
282
283 if (New) *New = true;
284 // Otherwise create a new alias set to hold the loaded pointer.
285 AliasSets.push_back(new AliasSet());
286 AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
287 return AliasSets.back();
288 }
289
add(Value * Ptr,uint64_t Size,const MDNode * TBAAInfo)290 bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
291 bool NewPtr;
292 addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
293 return NewPtr;
294 }
295
296
add(LoadInst * LI)297 bool AliasSetTracker::add(LoadInst *LI) {
298 if (LI->getOrdering() > Monotonic) return addUnknown(LI);
299 AliasSet::AccessType ATy = AliasSet::Refs;
300 if (!LI->isUnordered()) ATy = AliasSet::ModRef;
301 bool NewPtr;
302 AliasSet &AS = addPointer(LI->getOperand(0),
303 AA.getTypeStoreSize(LI->getType()),
304 LI->getMetadata(LLVMContext::MD_tbaa),
305 ATy, NewPtr);
306 if (LI->isVolatile()) AS.setVolatile();
307 return NewPtr;
308 }
309
add(StoreInst * SI)310 bool AliasSetTracker::add(StoreInst *SI) {
311 if (SI->getOrdering() > Monotonic) return addUnknown(SI);
312 AliasSet::AccessType ATy = AliasSet::Mods;
313 if (!SI->isUnordered()) ATy = AliasSet::ModRef;
314 bool NewPtr;
315 Value *Val = SI->getOperand(0);
316 AliasSet &AS = addPointer(SI->getOperand(1),
317 AA.getTypeStoreSize(Val->getType()),
318 SI->getMetadata(LLVMContext::MD_tbaa),
319 ATy, NewPtr);
320 if (SI->isVolatile()) AS.setVolatile();
321 return NewPtr;
322 }
323
add(VAArgInst * VAAI)324 bool AliasSetTracker::add(VAArgInst *VAAI) {
325 bool NewPtr;
326 addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize,
327 VAAI->getMetadata(LLVMContext::MD_tbaa),
328 AliasSet::ModRef, NewPtr);
329 return NewPtr;
330 }
331
332
addUnknown(Instruction * Inst)333 bool AliasSetTracker::addUnknown(Instruction *Inst) {
334 if (isa<DbgInfoIntrinsic>(Inst))
335 return true; // Ignore DbgInfo Intrinsics.
336 if (!Inst->mayReadOrWriteMemory())
337 return true; // doesn't alias anything
338
339 AliasSet *AS = findAliasSetForUnknownInst(Inst);
340 if (AS) {
341 AS->addUnknownInst(Inst, AA);
342 return false;
343 }
344 AliasSets.push_back(new AliasSet());
345 AS = &AliasSets.back();
346 AS->addUnknownInst(Inst, AA);
347 return true;
348 }
349
add(Instruction * I)350 bool AliasSetTracker::add(Instruction *I) {
351 // Dispatch to one of the other add methods.
352 if (LoadInst *LI = dyn_cast<LoadInst>(I))
353 return add(LI);
354 if (StoreInst *SI = dyn_cast<StoreInst>(I))
355 return add(SI);
356 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
357 return add(VAAI);
358 return addUnknown(I);
359 }
360
add(BasicBlock & BB)361 void AliasSetTracker::add(BasicBlock &BB) {
362 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
363 add(I);
364 }
365
add(const AliasSetTracker & AST)366 void AliasSetTracker::add(const AliasSetTracker &AST) {
367 assert(&AA == &AST.AA &&
368 "Merging AliasSetTracker objects with different Alias Analyses!");
369
370 // Loop over all of the alias sets in AST, adding the pointers contained
371 // therein into the current alias sets. This can cause alias sets to be
372 // merged together in the current AST.
373 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
374 if (I->Forward) continue; // Ignore forwarding alias sets
375
376 AliasSet &AS = const_cast<AliasSet&>(*I);
377
378 // If there are any call sites in the alias set, add them to this AST.
379 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
380 add(AS.UnknownInsts[i]);
381
382 // Loop over all of the pointers in this alias set.
383 bool X;
384 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
385 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
386 ASI.getTBAAInfo(),
387 (AliasSet::AccessType)AS.AccessTy, X);
388 if (AS.isVolatile()) NewAS.setVolatile();
389 }
390 }
391 }
392
393 /// remove - Remove the specified (potentially non-empty) alias set from the
394 /// tracker.
remove(AliasSet & AS)395 void AliasSetTracker::remove(AliasSet &AS) {
396 // Drop all call sites.
397 AS.UnknownInsts.clear();
398
399 // Clear the alias set.
400 unsigned NumRefs = 0;
401 while (!AS.empty()) {
402 AliasSet::PointerRec *P = AS.PtrList;
403
404 Value *ValToRemove = P->getValue();
405
406 // Unlink and delete entry from the list of values.
407 P->eraseFromList();
408
409 // Remember how many references need to be dropped.
410 ++NumRefs;
411
412 // Finally, remove the entry.
413 PointerMap.erase(ValToRemove);
414 }
415
416 // Stop using the alias set, removing it.
417 AS.RefCount -= NumRefs;
418 if (AS.RefCount == 0)
419 AS.removeFromTracker(*this);
420 }
421
422 bool
remove(Value * Ptr,uint64_t Size,const MDNode * TBAAInfo)423 AliasSetTracker::remove(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
424 AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
425 if (!AS) return false;
426 remove(*AS);
427 return true;
428 }
429
remove(LoadInst * LI)430 bool AliasSetTracker::remove(LoadInst *LI) {
431 uint64_t Size = AA.getTypeStoreSize(LI->getType());
432 const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
433 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
434 if (!AS) return false;
435 remove(*AS);
436 return true;
437 }
438
remove(StoreInst * SI)439 bool AliasSetTracker::remove(StoreInst *SI) {
440 uint64_t Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
441 const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
442 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
443 if (!AS) return false;
444 remove(*AS);
445 return true;
446 }
447
remove(VAArgInst * VAAI)448 bool AliasSetTracker::remove(VAArgInst *VAAI) {
449 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
450 AliasAnalysis::UnknownSize,
451 VAAI->getMetadata(LLVMContext::MD_tbaa));
452 if (!AS) return false;
453 remove(*AS);
454 return true;
455 }
456
removeUnknown(Instruction * I)457 bool AliasSetTracker::removeUnknown(Instruction *I) {
458 if (!I->mayReadOrWriteMemory())
459 return false; // doesn't alias anything
460
461 AliasSet *AS = findAliasSetForUnknownInst(I);
462 if (!AS) return false;
463 remove(*AS);
464 return true;
465 }
466
remove(Instruction * I)467 bool AliasSetTracker::remove(Instruction *I) {
468 // Dispatch to one of the other remove methods...
469 if (LoadInst *LI = dyn_cast<LoadInst>(I))
470 return remove(LI);
471 if (StoreInst *SI = dyn_cast<StoreInst>(I))
472 return remove(SI);
473 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
474 return remove(VAAI);
475 return removeUnknown(I);
476 }
477
478
479 // deleteValue method - This method is used to remove a pointer value from the
480 // AliasSetTracker entirely. It should be used when an instruction is deleted
481 // from the program to update the AST. If you don't use this, you would have
482 // dangling pointers to deleted instructions.
483 //
deleteValue(Value * PtrVal)484 void AliasSetTracker::deleteValue(Value *PtrVal) {
485 // Notify the alias analysis implementation that this value is gone.
486 AA.deleteValue(PtrVal);
487
488 // If this is a call instruction, remove the callsite from the appropriate
489 // AliasSet (if present).
490 if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) {
491 if (Inst->mayReadOrWriteMemory()) {
492 // Scan all the alias sets to see if this call site is contained.
493 for (iterator I = begin(), E = end(); I != E; ++I) {
494 if (I->Forward) continue;
495
496 I->removeUnknownInst(Inst);
497 }
498 }
499 }
500
501 // First, look up the PointerRec for this pointer.
502 PointerMapType::iterator I = PointerMap.find(PtrVal);
503 if (I == PointerMap.end()) return; // Noop
504
505 // If we found one, remove the pointer from the alias set it is in.
506 AliasSet::PointerRec *PtrValEnt = I->second;
507 AliasSet *AS = PtrValEnt->getAliasSet(*this);
508
509 // Unlink and delete from the list of values.
510 PtrValEnt->eraseFromList();
511
512 // Stop using the alias set.
513 AS->dropRef(*this);
514
515 PointerMap.erase(I);
516 }
517
518 // copyValue - This method should be used whenever a preexisting value in the
519 // program is copied or cloned, introducing a new value. Note that it is ok for
520 // clients that use this method to introduce the same value multiple times: if
521 // the tracker already knows about a value, it will ignore the request.
522 //
copyValue(Value * From,Value * To)523 void AliasSetTracker::copyValue(Value *From, Value *To) {
524 // Notify the alias analysis implementation that this value is copied.
525 AA.copyValue(From, To);
526
527 // First, look up the PointerRec for this pointer.
528 PointerMapType::iterator I = PointerMap.find(From);
529 if (I == PointerMap.end())
530 return; // Noop
531 assert(I->second->hasAliasSet() && "Dead entry?");
532
533 AliasSet::PointerRec &Entry = getEntryFor(To);
534 if (Entry.hasAliasSet()) return; // Already in the tracker!
535
536 // Add it to the alias set it aliases...
537 I = PointerMap.find(From);
538 AliasSet *AS = I->second->getAliasSet(*this);
539 AS->addPointer(*this, Entry, I->second->getSize(),
540 I->second->getTBAAInfo(),
541 true);
542 }
543
544
545
546 //===----------------------------------------------------------------------===//
547 // AliasSet/AliasSetTracker Printing Support
548 //===----------------------------------------------------------------------===//
549
print(raw_ostream & OS) const550 void AliasSet::print(raw_ostream &OS) const {
551 OS << " AliasSet[" << (void*)this << ", " << RefCount << "] ";
552 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
553 switch (AccessTy) {
554 case NoModRef: OS << "No access "; break;
555 case Refs : OS << "Ref "; break;
556 case Mods : OS << "Mod "; break;
557 case ModRef : OS << "Mod/Ref "; break;
558 default: llvm_unreachable("Bad value for AccessTy!");
559 }
560 if (isVolatile()) OS << "[volatile] ";
561 if (Forward)
562 OS << " forwarding to " << (void*)Forward;
563
564
565 if (!empty()) {
566 OS << "Pointers: ";
567 for (iterator I = begin(), E = end(); I != E; ++I) {
568 if (I != begin()) OS << ", ";
569 WriteAsOperand(OS << "(", I.getPointer());
570 OS << ", " << I.getSize() << ")";
571 }
572 }
573 if (!UnknownInsts.empty()) {
574 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
575 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
576 if (i) OS << ", ";
577 WriteAsOperand(OS, UnknownInsts[i]);
578 }
579 }
580 OS << "\n";
581 }
582
print(raw_ostream & OS) const583 void AliasSetTracker::print(raw_ostream &OS) const {
584 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
585 << PointerMap.size() << " pointer values.\n";
586 for (const_iterator I = begin(), E = end(); I != E; ++I)
587 I->print(OS);
588 OS << "\n";
589 }
590
dump() const591 void AliasSet::dump() const { print(dbgs()); }
dump() const592 void AliasSetTracker::dump() const { print(dbgs()); }
593
594 //===----------------------------------------------------------------------===//
595 // ASTCallbackVH Class Implementation
596 //===----------------------------------------------------------------------===//
597
deleted()598 void AliasSetTracker::ASTCallbackVH::deleted() {
599 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
600 AST->deleteValue(getValPtr());
601 // this now dangles!
602 }
603
allUsesReplacedWith(Value * V)604 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
605 AST->copyValue(getValPtr(), V);
606 }
607
ASTCallbackVH(Value * V,AliasSetTracker * ast)608 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
609 : CallbackVH(V), AST(ast) {}
610
611 AliasSetTracker::ASTCallbackVH &
operator =(Value * V)612 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
613 return *this = ASTCallbackVH(V, AST);
614 }
615
616 //===----------------------------------------------------------------------===//
617 // AliasSetPrinter Pass
618 //===----------------------------------------------------------------------===//
619
620 namespace {
621 class AliasSetPrinter : public FunctionPass {
622 AliasSetTracker *Tracker;
623 public:
624 static char ID; // Pass identification, replacement for typeid
AliasSetPrinter()625 AliasSetPrinter() : FunctionPass(ID) {
626 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
627 }
628
getAnalysisUsage(AnalysisUsage & AU) const629 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
630 AU.setPreservesAll();
631 AU.addRequired<AliasAnalysis>();
632 }
633
runOnFunction(Function & F)634 virtual bool runOnFunction(Function &F) {
635 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
636
637 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
638 Tracker->add(&*I);
639 Tracker->print(errs());
640 delete Tracker;
641 return false;
642 }
643 };
644 }
645
646 char AliasSetPrinter::ID = 0;
647 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
648 "Alias Set Printer", false, true)
649 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
650 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
651 "Alias Set Printer", false, true)
652