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