xref: /llvm-project/llvm/lib/Analysis/AliasSetTracker.cpp (revision 753c51bf889e605a2daf92e1710d7ad5ebc76ec3)
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/ADT/StringExtras.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/Function.h"
20 #include "llvm/IR/InstIterator.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/IR/PassManager.h"
24 #include "llvm/IR/PatternMatch.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/InitializePasses.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/AtomicOrdering.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 
35 using namespace llvm;
36 
37 static cl::opt<unsigned>
38     SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
39                         cl::init(250),
40                         cl::desc("The maximum number of pointers may-alias "
41                                  "sets may contain before degradation"));
42 
43 /// mergeSetIn - Merge the specified alias set into this alias set.
44 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST,
45                           BatchAAResults &BatchAA) {
46   assert(!AS.Forward && "Alias set is already forwarding!");
47   assert(!Forward && "This set is a forwarding set!!");
48 
49   bool WasMustAlias = (Alias == SetMustAlias);
50   // Update the alias and access types of this set...
51   Access |= AS.Access;
52   Alias  |= AS.Alias;
53 
54   if (Alias == SetMustAlias) {
55     // Check that these two merged sets really are must aliases.  Since both
56     // used to be must-alias sets, we can just check any pointer from each set
57     // for aliasing.
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 (!BatchAA.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         BatchAAResults &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, BatchAAResults &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 (Instruction *Inst : UnknownInsts)
221       if (isModOrRefSet(
222               AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
223         return AliasResult::MayAlias;
224   }
225 
226   return AliasResult::NoAlias;
227 }
228 
229 ModRefInfo AliasSet::aliasesUnknownInst(const Instruction *Inst,
230                                         BatchAAResults &AA) const {
231 
232   if (AliasAny)
233     return ModRefInfo::ModRef;
234 
235   if (!Inst->mayReadOrWriteMemory())
236     return ModRefInfo::NoModRef;
237 
238   for (Instruction *UnknownInst : UnknownInsts) {
239     const auto *C1 = dyn_cast<CallBase>(UnknownInst);
240     const auto *C2 = dyn_cast<CallBase>(Inst);
241     if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
242         isModOrRefSet(AA.getModRefInfo(C2, C1))) {
243       // TODO: Could be more precise, but not really useful right now.
244       return ModRefInfo::ModRef;
245     }
246   }
247 
248   ModRefInfo MR = ModRefInfo::NoModRef;
249   for (iterator I = begin(), E = end(); I != E; ++I) {
250     MR |= AA.getModRefInfo(
251         Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()));
252     if (isModAndRefSet(MR))
253       return MR;
254   }
255 
256   return MR;
257 }
258 
259 void AliasSetTracker::clear() {
260   // Delete all the PointerRec entries.
261   for (auto &I : PointerMap)
262     I.second->eraseFromList();
263 
264   PointerMap.clear();
265 
266   // The alias sets should all be clear now.
267   AliasSets.clear();
268 }
269 
270 /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
271 /// alias the pointer. Return the unified set, or nullptr if no set that aliases
272 /// the pointer was found. MustAliasAll is updated to true/false if the pointer
273 /// is found to MustAlias all the sets it merged.
274 AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
275                                                     LocationSize Size,
276                                                     const AAMDNodes &AAInfo,
277                                                     bool &MustAliasAll) {
278   AliasSet *FoundSet = nullptr;
279   MustAliasAll = true;
280   for (AliasSet &AS : llvm::make_early_inc_range(*this)) {
281     if (AS.Forward)
282       continue;
283 
284     AliasResult AR = AS.aliasesPointer(Ptr, Size, AAInfo, AA);
285     if (AR == AliasResult::NoAlias)
286       continue;
287 
288     if (AR != AliasResult::MustAlias)
289       MustAliasAll = false;
290 
291     if (!FoundSet) {
292       // If this is the first alias set ptr can go into, remember it.
293       FoundSet = &AS;
294     } else {
295       // Otherwise, we must merge the sets.
296       FoundSet->mergeSetIn(AS, *this, AA);
297     }
298   }
299 
300   return FoundSet;
301 }
302 
303 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
304   AliasSet *FoundSet = nullptr;
305   for (AliasSet &AS : llvm::make_early_inc_range(*this)) {
306     if (AS.Forward || !isModOrRefSet(AS.aliasesUnknownInst(Inst, AA)))
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, AA);
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 
354       // For MustAlias sets, also update Size/AAInfo of the representative
355       // pointer.
356       AliasSet &AS = *Entry.getAliasSet(*this);
357       if (AS.isMustAlias())
358         if (AliasSet::PointerRec *P = AS.getSomePointer())
359           P->updateSizeAndAAInfo(Size, AAInfo);
360     }
361     // Return the set!
362     return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
363   }
364 
365   if (AliasSet *AS =
366           mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
367     // Add it to the alias set it aliases.
368     AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll);
369     return *AS;
370   }
371 
372   // Otherwise create a new alias set to hold the loaded pointer.
373   AliasSets.push_back(new AliasSet());
374   AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true);
375   return AliasSets.back();
376 }
377 
378 void AliasSetTracker::add(Value *Ptr, LocationSize Size,
379                           const AAMDNodes &AAInfo) {
380   addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
381 }
382 
383 void AliasSetTracker::add(LoadInst *LI) {
384   if (isStrongerThanMonotonic(LI->getOrdering()))
385     return addUnknown(LI);
386   addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
387 }
388 
389 void AliasSetTracker::add(StoreInst *SI) {
390   if (isStrongerThanMonotonic(SI->getOrdering()))
391     return addUnknown(SI);
392   addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
393 }
394 
395 void AliasSetTracker::add(VAArgInst *VAAI) {
396   addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
397 }
398 
399 void AliasSetTracker::add(AnyMemSetInst *MSI) {
400   addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
401 }
402 
403 void AliasSetTracker::add(AnyMemTransferInst *MTI) {
404   addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
405   addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
406 }
407 
408 void AliasSetTracker::addUnknown(Instruction *Inst) {
409   if (isa<DbgInfoIntrinsic>(Inst))
410     return; // Ignore DbgInfo Intrinsics.
411 
412   if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
413     // These intrinsics will show up as affecting memory, but they are just
414     // markers.
415     switch (II->getIntrinsicID()) {
416     default:
417       break;
418       // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
419     case Intrinsic::assume:
420     case Intrinsic::experimental_noalias_scope_decl:
421     case Intrinsic::sideeffect:
422     case Intrinsic::pseudoprobe:
423       return;
424     }
425   }
426   if (!Inst->mayReadOrWriteMemory())
427     return; // doesn't alias anything
428 
429   if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
430     AS->addUnknownInst(Inst, AA);
431     return;
432   }
433   AliasSets.push_back(new AliasSet());
434   AliasSets.back().addUnknownInst(Inst, AA);
435 }
436 
437 void AliasSetTracker::add(Instruction *I) {
438   // Dispatch to one of the other add methods.
439   if (LoadInst *LI = dyn_cast<LoadInst>(I))
440     return add(LI);
441   if (StoreInst *SI = dyn_cast<StoreInst>(I))
442     return add(SI);
443   if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
444     return add(VAAI);
445   if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
446     return add(MSI);
447   if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
448     return add(MTI);
449 
450   // Handle all calls with known mod/ref sets genericall
451   if (auto *Call = dyn_cast<CallBase>(I))
452     if (Call->onlyAccessesArgMemory()) {
453       auto getAccessFromModRef = [](ModRefInfo MRI) {
454         if (isRefSet(MRI) && isModSet(MRI))
455           return AliasSet::ModRefAccess;
456         else if (isModSet(MRI))
457           return AliasSet::ModAccess;
458         else if (isRefSet(MRI))
459           return AliasSet::RefAccess;
460         else
461           return AliasSet::NoAccess;
462       };
463 
464       ModRefInfo CallMask = AA.getMemoryEffects(Call).getModRef();
465 
466       // Some intrinsics are marked as modifying memory for control flow
467       // modelling purposes, but don't actually modify any specific memory
468       // location.
469       using namespace PatternMatch;
470       if (Call->use_empty() &&
471           match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
472         CallMask &= ModRefInfo::Ref;
473 
474       for (auto IdxArgPair : enumerate(Call->args())) {
475         int ArgIdx = IdxArgPair.index();
476         const Value *Arg = IdxArgPair.value();
477         if (!Arg->getType()->isPointerTy())
478           continue;
479         MemoryLocation ArgLoc =
480             MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
481         ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
482         ArgMask &= CallMask;
483         if (!isNoModRef(ArgMask))
484           addPointer(ArgLoc, getAccessFromModRef(ArgMask));
485       }
486       return;
487     }
488 
489   return addUnknown(I);
490 }
491 
492 void AliasSetTracker::add(BasicBlock &BB) {
493   for (auto &I : BB)
494     add(&I);
495 }
496 
497 void AliasSetTracker::add(const AliasSetTracker &AST) {
498   assert(&AA == &AST.AA &&
499          "Merging AliasSetTracker objects with different Alias Analyses!");
500 
501   // Loop over all of the alias sets in AST, adding the pointers contained
502   // therein into the current alias sets.  This can cause alias sets to be
503   // merged together in the current AST.
504   for (const AliasSet &AS : AST) {
505     if (AS.Forward)
506       continue; // Ignore forwarding alias sets
507 
508     // If there are any call sites in the alias set, add them to this AST.
509     for (Instruction *Inst : AS.UnknownInsts)
510       add(Inst);
511 
512     // Loop over all of the pointers in this alias set.
513     for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
514       addPointer(
515           MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
516           (AliasSet::AccessLattice)AS.Access);
517   }
518 }
519 
520 AliasSet &AliasSetTracker::mergeAllAliasSets() {
521   assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
522          "Full merge should happen once, when the saturation threshold is "
523          "reached");
524 
525   // Collect all alias sets, so that we can drop references with impunity
526   // without worrying about iterator invalidation.
527   std::vector<AliasSet *> ASVector;
528   ASVector.reserve(SaturationThreshold);
529   for (AliasSet &AS : *this)
530     ASVector.push_back(&AS);
531 
532   // Copy all instructions and pointers into a new set, and forward all other
533   // sets to it.
534   AliasSets.push_back(new AliasSet());
535   AliasAnyAS = &AliasSets.back();
536   AliasAnyAS->Alias = AliasSet::SetMayAlias;
537   AliasAnyAS->Access = AliasSet::ModRefAccess;
538   AliasAnyAS->AliasAny = true;
539 
540   for (auto *Cur : ASVector) {
541     // If Cur was already forwarding, just forward to the new AS instead.
542     AliasSet *FwdTo = Cur->Forward;
543     if (FwdTo) {
544       Cur->Forward = AliasAnyAS;
545       AliasAnyAS->addRef();
546       FwdTo->dropRef(*this);
547       continue;
548     }
549 
550     // Otherwise, perform the actual merge.
551     AliasAnyAS->mergeSetIn(*Cur, *this, AA);
552   }
553 
554   return *AliasAnyAS;
555 }
556 
557 AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
558                                       AliasSet::AccessLattice E) {
559   AliasSet &AS = getAliasSetFor(Loc);
560   AS.Access |= E;
561 
562   if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
563     // The AST is now saturated. From here on, we conservatively consider all
564     // pointers to alias each-other.
565     return mergeAllAliasSets();
566   }
567 
568   return AS;
569 }
570 
571 //===----------------------------------------------------------------------===//
572 //               AliasSet/AliasSetTracker Printing Support
573 //===----------------------------------------------------------------------===//
574 
575 void AliasSet::print(raw_ostream &OS) const {
576   OS << "  AliasSet[" << (const void*)this << ", " << RefCount << "] ";
577   OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
578   switch (Access) {
579   case NoAccess:     OS << "No access "; break;
580   case RefAccess:    OS << "Ref       "; break;
581   case ModAccess:    OS << "Mod       "; break;
582   case ModRefAccess: OS << "Mod/Ref   "; break;
583   default: llvm_unreachable("Bad value for Access!");
584   }
585   if (Forward)
586     OS << " forwarding to " << (void*)Forward;
587 
588   if (!empty()) {
589     OS << "Pointers: ";
590     for (iterator I = begin(), E = end(); I != E; ++I) {
591       if (I != begin()) OS << ", ";
592       I.getPointer()->printAsOperand(OS << "(");
593       if (I.getSize() == LocationSize::afterPointer())
594         OS << ", unknown after)";
595       else if (I.getSize() == LocationSize::beforeOrAfterPointer())
596         OS << ", unknown before-or-after)";
597       else
598         OS << ", " << I.getSize() << ")";
599     }
600   }
601   if (!UnknownInsts.empty()) {
602     ListSeparator LS;
603     OS << "\n    " << UnknownInsts.size() << " Unknown instructions: ";
604     for (Instruction *I : UnknownInsts) {
605       OS << LS;
606       if (I->hasName())
607         I->printAsOperand(OS);
608       else
609         I->print(OS);
610     }
611   }
612   OS << "\n";
613 }
614 
615 void AliasSetTracker::print(raw_ostream &OS) const {
616   OS << "Alias Set Tracker: " << AliasSets.size();
617   if (AliasAnyAS)
618     OS << " (Saturated)";
619   OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
620   for (const AliasSet &AS : *this)
621     AS.print(OS);
622   OS << "\n";
623 }
624 
625 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
626 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
627 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
628 #endif
629 
630 //===----------------------------------------------------------------------===//
631 //                            AliasSetPrinter Pass
632 //===----------------------------------------------------------------------===//
633 
634 AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream &OS) : OS(OS) {}
635 
636 PreservedAnalyses AliasSetsPrinterPass::run(Function &F,
637                                             FunctionAnalysisManager &AM) {
638   auto &AA = AM.getResult<AAManager>(F);
639   BatchAAResults BatchAA(AA);
640   AliasSetTracker Tracker(BatchAA);
641   OS << "Alias sets for function '" << F.getName() << "':\n";
642   for (Instruction &I : instructions(F))
643     Tracker.add(&I);
644   Tracker.print(OS);
645   return PreservedAnalyses::all();
646 }
647