Home | History | Annotate | Line # | Download | only in Analysis
      1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 //
      9 // This file implements the AliasSetTracker and AliasSet classes.
     10 //
     11 //===----------------------------------------------------------------------===//
     12 
     13 #include "llvm/Analysis/AliasSetTracker.h"
     14 #include "llvm/Analysis/AliasAnalysis.h"
     15 #include "llvm/Analysis/GuardUtils.h"
     16 #include "llvm/Analysis/LoopInfo.h"
     17 #include "llvm/Analysis/MemoryLocation.h"
     18 #include "llvm/Config/llvm-config.h"
     19 #include "llvm/IR/Constants.h"
     20 #include "llvm/IR/DataLayout.h"
     21 #include "llvm/IR/Function.h"
     22 #include "llvm/IR/InstIterator.h"
     23 #include "llvm/IR/Instructions.h"
     24 #include "llvm/IR/IntrinsicInst.h"
     25 #include "llvm/IR/Module.h"
     26 #include "llvm/IR/PassManager.h"
     27 #include "llvm/IR/PatternMatch.h"
     28 #include "llvm/IR/Value.h"
     29 #include "llvm/InitializePasses.h"
     30 #include "llvm/Pass.h"
     31 #include "llvm/Support/AtomicOrdering.h"
     32 #include "llvm/Support/CommandLine.h"
     33 #include "llvm/Support/Compiler.h"
     34 #include "llvm/Support/Debug.h"
     35 #include "llvm/Support/ErrorHandling.h"
     36 #include "llvm/Support/raw_ostream.h"
     37 
     38 using namespace llvm;
     39 
     40 static cl::opt<unsigned>
     41     SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
     42                         cl::init(250),
     43                         cl::desc("The maximum number of pointers may-alias "
     44                                  "sets may contain before degradation"));
     45 
     46 /// mergeSetIn - Merge the specified alias set into this alias set.
     47 ///
     48 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
     49   assert(!AS.Forward && "Alias set is already forwarding!");
     50   assert(!Forward && "This set is a forwarding set!!");
     51 
     52   bool WasMustAlias = (Alias == SetMustAlias);
     53   // Update the alias and access types of this set...
     54   Access |= AS.Access;
     55   Alias  |= AS.Alias;
     56 
     57   if (Alias == SetMustAlias) {
     58     // Check that these two merged sets really are must aliases.  Since both
     59     // used to be must-alias sets, we can just check any pointer from each set
     60     // for aliasing.
     61     AliasAnalysis &AA = AST.getAliasAnalysis();
     62     PointerRec *L = getSomePointer();
     63     PointerRec *R = AS.getSomePointer();
     64 
     65     // If the pointers are not a must-alias pair, this set becomes a may alias.
     66     if (!AA.isMustAlias(
     67             MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
     68             MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())))
     69       Alias = SetMayAlias;
     70   }
     71 
     72   if (Alias == SetMayAlias) {
     73     if (WasMustAlias)
     74       AST.TotalMayAliasSetSize += size();
     75     if (AS.Alias == SetMustAlias)
     76       AST.TotalMayAliasSetSize += AS.size();
     77   }
     78 
     79   bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
     80   if (UnknownInsts.empty()) {            // Merge call sites...
     81     if (ASHadUnknownInsts) {
     82       std::swap(UnknownInsts, AS.UnknownInsts);
     83       addRef();
     84     }
     85   } else if (ASHadUnknownInsts) {
     86     llvm::append_range(UnknownInsts, AS.UnknownInsts);
     87     AS.UnknownInsts.clear();
     88   }
     89 
     90   AS.Forward = this; // Forward across AS now...
     91   addRef();          // AS is now pointing to us...
     92 
     93   // Merge the list of constituent pointers...
     94   if (AS.PtrList) {
     95     SetSize += AS.size();
     96     AS.SetSize = 0;
     97     *PtrListEnd = AS.PtrList;
     98     AS.PtrList->setPrevInList(PtrListEnd);
     99     PtrListEnd = AS.PtrListEnd;
    100 
    101     AS.PtrList = nullptr;
    102     AS.PtrListEnd = &AS.PtrList;
    103     assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
    104   }
    105   if (ASHadUnknownInsts)
    106     AS.dropRef(AST);
    107 }
    108 
    109 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
    110   if (AliasSet *Fwd = AS->Forward) {
    111     Fwd->dropRef(*this);
    112     AS->Forward = nullptr;
    113   } else // Update TotalMayAliasSetSize only if not forwarding.
    114       if (AS->Alias == AliasSet::SetMayAlias)
    115         TotalMayAliasSetSize -= AS->size();
    116 
    117   AliasSets.erase(AS);
    118   // If we've removed the saturated alias set, set saturated marker back to
    119   // nullptr and ensure this tracker is empty.
    120   if (AS == AliasAnyAS) {
    121     AliasAnyAS = nullptr;
    122     assert(AliasSets.empty() && "Tracker not empty");
    123   }
    124 }
    125 
    126 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
    127   assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
    128   AST.removeAliasSet(this);
    129 }
    130 
    131 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
    132                           LocationSize Size, const AAMDNodes &AAInfo,
    133                           bool KnownMustAlias, bool SkipSizeUpdate) {
    134   assert(!Entry.hasAliasSet() && "Entry already in set!");
    135 
    136   // Check to see if we have to downgrade to _may_ alias.
    137   if (isMustAlias())
    138     if (PointerRec *P = getSomePointer()) {
    139       if (!KnownMustAlias) {
    140         AliasAnalysis &AA = AST.getAliasAnalysis();
    141         AliasResult Result = AA.alias(
    142             MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
    143             MemoryLocation(Entry.getValue(), Size, AAInfo));
    144         if (Result != AliasResult::MustAlias) {
    145           Alias = SetMayAlias;
    146           AST.TotalMayAliasSetSize += size();
    147         }
    148         assert(Result != AliasResult::NoAlias && "Cannot be part of must set!");
    149       } else if (!SkipSizeUpdate)
    150         P->updateSizeAndAAInfo(Size, AAInfo);
    151     }
    152 
    153   Entry.setAliasSet(this);
    154   Entry.updateSizeAndAAInfo(Size, AAInfo);
    155 
    156   // Add it to the end of the list...
    157   ++SetSize;
    158   assert(*PtrListEnd == nullptr && "End of list is not null?");
    159   *PtrListEnd = &Entry;
    160   PtrListEnd = Entry.setPrevInList(PtrListEnd);
    161   assert(*PtrListEnd == nullptr && "End of list is not null?");
    162   // Entry points to alias set.
    163   addRef();
    164 
    165   if (Alias == SetMayAlias)
    166     AST.TotalMayAliasSetSize++;
    167 }
    168 
    169 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
    170   if (UnknownInsts.empty())
    171     addRef();
    172   UnknownInsts.emplace_back(I);
    173 
    174   // Guards are marked as modifying memory for control flow modelling purposes,
    175   // but don't actually modify any specific memory location.
    176   using namespace PatternMatch;
    177   bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) &&
    178     !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));
    179   if (!MayWriteMemory) {
    180     Alias = SetMayAlias;
    181     Access |= RefAccess;
    182     return;
    183   }
    184 
    185   // FIXME: This should use mod/ref information to make this not suck so bad
    186   Alias = SetMayAlias;
    187   Access = ModRefAccess;
    188 }
    189 
    190 /// aliasesPointer - If the specified pointer "may" (or must) alias one of the
    191 /// members in the set return the appropriate AliasResult. Otherwise return
    192 /// NoAlias.
    193 ///
    194 AliasResult AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
    195                                      const AAMDNodes &AAInfo,
    196                                      AliasAnalysis &AA) const {
    197   if (AliasAny)
    198     return AliasResult::MayAlias;
    199 
    200   if (Alias == SetMustAlias) {
    201     assert(UnknownInsts.empty() && "Illegal must alias set!");
    202 
    203     // If this is a set of MustAliases, only check to see if the pointer aliases
    204     // SOME value in the set.
    205     PointerRec *SomePtr = getSomePointer();
    206     assert(SomePtr && "Empty must-alias set??");
    207     return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
    208                                    SomePtr->getAAInfo()),
    209                     MemoryLocation(Ptr, Size, AAInfo));
    210   }
    211 
    212   // If this is a may-alias set, we have to check all of the pointers in the set
    213   // to be sure it doesn't alias the set...
    214   for (iterator I = begin(), E = end(); I != E; ++I) {
    215     AliasResult AR =
    216         AA.alias(MemoryLocation(Ptr, Size, AAInfo),
    217                  MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()));
    218     if (AR != AliasResult::NoAlias)
    219       return AR;
    220   }
    221 
    222   // Check the unknown instructions...
    223   if (!UnknownInsts.empty()) {
    224     for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
    225       if (auto *Inst = getUnknownInst(i))
    226         if (isModOrRefSet(
    227                 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
    228           return AliasResult::MayAlias;
    229   }
    230 
    231   return AliasResult::NoAlias;
    232 }
    233 
    234 bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
    235                                   AliasAnalysis &AA) const {
    236 
    237   if (AliasAny)
    238     return true;
    239 
    240   assert(Inst->mayReadOrWriteMemory() &&
    241          "Instruction must either read or write memory.");
    242 
    243   for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
    244     if (auto *UnknownInst = getUnknownInst(i)) {
    245       const auto *C1 = dyn_cast<CallBase>(UnknownInst);
    246       const auto *C2 = dyn_cast<CallBase>(Inst);
    247       if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
    248           isModOrRefSet(AA.getModRefInfo(C2, C1)))
    249         return true;
    250     }
    251   }
    252 
    253   for (iterator I = begin(), E = end(); I != E; ++I)
    254     if (isModOrRefSet(AA.getModRefInfo(
    255             Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
    256       return true;
    257 
    258   return false;
    259 }
    260 
    261 Instruction* AliasSet::getUniqueInstruction() {
    262   if (AliasAny)
    263     // May have collapses alias set
    264     return nullptr;
    265   if (begin() != end()) {
    266     if (!UnknownInsts.empty())
    267       // Another instruction found
    268       return nullptr;
    269     if (std::next(begin()) != end())
    270       // Another instruction found
    271       return nullptr;
    272     Value *Addr = begin()->getValue();
    273     assert(!Addr->user_empty() &&
    274            "where's the instruction which added this pointer?");
    275     if (std::next(Addr->user_begin()) != Addr->user_end())
    276       // Another instruction found -- this is really restrictive
    277       // TODO: generalize!
    278       return nullptr;
    279     return cast<Instruction>(*(Addr->user_begin()));
    280   }
    281   if (1 != UnknownInsts.size())
    282     return nullptr;
    283   return cast<Instruction>(UnknownInsts[0]);
    284 }
    285 
    286 void AliasSetTracker::clear() {
    287   // Delete all the PointerRec entries.
    288   for (auto &I : PointerMap)
    289     I.second->eraseFromList();
    290 
    291   PointerMap.clear();
    292 
    293   // The alias sets should all be clear now.
    294   AliasSets.clear();
    295 }
    296 
    297 /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
    298 /// alias the pointer. Return the unified set, or nullptr if no set that aliases
    299 /// the pointer was found. MustAliasAll is updated to true/false if the pointer
    300 /// is found to MustAlias all the sets it merged.
    301 AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
    302                                                     LocationSize Size,
    303                                                     const AAMDNodes &AAInfo,
    304                                                     bool &MustAliasAll) {
    305   AliasSet *FoundSet = nullptr;
    306   MustAliasAll = true;
    307   for (AliasSet &AS : llvm::make_early_inc_range(*this)) {
    308     if (AS.Forward)
    309       continue;
    310 
    311     AliasResult AR = AS.aliasesPointer(Ptr, Size, AAInfo, AA);
    312     if (AR == AliasResult::NoAlias)
    313       continue;
    314 
    315     if (AR != AliasResult::MustAlias)
    316       MustAliasAll = false;
    317 
    318     if (!FoundSet) {
    319       // If this is the first alias set ptr can go into, remember it.
    320       FoundSet = &AS;
    321     } else {
    322       // Otherwise, we must merge the sets.
    323       FoundSet->mergeSetIn(AS, *this);
    324     }
    325   }
    326 
    327   return FoundSet;
    328 }
    329 
    330 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
    331   AliasSet *FoundSet = nullptr;
    332   for (AliasSet &AS : llvm::make_early_inc_range(*this)) {
    333     if (AS.Forward || !AS.aliasesUnknownInst(Inst, AA))
    334       continue;
    335     if (!FoundSet) {
    336       // If this is the first alias set ptr can go into, remember it.
    337       FoundSet = &AS;
    338     } else {
    339       // Otherwise, we must merge the sets.
    340       FoundSet->mergeSetIn(AS, *this);
    341     }
    342   }
    343   return FoundSet;
    344 }
    345 
    346 AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
    347 
    348   Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
    349   const LocationSize Size = MemLoc.Size;
    350   const AAMDNodes &AAInfo = MemLoc.AATags;
    351 
    352   AliasSet::PointerRec &Entry = getEntryFor(Pointer);
    353 
    354   if (AliasAnyAS) {
    355     // At this point, the AST is saturated, so we only have one active alias
    356     // set. That means we already know which alias set we want to return, and
    357     // just need to add the pointer to that set to keep the data structure
    358     // consistent.
    359     // This, of course, means that we will never need a merge here.
    360     if (Entry.hasAliasSet()) {
    361       Entry.updateSizeAndAAInfo(Size, AAInfo);
    362       assert(Entry.getAliasSet(*this) == AliasAnyAS &&
    363              "Entry in saturated AST must belong to only alias set");
    364     } else {
    365       AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
    366     }
    367     return *AliasAnyAS;
    368   }
    369 
    370   bool MustAliasAll = false;
    371   // Check to see if the pointer is already known.
    372   if (Entry.hasAliasSet()) {
    373     // If the size changed, we may need to merge several alias sets.
    374     // Note that we can *not* return the result of mergeAliasSetsForPointer
    375     // due to a quirk of alias analysis behavior. Since alias(undef, undef)
    376     // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
    377     // the right set for undef, even if it exists.
    378     if (Entry.updateSizeAndAAInfo(Size, AAInfo))
    379       mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll);
    380     // Return the set!
    381     return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
    382   }
    383 
    384   if (AliasSet *AS =
    385           mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
    386     // Add it to the alias set it aliases.
    387     AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll);
    388     return *AS;
    389   }
    390 
    391   // Otherwise create a new alias set to hold the loaded pointer.
    392   AliasSets.push_back(new AliasSet());
    393   AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true);
    394   return AliasSets.back();
    395 }
    396 
    397 void AliasSetTracker::add(Value *Ptr, LocationSize Size,
    398                           const AAMDNodes &AAInfo) {
    399   addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
    400 }
    401 
    402 void AliasSetTracker::add(LoadInst *LI) {
    403   if (isStrongerThanMonotonic(LI->getOrdering()))
    404     return addUnknown(LI);
    405   addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
    406 }
    407 
    408 void AliasSetTracker::add(StoreInst *SI) {
    409   if (isStrongerThanMonotonic(SI->getOrdering()))
    410     return addUnknown(SI);
    411   addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
    412 }
    413 
    414 void AliasSetTracker::add(VAArgInst *VAAI) {
    415   addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
    416 }
    417 
    418 void AliasSetTracker::add(AnyMemSetInst *MSI) {
    419   addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
    420 }
    421 
    422 void AliasSetTracker::add(AnyMemTransferInst *MTI) {
    423   addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
    424   addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
    425 }
    426 
    427 void AliasSetTracker::addUnknown(Instruction *Inst) {
    428   if (isa<DbgInfoIntrinsic>(Inst))
    429     return; // Ignore DbgInfo Intrinsics.
    430 
    431   if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
    432     // These intrinsics will show up as affecting memory, but they are just
    433     // markers.
    434     switch (II->getIntrinsicID()) {
    435     default:
    436       break;
    437       // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
    438     case Intrinsic::assume:
    439     case Intrinsic::experimental_noalias_scope_decl:
    440     case Intrinsic::sideeffect:
    441     case Intrinsic::pseudoprobe:
    442       return;
    443     }
    444   }
    445   if (!Inst->mayReadOrWriteMemory())
    446     return; // doesn't alias anything
    447 
    448   if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
    449     AS->addUnknownInst(Inst, AA);
    450     return;
    451   }
    452   AliasSets.push_back(new AliasSet());
    453   AliasSets.back().addUnknownInst(Inst, AA);
    454 }
    455 
    456 void AliasSetTracker::add(Instruction *I) {
    457   // Dispatch to one of the other add methods.
    458   if (LoadInst *LI = dyn_cast<LoadInst>(I))
    459     return add(LI);
    460   if (StoreInst *SI = dyn_cast<StoreInst>(I))
    461     return add(SI);
    462   if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
    463     return add(VAAI);
    464   if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
    465     return add(MSI);
    466   if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
    467     return add(MTI);
    468 
    469   // Handle all calls with known mod/ref sets genericall
    470   if (auto *Call = dyn_cast<CallBase>(I))
    471     if (Call->onlyAccessesArgMemory()) {
    472       auto getAccessFromModRef = [](ModRefInfo MRI) {
    473         if (isRefSet(MRI) && isModSet(MRI))
    474           return AliasSet::ModRefAccess;
    475         else if (isModSet(MRI))
    476           return AliasSet::ModAccess;
    477         else if (isRefSet(MRI))
    478           return AliasSet::RefAccess;
    479         else
    480           return AliasSet::NoAccess;
    481       };
    482 
    483       ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call));
    484 
    485       // Some intrinsics are marked as modifying memory for control flow
    486       // modelling purposes, but don't actually modify any specific memory
    487       // location.
    488       using namespace PatternMatch;
    489       if (Call->use_empty() &&
    490           match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
    491         CallMask = clearMod(CallMask);
    492 
    493       for (auto IdxArgPair : enumerate(Call->args())) {
    494         int ArgIdx = IdxArgPair.index();
    495         const Value *Arg = IdxArgPair.value();
    496         if (!Arg->getType()->isPointerTy())
    497           continue;
    498         MemoryLocation ArgLoc =
    499             MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
    500         ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
    501         ArgMask = intersectModRef(CallMask, ArgMask);
    502         if (!isNoModRef(ArgMask))
    503           addPointer(ArgLoc, getAccessFromModRef(ArgMask));
    504       }
    505       return;
    506     }
    507 
    508   return addUnknown(I);
    509 }
    510 
    511 void AliasSetTracker::add(BasicBlock &BB) {
    512   for (auto &I : BB)
    513     add(&I);
    514 }
    515 
    516 void AliasSetTracker::add(const AliasSetTracker &AST) {
    517   assert(&AA == &AST.AA &&
    518          "Merging AliasSetTracker objects with different Alias Analyses!");
    519 
    520   // Loop over all of the alias sets in AST, adding the pointers contained
    521   // therein into the current alias sets.  This can cause alias sets to be
    522   // merged together in the current AST.
    523   for (const AliasSet &AS : AST) {
    524     if (AS.Forward)
    525       continue; // Ignore forwarding alias sets
    526 
    527     // If there are any call sites in the alias set, add them to this AST.
    528     for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
    529       if (auto *Inst = AS.getUnknownInst(i))
    530         add(Inst);
    531 
    532     // Loop over all of the pointers in this alias set.
    533     for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
    534       addPointer(
    535           MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
    536           (AliasSet::AccessLattice)AS.Access);
    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   // First, look up the PointerRec for this pointer.
    547   PointerMapType::iterator I = PointerMap.find_as(PtrVal);
    548   if (I == PointerMap.end()) return;  // Noop
    549 
    550   // If we found one, remove the pointer from the alias set it is in.
    551   AliasSet::PointerRec *PtrValEnt = I->second;
    552   AliasSet *AS = PtrValEnt->getAliasSet(*this);
    553 
    554   // Unlink and delete from the list of values.
    555   PtrValEnt->eraseFromList();
    556 
    557   if (AS->Alias == AliasSet::SetMayAlias) {
    558     AS->SetSize--;
    559     TotalMayAliasSetSize--;
    560   }
    561 
    562   // Stop using the alias set.
    563   AS->dropRef(*this);
    564 
    565   PointerMap.erase(I);
    566 }
    567 
    568 // copyValue - This method should be used whenever a preexisting value in the
    569 // program is copied or cloned, introducing a new value.  Note that it is ok for
    570 // clients that use this method to introduce the same value multiple times: if
    571 // the tracker already knows about a value, it will ignore the request.
    572 //
    573 void AliasSetTracker::copyValue(Value *From, Value *To) {
    574   // First, look up the PointerRec for this pointer.
    575   PointerMapType::iterator I = PointerMap.find_as(From);
    576   if (I == PointerMap.end())
    577     return;  // Noop
    578   assert(I->second->hasAliasSet() && "Dead entry?");
    579 
    580   AliasSet::PointerRec &Entry = getEntryFor(To);
    581   if (Entry.hasAliasSet()) return;    // Already in the tracker!
    582 
    583   // getEntryFor above may invalidate iterator \c I, so reinitialize it.
    584   I = PointerMap.find_as(From);
    585   // Add it to the alias set it aliases...
    586   AliasSet *AS = I->second->getAliasSet(*this);
    587   AS->addPointer(*this, Entry, I->second->getSize(), I->second->getAAInfo(),
    588                  true, true);
    589 }
    590 
    591 AliasSet &AliasSetTracker::mergeAllAliasSets() {
    592   assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
    593          "Full merge should happen once, when the saturation threshold is "
    594          "reached");
    595 
    596   // Collect all alias sets, so that we can drop references with impunity
    597   // without worrying about iterator invalidation.
    598   std::vector<AliasSet *> ASVector;
    599   ASVector.reserve(SaturationThreshold);
    600   for (AliasSet &AS : *this)
    601     ASVector.push_back(&AS);
    602 
    603   // Copy all instructions and pointers into a new set, and forward all other
    604   // sets to it.
    605   AliasSets.push_back(new AliasSet());
    606   AliasAnyAS = &AliasSets.back();
    607   AliasAnyAS->Alias = AliasSet::SetMayAlias;
    608   AliasAnyAS->Access = AliasSet::ModRefAccess;
    609   AliasAnyAS->AliasAny = true;
    610 
    611   for (auto Cur : ASVector) {
    612     // If Cur was already forwarding, just forward to the new AS instead.
    613     AliasSet *FwdTo = Cur->Forward;
    614     if (FwdTo) {
    615       Cur->Forward = AliasAnyAS;
    616       AliasAnyAS->addRef();
    617       FwdTo->dropRef(*this);
    618       continue;
    619     }
    620 
    621     // Otherwise, perform the actual merge.
    622     AliasAnyAS->mergeSetIn(*Cur, *this);
    623   }
    624 
    625   return *AliasAnyAS;
    626 }
    627 
    628 AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
    629                                       AliasSet::AccessLattice E) {
    630   AliasSet &AS = getAliasSetFor(Loc);
    631   AS.Access |= E;
    632 
    633   if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
    634     // The AST is now saturated. From here on, we conservatively consider all
    635     // pointers to alias each-other.
    636     return mergeAllAliasSets();
    637   }
    638 
    639   return AS;
    640 }
    641 
    642 //===----------------------------------------------------------------------===//
    643 //               AliasSet/AliasSetTracker Printing Support
    644 //===----------------------------------------------------------------------===//
    645 
    646 void AliasSet::print(raw_ostream &OS) const {
    647   OS << "  AliasSet[" << (const void*)this << ", " << RefCount << "] ";
    648   OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
    649   switch (Access) {
    650   case NoAccess:     OS << "No access "; break;
    651   case RefAccess:    OS << "Ref       "; break;
    652   case ModAccess:    OS << "Mod       "; break;
    653   case ModRefAccess: OS << "Mod/Ref   "; break;
    654   default: llvm_unreachable("Bad value for Access!");
    655   }
    656   if (Forward)
    657     OS << " forwarding to " << (void*)Forward;
    658 
    659   if (!empty()) {
    660     OS << "Pointers: ";
    661     for (iterator I = begin(), E = end(); I != E; ++I) {
    662       if (I != begin()) OS << ", ";
    663       I.getPointer()->printAsOperand(OS << "(");
    664       if (I.getSize() == LocationSize::afterPointer())
    665         OS << ", unknown after)";
    666       else if (I.getSize() == LocationSize::beforeOrAfterPointer())
    667         OS << ", unknown before-or-after)";
    668       else
    669         OS << ", " << I.getSize() << ")";
    670     }
    671   }
    672   if (!UnknownInsts.empty()) {
    673     OS << "\n    " << UnknownInsts.size() << " Unknown instructions: ";
    674     for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
    675       if (i) OS << ", ";
    676       if (auto *I = getUnknownInst(i)) {
    677         if (I->hasName())
    678           I->printAsOperand(OS);
    679         else
    680           I->print(OS);
    681       }
    682     }
    683   }
    684   OS << "\n";
    685 }
    686 
    687 void AliasSetTracker::print(raw_ostream &OS) const {
    688   OS << "Alias Set Tracker: " << AliasSets.size();
    689   if (AliasAnyAS)
    690     OS << " (Saturated)";
    691   OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
    692   for (const AliasSet &AS : *this)
    693     AS.print(OS);
    694   OS << "\n";
    695 }
    696 
    697 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    698 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
    699 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
    700 #endif
    701 
    702 //===----------------------------------------------------------------------===//
    703 //                     ASTCallbackVH Class Implementation
    704 //===----------------------------------------------------------------------===//
    705 
    706 void AliasSetTracker::ASTCallbackVH::deleted() {
    707   assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
    708   AST->deleteValue(getValPtr());
    709   // this now dangles!
    710 }
    711 
    712 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
    713   AST->copyValue(getValPtr(), V);
    714 }
    715 
    716 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
    717   : CallbackVH(V), AST(ast) {}
    718 
    719 AliasSetTracker::ASTCallbackVH &
    720 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
    721   return *this = ASTCallbackVH(V, AST);
    722 }
    723 
    724 //===----------------------------------------------------------------------===//
    725 //                            AliasSetPrinter Pass
    726 //===----------------------------------------------------------------------===//
    727 
    728 namespace {
    729 
    730   class AliasSetPrinter : public FunctionPass {
    731   public:
    732     static char ID; // Pass identification, replacement for typeid
    733 
    734     AliasSetPrinter() : FunctionPass(ID) {
    735       initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
    736     }
    737 
    738     void getAnalysisUsage(AnalysisUsage &AU) const override {
    739       AU.setPreservesAll();
    740       AU.addRequired<AAResultsWrapperPass>();
    741     }
    742 
    743     bool runOnFunction(Function &F) override {
    744       auto &AAWP = getAnalysis<AAResultsWrapperPass>();
    745       AliasSetTracker Tracker(AAWP.getAAResults());
    746       errs() << "Alias sets for function '" << F.getName() << "':\n";
    747       for (Instruction &I : instructions(F))
    748         Tracker.add(&I);
    749       Tracker.print(errs());
    750       return false;
    751     }
    752   };
    753 
    754 } // end anonymous namespace
    755 
    756 char AliasSetPrinter::ID = 0;
    757 
    758 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
    759                 "Alias Set Printer", false, true)
    760 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
    761 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
    762                 "Alias Set Printer", false, true)
    763 
    764 AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream &OS) : OS(OS) {}
    765 
    766 PreservedAnalyses AliasSetsPrinterPass::run(Function &F,
    767                                             FunctionAnalysisManager &AM) {
    768   auto &AA = AM.getResult<AAManager>(F);
    769   AliasSetTracker Tracker(AA);
    770   OS << "Alias sets for function '" << F.getName() << "':\n";
    771   for (Instruction &I : instructions(F))
    772     Tracker.add(&I);
    773   Tracker.print(OS);
    774   return PreservedAnalyses::all();
    775 }
    776