1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the AliasSetTracker and AliasSet classes.
11 //===----------------------------------------------------------------------===//
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"
45 static cl::opt<unsigned>
46 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
48 cl::desc("The maximum number of pointers may-alias "
49 "sets may contain before degradation"));
51 /// mergeSetIn - Merge the specified alias set into this alias set.
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!!");
57 bool WasMustAlias = (Alias == SetMustAlias);
58 // Update the alias and access types of this set...
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
66 AliasAnalysis &AA = AST.getAliasAnalysis();
67 PointerRec *L = getSomePointer();
68 PointerRec *R = AS.getSomePointer();
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())) !=
77 if (Alias == SetMayAlias) {
79 AST.TotalMayAliasSetSize += size();
80 if (AS.Alias == SetMustAlias)
81 AST.TotalMayAliasSetSize += AS.size();
84 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
85 if (UnknownInsts.empty()) { // Merge call sites...
86 if (ASHadUnknownInsts) {
87 std::swap(UnknownInsts, AS.UnknownInsts);
90 } else if (ASHadUnknownInsts) {
91 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
92 AS.UnknownInsts.clear();
95 AS.Forward = this; // Forward across AS now...
96 addRef(); // AS is now pointing to us...
98 // Merge the list of constituent pointers...
100 SetSize += AS.size();
102 *PtrListEnd = AS.PtrList;
103 AS.PtrList->setPrevInList(PtrListEnd);
104 PtrListEnd = AS.PtrListEnd;
106 AS.PtrList = nullptr;
107 AS.PtrListEnd = &AS.PtrList;
108 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
110 if (ASHadUnknownInsts)
114 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
115 if (AliasSet *Fwd = AS->Forward) {
117 AS->Forward = nullptr;
118 } else // Update TotalMayAliasSetSize only if not forwarding.
119 if (AS->Alias == AliasSet::SetMayAlias)
120 TotalMayAliasSetSize -= AS->size();
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");
131 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
132 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
133 AST.removeAliasSet(this);
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!");
141 // Check to see if we have to downgrade to _may_ alias.
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) {
151 AST.TotalMayAliasSetSize += size();
153 assert(Result != NoAlias && "Cannot be part of must set!");
154 } else if (!SkipSizeUpdate)
155 P->updateSizeAndAAInfo(Size, AAInfo);
158 Entry.setAliasSet(this);
159 Entry.updateSizeAndAAInfo(Size, AAInfo);
161 // Add it to the end of the list...
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.
170 if (Alias == SetMayAlias)
171 AST.TotalMayAliasSetSize++;
174 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
175 if (UnknownInsts.empty())
177 UnknownInsts.emplace_back(I);
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) {
190 // FIXME: This should use mod/ref information to make this not suck so bad
192 Access = ModRefAccess;
195 /// aliasesPointer - If the specified pointer "may" (or must) alias one of the
196 /// members in the set return the appropriate AliasResult. Otherwise return
199 AliasResult AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
200 const AAMDNodes &AAInfo,
201 AliasAnalysis &AA) const {
205 if (Alias == SetMustAlias) {
206 assert(UnknownInsts.empty() && "Illegal must alias set!");
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));
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())))
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))
230 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
237 bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
238 AliasAnalysis &AA) const {
243 assert(Inst->mayReadOrWriteMemory() &&
244 "Instruction must either read or write memory.");
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)))
256 for (iterator I = begin(), E = end(); I != E; ++I)
257 if (isModOrRefSet(AA.getModRefInfo(
258 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
264 Instruction* AliasSet::getUniqueInstruction() {
266 // May have collapses alias set
268 if (begin() != end()) {
269 if (!UnknownInsts.empty())
270 // Another instruction found
272 if (std::next(begin()) != end())
273 // Another instruction found
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
282 return cast<Instruction>(*(Addr->user_begin()));
284 if (1 != UnknownInsts.size())
286 return cast<Instruction>(UnknownInsts[0]);
289 void AliasSetTracker::clear() {
290 // Delete all the PointerRec entries.
291 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
293 I->second->eraseFromList();
297 // The alias sets should all be clear now.
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,
307 const AAMDNodes &AAInfo,
308 bool &MustAliasAll) {
309 AliasSet *FoundSet = nullptr;
310 AliasResult AllAR = MustAlias;
311 for (iterator I = begin(), E = end(); I != E;) {
316 AliasResult AR = Cur->aliasesPointer(Ptr, Size, AAInfo, AA);
321 AliasResult(AllAR & AR); // Possible downgrade to May/Partial, even No
324 // If this is the first alias set ptr can go into, remember it.
327 // Otherwise, we must merge the sets.
328 FoundSet->mergeSetIn(*Cur, *this);
332 MustAliasAll = (AllAR == MustAlias);
336 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
337 AliasSet *FoundSet = nullptr;
338 for (iterator I = begin(), E = end(); I != E;) {
340 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
343 // If this is the first alias set ptr can go into, remember it.
346 // Otherwise, we must merge the sets.
347 FoundSet->mergeSetIn(*Cur, *this);
353 AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
355 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
356 const LocationSize Size = MemLoc.Size;
357 const AAMDNodes &AAInfo = MemLoc.AATags;
359 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
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
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");
372 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
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);
388 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
392 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
393 // Add it to the alias set it aliases.
394 AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll);
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();
404 void AliasSetTracker::add(Value *Ptr, LocationSize Size,
405 const AAMDNodes &AAInfo) {
406 addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
409 void AliasSetTracker::add(LoadInst *LI) {
410 if (isStrongerThanMonotonic(LI->getOrdering()))
411 return addUnknown(LI);
412 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
415 void AliasSetTracker::add(StoreInst *SI) {
416 if (isStrongerThanMonotonic(SI->getOrdering()))
417 return addUnknown(SI);
418 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
421 void AliasSetTracker::add(VAArgInst *VAAI) {
422 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
425 void AliasSetTracker::add(AnyMemSetInst *MSI) {
426 addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
429 void AliasSetTracker::add(AnyMemTransferInst *MTI) {
430 addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
431 addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
434 void AliasSetTracker::addUnknown(Instruction *Inst) {
435 if (isa<DbgInfoIntrinsic>(Inst))
436 return; // Ignore DbgInfo Intrinsics.
438 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
439 // These intrinsics will show up as affecting memory, but they are just
441 switch (II->getIntrinsicID()) {
444 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
445 case Intrinsic::assume:
446 case Intrinsic::sideeffect:
450 if (!Inst->mayReadOrWriteMemory())
451 return; // doesn't alias anything
453 if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
454 AS->addUnknownInst(Inst, AA);
457 AliasSets.push_back(new AliasSet());
458 AliasSets.back().addUnknownInst(Inst, AA);
461 void AliasSetTracker::add(Instruction *I) {
462 // Dispatch to one of the other add methods.
463 if (LoadInst *LI = dyn_cast<LoadInst>(I))
465 if (StoreInst *SI = dyn_cast<StoreInst>(I))
467 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
469 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
471 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
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;
485 return AliasSet::NoAccess;
488 ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call));
490 // Some intrinsics are marked as modifying memory for control flow
491 // modelling purposes, but don't actually modify any specific memory
493 using namespace PatternMatch;
494 if (Call->use_empty() &&
495 match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
496 CallMask = clearMod(CallMask);
498 for (auto IdxArgPair : enumerate(Call->args())) {
499 int ArgIdx = IdxArgPair.index();
500 const Value *Arg = IdxArgPair.value();
501 if (!Arg->getType()->isPointerTy())
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));
513 return addUnknown(I);
516 void AliasSetTracker::add(BasicBlock &BB) {
521 void AliasSetTracker::add(const AliasSetTracker &AST) {
522 assert(&AA == &AST.AA &&
523 "Merging AliasSetTracker objects with different Alias Analyses!");
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) {
530 continue; // Ignore forwarding alias sets
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))
537 // Loop over all of the pointers in this alias set.
538 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
540 MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
541 (AliasSet::AccessLattice)AS.Access);
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());
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.
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
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);
568 // Unlink and delete from the list of values.
569 PtrValEnt->eraseFromList();
571 if (AS->Alias == AliasSet::SetMayAlias) {
573 TotalMayAliasSetSize--;
576 // Stop using the alias set.
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.
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())
592 assert(I->second->hasAliasSet() && "Dead entry?");
594 AliasSet::PointerRec &Entry = getEntryFor(To);
595 if (Entry.hasAliasSet()) return; // Already in the tracker!
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(),
605 AliasSet &AliasSetTracker::mergeAllAliasSets() {
606 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
607 "Full merge should happen once, when the saturation threshold is "
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);
617 // Copy all instructions and pointers into a new set, and forward all other
619 AliasSets.push_back(new AliasSet());
620 AliasAnyAS = &AliasSets.back();
621 AliasAnyAS->Alias = AliasSet::SetMayAlias;
622 AliasAnyAS->Access = AliasSet::ModRefAccess;
623 AliasAnyAS->AliasAny = true;
625 for (auto Cur : ASVector) {
626 // If Cur was already forwarding, just forward to the new AS instead.
627 AliasSet *FwdTo = Cur->Forward;
629 Cur->Forward = AliasAnyAS;
630 AliasAnyAS->addRef();
631 FwdTo->dropRef(*this);
635 // Otherwise, perform the actual merge.
636 AliasAnyAS->mergeSetIn(*Cur, *this);
642 AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
643 AliasSet::AccessLattice E) {
644 AliasSet &AS = getAliasSetFor(Loc);
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();
656 //===----------------------------------------------------------------------===//
657 // AliasSet/AliasSetTracker Printing Support
658 //===----------------------------------------------------------------------===//
660 void AliasSet::print(raw_ostream &OS) const {
661 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
662 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
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!");
671 OS << " forwarding to " << (void*)Forward;
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())
681 OS << ", " << I.getSize() << ")";
684 if (!UnknownInsts.empty()) {
685 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
686 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
688 if (auto *I = getUnknownInst(i)) {
690 I->printAsOperand(OS);
699 void AliasSetTracker::print(raw_ostream &OS) const {
700 OS << "Alias Set Tracker: " << AliasSets.size();
702 OS << " (Saturated)";
703 OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
704 for (const AliasSet &AS : *this)
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()); }
714 //===----------------------------------------------------------------------===//
715 // ASTCallbackVH Class Implementation
716 //===----------------------------------------------------------------------===//
718 void AliasSetTracker::ASTCallbackVH::deleted() {
719 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
720 AST->deleteValue(getValPtr());
724 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
725 AST->copyValue(getValPtr(), V);
728 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
729 : CallbackVH(V), AST(ast) {}
731 AliasSetTracker::ASTCallbackVH &
732 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
733 return *this = ASTCallbackVH(V, AST);
736 //===----------------------------------------------------------------------===//
737 // AliasSetPrinter Pass
738 //===----------------------------------------------------------------------===//
742 class AliasSetPrinter : public FunctionPass {
743 AliasSetTracker *Tracker;
746 static char ID; // Pass identification, replacement for typeid
748 AliasSetPrinter() : FunctionPass(ID) {
749 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
752 void getAnalysisUsage(AnalysisUsage &AU) const override {
753 AU.setPreservesAll();
754 AU.addRequired<AAResultsWrapperPass>();
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)
763 Tracker->print(errs());
769 } // end anonymous namespace
771 char AliasSetPrinter::ID = 0;
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)