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