LLVM API Documentation

AliasSetTracker.cpp
Go to the documentation of this file.
00001 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the AliasSetTracker and AliasSet classes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Analysis/AliasSetTracker.h"
00015 #include "llvm/Analysis/AliasAnalysis.h"
00016 #include "llvm/IR/DataLayout.h"
00017 #include "llvm/IR/InstIterator.h"
00018 #include "llvm/IR/Instructions.h"
00019 #include "llvm/IR/IntrinsicInst.h"
00020 #include "llvm/IR/LLVMContext.h"
00021 #include "llvm/IR/Type.h"
00022 #include "llvm/Pass.h"
00023 #include "llvm/Support/Debug.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 #include "llvm/Support/raw_ostream.h"
00026 using namespace llvm;
00027 
00028 /// mergeSetIn - Merge the specified alias set into this alias set.
00029 ///
00030 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
00031   assert(!AS.Forward && "Alias set is already forwarding!");
00032   assert(!Forward && "This set is a forwarding set!!");
00033 
00034   // Update the alias and access types of this set...
00035   AccessTy |= AS.AccessTy;
00036   AliasTy  |= AS.AliasTy;
00037   Volatile |= AS.Volatile;
00038 
00039   if (AliasTy == MustAlias) {
00040     // Check that these two merged sets really are must aliases.  Since both
00041     // used to be must-alias sets, we can just check any pointer from each set
00042     // for aliasing.
00043     AliasAnalysis &AA = AST.getAliasAnalysis();
00044     PointerRec *L = getSomePointer();
00045     PointerRec *R = AS.getSomePointer();
00046 
00047     // If the pointers are not a must-alias pair, this set becomes a may alias.
00048     if (AA.alias(AliasAnalysis::Location(L->getValue(),
00049                                          L->getSize(),
00050                                          L->getTBAAInfo()),
00051                  AliasAnalysis::Location(R->getValue(),
00052                                          R->getSize(),
00053                                          R->getTBAAInfo()))
00054         != AliasAnalysis::MustAlias)
00055       AliasTy = MayAlias;
00056   }
00057 
00058   if (UnknownInsts.empty()) {            // Merge call sites...
00059     if (!AS.UnknownInsts.empty())
00060       std::swap(UnknownInsts, AS.UnknownInsts);
00061   } else if (!AS.UnknownInsts.empty()) {
00062     UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
00063     AS.UnknownInsts.clear();
00064   }
00065 
00066   AS.Forward = this;  // Forward across AS now...
00067   addRef();           // AS is now pointing to us...
00068 
00069   // Merge the list of constituent pointers...
00070   if (AS.PtrList) {
00071     *PtrListEnd = AS.PtrList;
00072     AS.PtrList->setPrevInList(PtrListEnd);
00073     PtrListEnd = AS.PtrListEnd;
00074 
00075     AS.PtrList = nullptr;
00076     AS.PtrListEnd = &AS.PtrList;
00077     assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
00078   }
00079 }
00080 
00081 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
00082   if (AliasSet *Fwd = AS->Forward) {
00083     Fwd->dropRef(*this);
00084     AS->Forward = nullptr;
00085   }
00086   AliasSets.erase(AS);
00087 }
00088 
00089 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
00090   assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
00091   AST.removeAliasSet(this);
00092 }
00093 
00094 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
00095                           uint64_t Size, const MDNode *TBAAInfo,
00096                           bool KnownMustAlias) {
00097   assert(!Entry.hasAliasSet() && "Entry already in set!");
00098 
00099   // Check to see if we have to downgrade to _may_ alias.
00100   if (isMustAlias() && !KnownMustAlias)
00101     if (PointerRec *P = getSomePointer()) {
00102       AliasAnalysis &AA = AST.getAliasAnalysis();
00103       AliasAnalysis::AliasResult Result =
00104         AA.alias(AliasAnalysis::Location(P->getValue(), P->getSize(),
00105                                          P->getTBAAInfo()),
00106                  AliasAnalysis::Location(Entry.getValue(), Size, TBAAInfo));
00107       if (Result != AliasAnalysis::MustAlias)
00108         AliasTy = MayAlias;
00109       else                  // First entry of must alias must have maximum size!
00110         P->updateSizeAndTBAAInfo(Size, TBAAInfo);
00111       assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
00112     }
00113 
00114   Entry.setAliasSet(this);
00115   Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
00116 
00117   // Add it to the end of the list...
00118   assert(*PtrListEnd == nullptr && "End of list is not null?");
00119   *PtrListEnd = &Entry;
00120   PtrListEnd = Entry.setPrevInList(PtrListEnd);
00121   assert(*PtrListEnd == nullptr && "End of list is not null?");
00122   addRef();               // Entry points to alias set.
00123 }
00124 
00125 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
00126   UnknownInsts.push_back(I);
00127 
00128   if (!I->mayWriteToMemory()) {
00129     AliasTy = MayAlias;
00130     AccessTy |= Refs;
00131     return;
00132   }
00133 
00134   // FIXME: This should use mod/ref information to make this not suck so bad
00135   AliasTy = MayAlias;
00136   AccessTy = ModRef;
00137 }
00138 
00139 /// aliasesPointer - Return true if the specified pointer "may" (or must)
00140 /// alias one of the members in the set.
00141 ///
00142 bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
00143                               const MDNode *TBAAInfo,
00144                               AliasAnalysis &AA) const {
00145   if (AliasTy == MustAlias) {
00146     assert(UnknownInsts.empty() && "Illegal must alias set!");
00147 
00148     // If this is a set of MustAliases, only check to see if the pointer aliases
00149     // SOME value in the set.
00150     PointerRec *SomePtr = getSomePointer();
00151     assert(SomePtr && "Empty must-alias set??");
00152     return AA.alias(AliasAnalysis::Location(SomePtr->getValue(),
00153                                             SomePtr->getSize(),
00154                                             SomePtr->getTBAAInfo()),
00155                     AliasAnalysis::Location(Ptr, Size, TBAAInfo));
00156   }
00157 
00158   // If this is a may-alias set, we have to check all of the pointers in the set
00159   // to be sure it doesn't alias the set...
00160   for (iterator I = begin(), E = end(); I != E; ++I)
00161     if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
00162                  AliasAnalysis::Location(I.getPointer(), I.getSize(),
00163                                          I.getTBAAInfo())))
00164       return true;
00165 
00166   // Check the unknown instructions...
00167   if (!UnknownInsts.empty()) {
00168     for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
00169       if (AA.getModRefInfo(UnknownInsts[i],
00170                            AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
00171             AliasAnalysis::NoModRef)
00172         return true;
00173   }
00174 
00175   return false;
00176 }
00177 
00178 bool AliasSet::aliasesUnknownInst(Instruction *Inst, AliasAnalysis &AA) const {
00179   if (!Inst->mayReadOrWriteMemory())
00180     return false;
00181 
00182   for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
00183     CallSite C1 = getUnknownInst(i), C2 = Inst;
00184     if (!C1 || !C2 ||
00185         AA.getModRefInfo(C1, C2) != AliasAnalysis::NoModRef ||
00186         AA.getModRefInfo(C2, C1) != AliasAnalysis::NoModRef)
00187       return true;
00188   }
00189 
00190   for (iterator I = begin(), E = end(); I != E; ++I)
00191     if (AA.getModRefInfo(Inst, AliasAnalysis::Location(I.getPointer(),
00192                                                        I.getSize(),
00193                                                        I.getTBAAInfo())) !=
00194            AliasAnalysis::NoModRef)
00195       return true;
00196 
00197   return false;
00198 }
00199 
00200 void AliasSetTracker::clear() {
00201   // Delete all the PointerRec entries.
00202   for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
00203        I != E; ++I)
00204     I->second->eraseFromList();
00205   
00206   PointerMap.clear();
00207   
00208   // The alias sets should all be clear now.
00209   AliasSets.clear();
00210 }
00211 
00212 
00213 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
00214 /// instruction referring to the pointer into.  If there are multiple alias sets
00215 /// that may alias the pointer, merge them together and return the unified set.
00216 ///
00217 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
00218                                                   uint64_t Size,
00219                                                   const MDNode *TBAAInfo) {
00220   AliasSet *FoundSet = nullptr;
00221   for (iterator I = begin(), E = end(); I != E; ++I) {
00222     if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
00223     
00224     if (!FoundSet) {      // If this is the first alias set ptr can go into.
00225       FoundSet = I;       // Remember it.
00226     } else {              // Otherwise, we must merge the sets.
00227       FoundSet->mergeSetIn(*I, *this);     // Merge in contents.
00228     }
00229   }
00230 
00231   return FoundSet;
00232 }
00233 
00234 /// containsPointer - Return true if the specified location is represented by
00235 /// this alias set, false otherwise.  This does not modify the AST object or
00236 /// alias sets.
00237 bool AliasSetTracker::containsPointer(Value *Ptr, uint64_t Size,
00238                                       const MDNode *TBAAInfo) const {
00239   for (const_iterator I = begin(), E = end(); I != E; ++I)
00240     if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
00241       return true;
00242   return false;
00243 }
00244 
00245 
00246 
00247 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
00248   AliasSet *FoundSet = nullptr;
00249   for (iterator I = begin(), E = end(); I != E; ++I) {
00250     if (I->Forward || !I->aliasesUnknownInst(Inst, AA))
00251       continue;
00252     
00253     if (!FoundSet)            // If this is the first alias set ptr can go into.
00254       FoundSet = I;           // Remember it.
00255     else if (!I->Forward)     // Otherwise, we must merge the sets.
00256       FoundSet->mergeSetIn(*I, *this);     // Merge in contents.
00257   }
00258   return FoundSet;
00259 }
00260 
00261 
00262 
00263 
00264 /// getAliasSetForPointer - Return the alias set that the specified pointer
00265 /// lives in.
00266 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
00267                                                  const MDNode *TBAAInfo,
00268                                                  bool *New) {
00269   AliasSet::PointerRec &Entry = getEntryFor(Pointer);
00270 
00271   // Check to see if the pointer is already known.
00272   if (Entry.hasAliasSet()) {
00273     Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
00274     // Return the set!
00275     return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
00276   }
00277   
00278   if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
00279     // Add it to the alias set it aliases.
00280     AS->addPointer(*this, Entry, Size, TBAAInfo);
00281     return *AS;
00282   }
00283   
00284   if (New) *New = true;
00285   // Otherwise create a new alias set to hold the loaded pointer.
00286   AliasSets.push_back(new AliasSet());
00287   AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
00288   return AliasSets.back();
00289 }
00290 
00291 bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
00292   bool NewPtr;
00293   addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
00294   return NewPtr;
00295 }
00296 
00297 
00298 bool AliasSetTracker::add(LoadInst *LI) {
00299   if (LI->getOrdering() > Monotonic) return addUnknown(LI);
00300   AliasSet::AccessType ATy = AliasSet::Refs;
00301   bool NewPtr;
00302   AliasSet &AS = addPointer(LI->getOperand(0),
00303                             AA.getTypeStoreSize(LI->getType()),
00304                             LI->getMetadata(LLVMContext::MD_tbaa),
00305                             ATy, NewPtr);
00306   if (LI->isVolatile()) AS.setVolatile();
00307   return NewPtr;
00308 }
00309 
00310 bool AliasSetTracker::add(StoreInst *SI) {
00311   if (SI->getOrdering() > Monotonic) return addUnknown(SI);
00312   AliasSet::AccessType ATy = AliasSet::Mods;
00313   bool NewPtr;
00314   Value *Val = SI->getOperand(0);
00315   AliasSet &AS = addPointer(SI->getOperand(1),
00316                             AA.getTypeStoreSize(Val->getType()),
00317                             SI->getMetadata(LLVMContext::MD_tbaa),
00318                             ATy, NewPtr);
00319   if (SI->isVolatile()) AS.setVolatile();
00320   return NewPtr;
00321 }
00322 
00323 bool AliasSetTracker::add(VAArgInst *VAAI) {
00324   bool NewPtr;
00325   addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize, 
00326              VAAI->getMetadata(LLVMContext::MD_tbaa),
00327              AliasSet::ModRef, NewPtr);
00328   return NewPtr;
00329 }
00330 
00331 
00332 bool AliasSetTracker::addUnknown(Instruction *Inst) {
00333   if (isa<DbgInfoIntrinsic>(Inst)) 
00334     return true; // Ignore DbgInfo Intrinsics.
00335   if (!Inst->mayReadOrWriteMemory())
00336     return true; // doesn't alias anything
00337 
00338   AliasSet *AS = findAliasSetForUnknownInst(Inst);
00339   if (AS) {
00340     AS->addUnknownInst(Inst, AA);
00341     return false;
00342   }
00343   AliasSets.push_back(new AliasSet());
00344   AS = &AliasSets.back();
00345   AS->addUnknownInst(Inst, AA);
00346   return true;
00347 }
00348 
00349 bool AliasSetTracker::add(Instruction *I) {
00350   // Dispatch to one of the other add methods.
00351   if (LoadInst *LI = dyn_cast<LoadInst>(I))
00352     return add(LI);
00353   if (StoreInst *SI = dyn_cast<StoreInst>(I))
00354     return add(SI);
00355   if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
00356     return add(VAAI);
00357   return addUnknown(I);
00358 }
00359 
00360 void AliasSetTracker::add(BasicBlock &BB) {
00361   for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
00362     add(I);
00363 }
00364 
00365 void AliasSetTracker::add(const AliasSetTracker &AST) {
00366   assert(&AA == &AST.AA &&
00367          "Merging AliasSetTracker objects with different Alias Analyses!");
00368 
00369   // Loop over all of the alias sets in AST, adding the pointers contained
00370   // therein into the current alias sets.  This can cause alias sets to be
00371   // merged together in the current AST.
00372   for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
00373     if (I->Forward) continue;   // Ignore forwarding alias sets
00374     
00375     AliasSet &AS = const_cast<AliasSet&>(*I);
00376 
00377     // If there are any call sites in the alias set, add them to this AST.
00378     for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
00379       add(AS.UnknownInsts[i]);
00380 
00381     // Loop over all of the pointers in this alias set.
00382     bool X;
00383     for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
00384       AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
00385                                    ASI.getTBAAInfo(),
00386                                    (AliasSet::AccessType)AS.AccessTy, X);
00387       if (AS.isVolatile()) NewAS.setVolatile();
00388     }
00389   }
00390 }
00391 
00392 /// remove - Remove the specified (potentially non-empty) alias set from the
00393 /// tracker.
00394 void AliasSetTracker::remove(AliasSet &AS) {
00395   // Drop all call sites.
00396   AS.UnknownInsts.clear();
00397   
00398   // Clear the alias set.
00399   unsigned NumRefs = 0;
00400   while (!AS.empty()) {
00401     AliasSet::PointerRec *P = AS.PtrList;
00402 
00403     Value *ValToRemove = P->getValue();
00404     
00405     // Unlink and delete entry from the list of values.
00406     P->eraseFromList();
00407     
00408     // Remember how many references need to be dropped.
00409     ++NumRefs;
00410 
00411     // Finally, remove the entry.
00412     PointerMap.erase(ValToRemove);
00413   }
00414   
00415   // Stop using the alias set, removing it.
00416   AS.RefCount -= NumRefs;
00417   if (AS.RefCount == 0)
00418     AS.removeFromTracker(*this);
00419 }
00420 
00421 bool
00422 AliasSetTracker::remove(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
00423   AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
00424   if (!AS) return false;
00425   remove(*AS);
00426   return true;
00427 }
00428 
00429 bool AliasSetTracker::remove(LoadInst *LI) {
00430   uint64_t Size = AA.getTypeStoreSize(LI->getType());
00431   const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
00432   AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
00433   if (!AS) return false;
00434   remove(*AS);
00435   return true;
00436 }
00437 
00438 bool AliasSetTracker::remove(StoreInst *SI) {
00439   uint64_t Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
00440   const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
00441   AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
00442   if (!AS) return false;
00443   remove(*AS);
00444   return true;
00445 }
00446 
00447 bool AliasSetTracker::remove(VAArgInst *VAAI) {
00448   AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
00449                                         AliasAnalysis::UnknownSize,
00450                                         VAAI->getMetadata(LLVMContext::MD_tbaa));
00451   if (!AS) return false;
00452   remove(*AS);
00453   return true;
00454 }
00455 
00456 bool AliasSetTracker::removeUnknown(Instruction *I) {
00457   if (!I->mayReadOrWriteMemory())
00458     return false; // doesn't alias anything
00459 
00460   AliasSet *AS = findAliasSetForUnknownInst(I);
00461   if (!AS) return false;
00462   remove(*AS);
00463   return true;
00464 }
00465 
00466 bool AliasSetTracker::remove(Instruction *I) {
00467   // Dispatch to one of the other remove methods...
00468   if (LoadInst *LI = dyn_cast<LoadInst>(I))
00469     return remove(LI);
00470   if (StoreInst *SI = dyn_cast<StoreInst>(I))
00471     return remove(SI);
00472   if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
00473     return remove(VAAI);
00474   return removeUnknown(I);
00475 }
00476 
00477 
00478 // deleteValue method - This method is used to remove a pointer value from the
00479 // AliasSetTracker entirely.  It should be used when an instruction is deleted
00480 // from the program to update the AST.  If you don't use this, you would have
00481 // dangling pointers to deleted instructions.
00482 //
00483 void AliasSetTracker::deleteValue(Value *PtrVal) {
00484   // Notify the alias analysis implementation that this value is gone.
00485   AA.deleteValue(PtrVal);
00486 
00487   // If this is a call instruction, remove the callsite from the appropriate
00488   // AliasSet (if present).
00489   if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) {
00490     if (Inst->mayReadOrWriteMemory()) {
00491       // Scan all the alias sets to see if this call site is contained.
00492       for (iterator I = begin(), E = end(); I != E; ++I) {
00493         if (I->Forward) continue;
00494         
00495         I->removeUnknownInst(Inst);
00496       }
00497     }
00498   }
00499 
00500   // First, look up the PointerRec for this pointer.
00501   PointerMapType::iterator I = PointerMap.find_as(PtrVal);
00502   if (I == PointerMap.end()) return;  // Noop
00503 
00504   // If we found one, remove the pointer from the alias set it is in.
00505   AliasSet::PointerRec *PtrValEnt = I->second;
00506   AliasSet *AS = PtrValEnt->getAliasSet(*this);
00507 
00508   // Unlink and delete from the list of values.
00509   PtrValEnt->eraseFromList();
00510   
00511   // Stop using the alias set.
00512   AS->dropRef(*this);
00513   
00514   PointerMap.erase(I);
00515 }
00516 
00517 // copyValue - This method should be used whenever a preexisting value in the
00518 // program is copied or cloned, introducing a new value.  Note that it is ok for
00519 // clients that use this method to introduce the same value multiple times: if
00520 // the tracker already knows about a value, it will ignore the request.
00521 //
00522 void AliasSetTracker::copyValue(Value *From, Value *To) {
00523   // Notify the alias analysis implementation that this value is copied.
00524   AA.copyValue(From, To);
00525 
00526   // First, look up the PointerRec for this pointer.
00527   PointerMapType::iterator I = PointerMap.find_as(From);
00528   if (I == PointerMap.end())
00529     return;  // Noop
00530   assert(I->second->hasAliasSet() && "Dead entry?");
00531 
00532   AliasSet::PointerRec &Entry = getEntryFor(To);
00533   if (Entry.hasAliasSet()) return;    // Already in the tracker!
00534 
00535   // Add it to the alias set it aliases...
00536   I = PointerMap.find_as(From);
00537   AliasSet *AS = I->second->getAliasSet(*this);
00538   AS->addPointer(*this, Entry, I->second->getSize(),
00539                  I->second->getTBAAInfo(),
00540                  true);
00541 }
00542 
00543 
00544 
00545 //===----------------------------------------------------------------------===//
00546 //               AliasSet/AliasSetTracker Printing Support
00547 //===----------------------------------------------------------------------===//
00548 
00549 void AliasSet::print(raw_ostream &OS) const {
00550   OS << "  AliasSet[" << (const void*)this << ", " << RefCount << "] ";
00551   OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
00552   switch (AccessTy) {
00553   case NoModRef: OS << "No access "; break;
00554   case Refs    : OS << "Ref       "; break;
00555   case Mods    : OS << "Mod       "; break;
00556   case ModRef  : OS << "Mod/Ref   "; break;
00557   default: llvm_unreachable("Bad value for AccessTy!");
00558   }
00559   if (isVolatile()) OS << "[volatile] ";
00560   if (Forward)
00561     OS << " forwarding to " << (void*)Forward;
00562 
00563 
00564   if (!empty()) {
00565     OS << "Pointers: ";
00566     for (iterator I = begin(), E = end(); I != E; ++I) {
00567       if (I != begin()) OS << ", ";
00568       I.getPointer()->printAsOperand(OS << "(");
00569       OS << ", " << I.getSize() << ")";
00570     }
00571   }
00572   if (!UnknownInsts.empty()) {
00573     OS << "\n    " << UnknownInsts.size() << " Unknown instructions: ";
00574     for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
00575       if (i) OS << ", ";
00576       UnknownInsts[i]->printAsOperand(OS);
00577     }
00578   }
00579   OS << "\n";
00580 }
00581 
00582 void AliasSetTracker::print(raw_ostream &OS) const {
00583   OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
00584      << PointerMap.size() << " pointer values.\n";
00585   for (const_iterator I = begin(), E = end(); I != E; ++I)
00586     I->print(OS);
00587   OS << "\n";
00588 }
00589 
00590 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00591 void AliasSet::dump() const { print(dbgs()); }
00592 void AliasSetTracker::dump() const { print(dbgs()); }
00593 #endif
00594 
00595 //===----------------------------------------------------------------------===//
00596 //                     ASTCallbackVH Class Implementation
00597 //===----------------------------------------------------------------------===//
00598 
00599 void AliasSetTracker::ASTCallbackVH::deleted() {
00600   assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
00601   AST->deleteValue(getValPtr());
00602   // this now dangles!
00603 }
00604 
00605 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
00606   AST->copyValue(getValPtr(), V);
00607 }
00608 
00609 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
00610   : CallbackVH(V), AST(ast) {}
00611 
00612 AliasSetTracker::ASTCallbackVH &
00613 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
00614   return *this = ASTCallbackVH(V, AST);
00615 }
00616 
00617 //===----------------------------------------------------------------------===//
00618 //                            AliasSetPrinter Pass
00619 //===----------------------------------------------------------------------===//
00620 
00621 namespace {
00622   class AliasSetPrinter : public FunctionPass {
00623     AliasSetTracker *Tracker;
00624   public:
00625     static char ID; // Pass identification, replacement for typeid
00626     AliasSetPrinter() : FunctionPass(ID) {
00627       initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
00628     }
00629 
00630     void getAnalysisUsage(AnalysisUsage &AU) const override {
00631       AU.setPreservesAll();
00632       AU.addRequired<AliasAnalysis>();
00633     }
00634 
00635     bool runOnFunction(Function &F) override {
00636       Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
00637 
00638       for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
00639         Tracker->add(&*I);
00640       Tracker->print(errs());
00641       delete Tracker;
00642       return false;
00643     }
00644   };
00645 }
00646 
00647 char AliasSetPrinter::ID = 0;
00648 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
00649                 "Alias Set Printer", false, true)
00650 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
00651 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
00652                 "Alias Set Printer", false, true)