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