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