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