xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/AliasSetTracker.cpp (revision 77013d11e6483b970af25e13c9b892075742f7e5)
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/GuardUtils.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/Analysis/MemoryLocation.h"
17 #include "llvm/Analysis/MemorySSA.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.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
67                  MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
68         MustAlias)
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 != MustAlias) {
145           Alias = SetMayAlias;
146           AST.TotalMayAliasSetSize += size();
147         }
148         assert(Result != 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 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     if (AliasResult AR = AA.alias(
216             MemoryLocation(Ptr, Size, AAInfo),
217             MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
218       return AR;
219 
220   // Check the unknown instructions...
221   if (!UnknownInsts.empty()) {
222     for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
223       if (auto *Inst = getUnknownInst(i))
224         if (isModOrRefSet(
225                 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
226           return MayAlias;
227   }
228 
229   return NoAlias;
230 }
231 
232 bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
233                                   AliasAnalysis &AA) const {
234 
235   if (AliasAny)
236     return true;
237 
238   assert(Inst->mayReadOrWriteMemory() &&
239          "Instruction must either read or write memory.");
240 
241   for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
242     if (auto *UnknownInst = getUnknownInst(i)) {
243       const auto *C1 = dyn_cast<CallBase>(UnknownInst);
244       const auto *C2 = dyn_cast<CallBase>(Inst);
245       if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
246           isModOrRefSet(AA.getModRefInfo(C2, C1)))
247         return true;
248     }
249   }
250 
251   for (iterator I = begin(), E = end(); I != E; ++I)
252     if (isModOrRefSet(AA.getModRefInfo(
253             Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
254       return true;
255 
256   return false;
257 }
258 
259 Instruction* AliasSet::getUniqueInstruction() {
260   if (AliasAny)
261     // May have collapses alias set
262     return nullptr;
263   if (begin() != end()) {
264     if (!UnknownInsts.empty())
265       // Another instruction found
266       return nullptr;
267     if (std::next(begin()) != end())
268       // Another instruction found
269       return nullptr;
270     Value *Addr = begin()->getValue();
271     assert(!Addr->user_empty() &&
272            "where's the instruction which added this pointer?");
273     if (std::next(Addr->user_begin()) != Addr->user_end())
274       // Another instruction found -- this is really restrictive
275       // TODO: generalize!
276       return nullptr;
277     return cast<Instruction>(*(Addr->user_begin()));
278   }
279   if (1 != UnknownInsts.size())
280     return nullptr;
281   return cast<Instruction>(UnknownInsts[0]);
282 }
283 
284 void AliasSetTracker::clear() {
285   // Delete all the PointerRec entries.
286   for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
287        I != E; ++I)
288     I->second->eraseFromList();
289 
290   PointerMap.clear();
291 
292   // The alias sets should all be clear now.
293   AliasSets.clear();
294 }
295 
296 /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
297 /// alias the pointer. Return the unified set, or nullptr if no set that aliases
298 /// the pointer was found. MustAliasAll is updated to true/false if the pointer
299 /// is found to MustAlias all the sets it merged.
300 AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
301                                                     LocationSize Size,
302                                                     const AAMDNodes &AAInfo,
303                                                     bool &MustAliasAll) {
304   AliasSet *FoundSet = nullptr;
305   AliasResult AllAR = MustAlias;
306   for (iterator I = begin(), E = end(); I != E;) {
307     iterator Cur = I++;
308     if (Cur->Forward)
309       continue;
310 
311     AliasResult AR = Cur->aliasesPointer(Ptr, Size, AAInfo, AA);
312     if (AR == NoAlias)
313       continue;
314 
315     AllAR =
316         AliasResult(AllAR & AR); // Possible downgrade to May/Partial, even No
317 
318     if (!FoundSet) {
319       // If this is the first alias set ptr can go into, remember it.
320       FoundSet = &*Cur;
321     } else {
322       // Otherwise, we must merge the sets.
323       FoundSet->mergeSetIn(*Cur, *this);
324     }
325   }
326 
327   MustAliasAll = (AllAR == MustAlias);
328   return FoundSet;
329 }
330 
331 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
332   AliasSet *FoundSet = nullptr;
333   for (iterator I = begin(), E = end(); I != E;) {
334     iterator Cur = I++;
335     if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
336       continue;
337     if (!FoundSet) {
338       // If this is the first alias set ptr can go into, remember it.
339       FoundSet = &*Cur;
340     } else {
341       // Otherwise, we must merge the sets.
342       FoundSet->mergeSetIn(*Cur, *this);
343     }
344   }
345   return FoundSet;
346 }
347 
348 AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
349 
350   Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
351   const LocationSize Size = MemLoc.Size;
352   const AAMDNodes &AAInfo = MemLoc.AATags;
353 
354   AliasSet::PointerRec &Entry = getEntryFor(Pointer);
355 
356   if (AliasAnyAS) {
357     // At this point, the AST is saturated, so we only have one active alias
358     // set. That means we already know which alias set we want to return, and
359     // just need to add the pointer to that set to keep the data structure
360     // consistent.
361     // This, of course, means that we will never need a merge here.
362     if (Entry.hasAliasSet()) {
363       Entry.updateSizeAndAAInfo(Size, AAInfo);
364       assert(Entry.getAliasSet(*this) == AliasAnyAS &&
365              "Entry in saturated AST must belong to only alias set");
366     } else {
367       AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
368     }
369     return *AliasAnyAS;
370   }
371 
372   bool MustAliasAll = false;
373   // Check to see if the pointer is already known.
374   if (Entry.hasAliasSet()) {
375     // If the size changed, we may need to merge several alias sets.
376     // Note that we can *not* return the result of mergeAliasSetsForPointer
377     // due to a quirk of alias analysis behavior. Since alias(undef, undef)
378     // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
379     // the right set for undef, even if it exists.
380     if (Entry.updateSizeAndAAInfo(Size, AAInfo))
381       mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll);
382     // Return the set!
383     return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
384   }
385 
386   if (AliasSet *AS =
387           mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
388     // Add it to the alias set it aliases.
389     AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll);
390     return *AS;
391   }
392 
393   // Otherwise create a new alias set to hold the loaded pointer.
394   AliasSets.push_back(new AliasSet());
395   AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true);
396   return AliasSets.back();
397 }
398 
399 void AliasSetTracker::add(Value *Ptr, LocationSize Size,
400                           const AAMDNodes &AAInfo) {
401   addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
402 }
403 
404 void AliasSetTracker::add(LoadInst *LI) {
405   if (isStrongerThanMonotonic(LI->getOrdering()))
406     return addUnknown(LI);
407   addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
408 }
409 
410 void AliasSetTracker::add(StoreInst *SI) {
411   if (isStrongerThanMonotonic(SI->getOrdering()))
412     return addUnknown(SI);
413   addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
414 }
415 
416 void AliasSetTracker::add(VAArgInst *VAAI) {
417   addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
418 }
419 
420 void AliasSetTracker::add(AnyMemSetInst *MSI) {
421   addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
422 }
423 
424 void AliasSetTracker::add(AnyMemTransferInst *MTI) {
425   addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
426   addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
427 }
428 
429 void AliasSetTracker::addUnknown(Instruction *Inst) {
430   if (isa<DbgInfoIntrinsic>(Inst))
431     return; // Ignore DbgInfo Intrinsics.
432 
433   if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
434     // These intrinsics will show up as affecting memory, but they are just
435     // markers.
436     switch (II->getIntrinsicID()) {
437     default:
438       break;
439       // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
440     case Intrinsic::assume:
441     case Intrinsic::experimental_noalias_scope_decl:
442     case Intrinsic::sideeffect:
443     case Intrinsic::pseudoprobe:
444       return;
445     }
446   }
447   if (!Inst->mayReadOrWriteMemory())
448     return; // doesn't alias anything
449 
450   if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
451     AS->addUnknownInst(Inst, AA);
452     return;
453   }
454   AliasSets.push_back(new AliasSet());
455   AliasSets.back().addUnknownInst(Inst, AA);
456 }
457 
458 void AliasSetTracker::add(Instruction *I) {
459   // Dispatch to one of the other add methods.
460   if (LoadInst *LI = dyn_cast<LoadInst>(I))
461     return add(LI);
462   if (StoreInst *SI = dyn_cast<StoreInst>(I))
463     return add(SI);
464   if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
465     return add(VAAI);
466   if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
467     return add(MSI);
468   if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
469     return add(MTI);
470 
471   // Handle all calls with known mod/ref sets genericall
472   if (auto *Call = dyn_cast<CallBase>(I))
473     if (Call->onlyAccessesArgMemory()) {
474       auto getAccessFromModRef = [](ModRefInfo MRI) {
475         if (isRefSet(MRI) && isModSet(MRI))
476           return AliasSet::ModRefAccess;
477         else if (isModSet(MRI))
478           return AliasSet::ModAccess;
479         else if (isRefSet(MRI))
480           return AliasSet::RefAccess;
481         else
482           return AliasSet::NoAccess;
483       };
484 
485       ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call));
486 
487       // Some intrinsics are marked as modifying memory for control flow
488       // modelling purposes, but don't actually modify any specific memory
489       // location.
490       using namespace PatternMatch;
491       if (Call->use_empty() &&
492           match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
493         CallMask = clearMod(CallMask);
494 
495       for (auto IdxArgPair : enumerate(Call->args())) {
496         int ArgIdx = IdxArgPair.index();
497         const Value *Arg = IdxArgPair.value();
498         if (!Arg->getType()->isPointerTy())
499           continue;
500         MemoryLocation ArgLoc =
501             MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
502         ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
503         ArgMask = intersectModRef(CallMask, ArgMask);
504         if (!isNoModRef(ArgMask))
505           addPointer(ArgLoc, getAccessFromModRef(ArgMask));
506       }
507       return;
508     }
509 
510   return addUnknown(I);
511 }
512 
513 void AliasSetTracker::add(BasicBlock &BB) {
514   for (auto &I : BB)
515     add(&I);
516 }
517 
518 void AliasSetTracker::add(const AliasSetTracker &AST) {
519   assert(&AA == &AST.AA &&
520          "Merging AliasSetTracker objects with different Alias Analyses!");
521 
522   // Loop over all of the alias sets in AST, adding the pointers contained
523   // therein into the current alias sets.  This can cause alias sets to be
524   // merged together in the current AST.
525   for (const AliasSet &AS : AST) {
526     if (AS.Forward)
527       continue; // Ignore forwarding alias sets
528 
529     // If there are any call sites in the alias set, add them to this AST.
530     for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
531       if (auto *Inst = AS.getUnknownInst(i))
532         add(Inst);
533 
534     // Loop over all of the pointers in this alias set.
535     for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
536       addPointer(
537           MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
538           (AliasSet::AccessLattice)AS.Access);
539   }
540 }
541 
542 void AliasSetTracker::addAllInstructionsInLoopUsingMSSA() {
543   assert(MSSA && L && "MSSA and L must be available");
544   for (const BasicBlock *BB : L->blocks())
545     if (auto *Accesses = MSSA->getBlockAccesses(BB))
546       for (auto &Access : *Accesses)
547         if (auto *MUD = dyn_cast<MemoryUseOrDef>(&Access))
548           add(MUD->getMemoryInst());
549 }
550 
551 // deleteValue method - This method is used to remove a pointer value from the
552 // AliasSetTracker entirely.  It should be used when an instruction is deleted
553 // from the program to update the AST.  If you don't use this, you would have
554 // dangling pointers to deleted instructions.
555 //
556 void AliasSetTracker::deleteValue(Value *PtrVal) {
557   // First, look up the PointerRec for this pointer.
558   PointerMapType::iterator I = PointerMap.find_as(PtrVal);
559   if (I == PointerMap.end()) return;  // Noop
560 
561   // If we found one, remove the pointer from the alias set it is in.
562   AliasSet::PointerRec *PtrValEnt = I->second;
563   AliasSet *AS = PtrValEnt->getAliasSet(*this);
564 
565   // Unlink and delete from the list of values.
566   PtrValEnt->eraseFromList();
567 
568   if (AS->Alias == AliasSet::SetMayAlias) {
569     AS->SetSize--;
570     TotalMayAliasSetSize--;
571   }
572 
573   // Stop using the alias set.
574   AS->dropRef(*this);
575 
576   PointerMap.erase(I);
577 }
578 
579 // copyValue - This method should be used whenever a preexisting value in the
580 // program is copied or cloned, introducing a new value.  Note that it is ok for
581 // clients that use this method to introduce the same value multiple times: if
582 // the tracker already knows about a value, it will ignore the request.
583 //
584 void AliasSetTracker::copyValue(Value *From, Value *To) {
585   // First, look up the PointerRec for this pointer.
586   PointerMapType::iterator I = PointerMap.find_as(From);
587   if (I == PointerMap.end())
588     return;  // Noop
589   assert(I->second->hasAliasSet() && "Dead entry?");
590 
591   AliasSet::PointerRec &Entry = getEntryFor(To);
592   if (Entry.hasAliasSet()) return;    // Already in the tracker!
593 
594   // getEntryFor above may invalidate iterator \c I, so reinitialize it.
595   I = PointerMap.find_as(From);
596   // Add it to the alias set it aliases...
597   AliasSet *AS = I->second->getAliasSet(*this);
598   AS->addPointer(*this, Entry, I->second->getSize(), I->second->getAAInfo(),
599                  true, true);
600 }
601 
602 AliasSet &AliasSetTracker::mergeAllAliasSets() {
603   assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
604          "Full merge should happen once, when the saturation threshold is "
605          "reached");
606 
607   // Collect all alias sets, so that we can drop references with impunity
608   // without worrying about iterator invalidation.
609   std::vector<AliasSet *> ASVector;
610   ASVector.reserve(SaturationThreshold);
611   for (iterator I = begin(), E = end(); I != E; I++)
612     ASVector.push_back(&*I);
613 
614   // Copy all instructions and pointers into a new set, and forward all other
615   // sets to it.
616   AliasSets.push_back(new AliasSet());
617   AliasAnyAS = &AliasSets.back();
618   AliasAnyAS->Alias = AliasSet::SetMayAlias;
619   AliasAnyAS->Access = AliasSet::ModRefAccess;
620   AliasAnyAS->AliasAny = true;
621 
622   for (auto Cur : ASVector) {
623     // If Cur was already forwarding, just forward to the new AS instead.
624     AliasSet *FwdTo = Cur->Forward;
625     if (FwdTo) {
626       Cur->Forward = AliasAnyAS;
627       AliasAnyAS->addRef();
628       FwdTo->dropRef(*this);
629       continue;
630     }
631 
632     // Otherwise, perform the actual merge.
633     AliasAnyAS->mergeSetIn(*Cur, *this);
634   }
635 
636   return *AliasAnyAS;
637 }
638 
639 AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
640                                       AliasSet::AccessLattice E) {
641   AliasSet &AS = getAliasSetFor(Loc);
642   AS.Access |= E;
643 
644   if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
645     // The AST is now saturated. From here on, we conservatively consider all
646     // pointers to alias each-other.
647     return mergeAllAliasSets();
648   }
649 
650   return AS;
651 }
652 
653 //===----------------------------------------------------------------------===//
654 //               AliasSet/AliasSetTracker Printing Support
655 //===----------------------------------------------------------------------===//
656 
657 void AliasSet::print(raw_ostream &OS) const {
658   OS << "  AliasSet[" << (const void*)this << ", " << RefCount << "] ";
659   OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
660   switch (Access) {
661   case NoAccess:     OS << "No access "; break;
662   case RefAccess:    OS << "Ref       "; break;
663   case ModAccess:    OS << "Mod       "; break;
664   case ModRefAccess: OS << "Mod/Ref   "; break;
665   default: llvm_unreachable("Bad value for Access!");
666   }
667   if (Forward)
668     OS << " forwarding to " << (void*)Forward;
669 
670   if (!empty()) {
671     OS << "Pointers: ";
672     for (iterator I = begin(), E = end(); I != E; ++I) {
673       if (I != begin()) OS << ", ";
674       I.getPointer()->printAsOperand(OS << "(");
675       if (I.getSize() == LocationSize::afterPointer())
676         OS << ", unknown after)";
677       else if (I.getSize() == LocationSize::beforeOrAfterPointer())
678         OS << ", unknown before-or-after)";
679       else
680         OS << ", " << I.getSize() << ")";
681     }
682   }
683   if (!UnknownInsts.empty()) {
684     OS << "\n    " << UnknownInsts.size() << " Unknown instructions: ";
685     for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
686       if (i) OS << ", ";
687       if (auto *I = getUnknownInst(i)) {
688         if (I->hasName())
689           I->printAsOperand(OS);
690         else
691           I->print(OS);
692       }
693     }
694   }
695   OS << "\n";
696 }
697 
698 void AliasSetTracker::print(raw_ostream &OS) const {
699   OS << "Alias Set Tracker: " << AliasSets.size();
700   if (AliasAnyAS)
701     OS << " (Saturated)";
702   OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
703   for (const AliasSet &AS : *this)
704     AS.print(OS);
705   OS << "\n";
706 }
707 
708 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
709 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
710 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
711 #endif
712 
713 //===----------------------------------------------------------------------===//
714 //                     ASTCallbackVH Class Implementation
715 //===----------------------------------------------------------------------===//
716 
717 void AliasSetTracker::ASTCallbackVH::deleted() {
718   assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
719   AST->deleteValue(getValPtr());
720   // this now dangles!
721 }
722 
723 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
724   AST->copyValue(getValPtr(), V);
725 }
726 
727 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
728   : CallbackVH(V), AST(ast) {}
729 
730 AliasSetTracker::ASTCallbackVH &
731 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
732   return *this = ASTCallbackVH(V, AST);
733 }
734 
735 //===----------------------------------------------------------------------===//
736 //                            AliasSetPrinter Pass
737 //===----------------------------------------------------------------------===//
738 
739 namespace {
740 
741   class AliasSetPrinter : public FunctionPass {
742   public:
743     static char ID; // Pass identification, replacement for typeid
744 
745     AliasSetPrinter() : FunctionPass(ID) {
746       initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
747     }
748 
749     void getAnalysisUsage(AnalysisUsage &AU) const override {
750       AU.setPreservesAll();
751       AU.addRequired<AAResultsWrapperPass>();
752     }
753 
754     bool runOnFunction(Function &F) override {
755       auto &AAWP = getAnalysis<AAResultsWrapperPass>();
756       AliasSetTracker Tracker(AAWP.getAAResults());
757       errs() << "Alias sets for function '" << F.getName() << "':\n";
758       for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
759         Tracker.add(&*I);
760       Tracker.print(errs());
761       return false;
762     }
763   };
764 
765 } // end anonymous namespace
766 
767 char AliasSetPrinter::ID = 0;
768 
769 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
770                 "Alias Set Printer", false, true)
771 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
772 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
773                 "Alias Set Printer", false, true)
774 
775 AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream &OS) : OS(OS) {}
776 
777 PreservedAnalyses AliasSetsPrinterPass::run(Function &F,
778                                             FunctionAnalysisManager &AM) {
779   auto &AA = AM.getResult<AAManager>(F);
780   AliasSetTracker Tracker(AA);
781   OS << "Alias sets for function '" << F.getName() << "':\n";
782   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
783     Tracker.add(&*I);
784   Tracker.print(OS);
785   return PreservedAnalyses::all();
786 }
787