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