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