1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AliasSetTracker and AliasSet classes.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/AliasSetTracker.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/InstIterator.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
29 /// mergeSetIn - Merge the specified alias set into this alias set.
31 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
32 assert(!AS.Forward && "Alias set is already forwarding!");
33 assert(!Forward && "This set is a forwarding set!!");
35 // Update the alias and access types of this set...
38 Volatile |= AS.Volatile;
40 if (Alias == SetMustAlias) {
41 // Check that these two merged sets really are must aliases. Since both
42 // used to be must-alias sets, we can just check any pointer from each set
44 AliasAnalysis &AA = AST.getAliasAnalysis();
45 PointerRec *L = getSomePointer();
46 PointerRec *R = AS.getSomePointer();
48 // If the pointers are not a must-alias pair, this set becomes a may alias.
49 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
50 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
55 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
56 if (UnknownInsts.empty()) { // Merge call sites...
57 if (ASHadUnknownInsts) {
58 std::swap(UnknownInsts, AS.UnknownInsts);
61 } else if (ASHadUnknownInsts) {
62 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
63 AS.UnknownInsts.clear();
66 AS.Forward = this; // Forward across AS now...
67 addRef(); // AS is now pointing to us...
69 // Merge the list of constituent pointers...
71 *PtrListEnd = AS.PtrList;
72 AS.PtrList->setPrevInList(PtrListEnd);
73 PtrListEnd = AS.PtrListEnd;
76 AS.PtrListEnd = &AS.PtrList;
77 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
79 if (ASHadUnknownInsts)
83 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
84 if (AliasSet *Fwd = AS->Forward) {
86 AS->Forward = nullptr;
91 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
92 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
93 AST.removeAliasSet(this);
96 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
97 uint64_t Size, const AAMDNodes &AAInfo,
98 ModRefInfo MR, bool KnownMustAlias) {
99 assert(!Entry.hasAliasSet() && "Entry already in set!");
101 // Check to see if we have to downgrade to _may_ alias.
102 if (isMustAlias() && !KnownMustAlias)
103 if (PointerRec *P = getSomePointer()) {
104 AliasAnalysis &AA = AST.getAliasAnalysis();
106 AA.alias(MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
107 MemoryLocation(Entry.getValue(), Size, AAInfo));
108 if (Result != MustAlias)
110 else // First entry of must alias must have maximum size!
111 P->updateSizeAndAAInfo(Size, AAInfo);
112 assert(Result != NoAlias && "Cannot be part of must set!");
115 // upgrading access if existing UnknownInst has ModRef with new pointer
118 Entry.setAliasSet(this);
119 Entry.updateSizeAndAAInfo(Size, AAInfo);
121 // Add it to the end of the list...
122 assert(*PtrListEnd == nullptr && "End of list is not null?");
123 *PtrListEnd = &Entry;
124 PtrListEnd = Entry.setPrevInList(PtrListEnd);
125 assert(*PtrListEnd == nullptr && "End of list is not null?");
126 addRef(); // Entry points to alias set.
129 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA,
131 if (UnknownInsts.empty())
133 UnknownInsts.emplace_back(I);
135 if (!I->mayWriteToMemory()) {
146 /// returns true if there is no request of worst ModRefInfo
147 /// (MRcommonPtr is null)
148 /// or when achieved maximum ModRefInfo (MRI_ModRef).
149 bool processMR(ModRefInfo MR, ModRefInfo* MRcommonPtr, ModRefInfo& MRcommon) {
150 MRcommon = ModRefInfo(MRcommon | MR);
151 return !MRcommonPtr || (MRcommon == MRI_ModRef);
153 bool fillExitMR(ModRefInfo* MRcommonPtr, ModRefInfo& MRcommon) {
154 if (MRcommonPtr) *MRcommonPtr = MRcommon;
155 return MRcommon != MRI_NoModRef;
159 /// aliasesPointer - Return true if the specified pointer "may" (or must)
160 /// alias one of the members in the set.
162 bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
163 const AAMDNodes &AAInfo,
165 ModRefInfo* MRcommonPtr) const {
166 if (Alias == SetMustAlias) {
167 assert(UnknownInsts.empty() && "Illegal must alias set!");
169 // If this is a set of MustAliases, only check to see if the pointer aliases
170 // SOME value in the set.
171 PointerRec *SomePtr = getSomePointer();
172 assert(SomePtr && "Empty must-alias set??");
173 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
174 SomePtr->getAAInfo()),
175 MemoryLocation(Ptr, Size, AAInfo));
178 // If this is a may-alias set, we have to check all of the pointers in the set
179 // to be sure it doesn't alias the set...
180 for (iterator I = begin(), E = end(); I != E; ++I)
181 if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
182 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
185 // to gather worst ModRefInfo
186 ModRefInfo MRcommon = MRI_NoModRef;
188 // Check the unknown instructions...
189 if (!UnknownInsts.empty()) {
190 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
191 if (processMR(AA.getModRefInfo(UnknownInsts[i],
192 MemoryLocation(Ptr, Size, AAInfo)), MRcommonPtr, MRcommon))
193 return fillExitMR(MRcommonPtr, MRcommon);
196 return fillExitMR(MRcommonPtr, MRcommon);
199 bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
201 ModRefInfo* MRcommonPtr) const {
202 if (!Inst->mayReadOrWriteMemory())
205 // to gather worst ModRefInfo
206 ModRefInfo MRcommon = MRI_NoModRef;
208 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
209 ImmutableCallSite C1(getUnknownInst(i)), C2(Inst);
211 processMR(AA.getModRefInfo(C1, C2), MRcommonPtr, MRcommon) ||
212 processMR(AA.getModRefInfo(C2, C1), MRcommonPtr, MRcommon))
213 return fillExitMR(MRcommonPtr, MRcommon);
216 for (iterator I = begin(), E = end(); I != E; ++I) {
217 ModRefInfo MR = AA.getModRefInfo(
218 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()));
219 if (processMR(MR, MRcommonPtr, MRcommon))
220 return fillExitMR(MRcommonPtr, MRcommon);
222 return fillExitMR(MRcommonPtr, MRcommon);
225 void AliasSetTracker::clear() {
226 // Delete all the PointerRec entries.
227 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
229 I->second->eraseFromList();
233 // The alias sets should all be clear now.
238 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
239 /// instruction referring to the pointer into. If there are multiple alias sets
240 /// that may alias the pointer, merge them together and return the unified set.
242 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
244 const AAMDNodes &AAInfo,
245 ModRefInfo* MRcommonPtr) {
246 AliasSet *FoundSet = nullptr;
247 for (iterator I = begin(), E = end(); I != E;) {
249 ModRefInfo MR = MRI_NoModRef;
250 if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA, &MR))
252 *MRcommonPtr = ModRefInfo(*MRcommonPtr | MR);
254 if (!FoundSet) { // If this is the first alias set ptr can go into.
255 FoundSet = &*Cur; // Remember it.
256 } else { // Otherwise, we must merge the sets.
257 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
264 /// containsPointer - Return true if the specified location is represented by
265 /// this alias set, false otherwise. This does not modify the AST object or
267 bool AliasSetTracker::containsPointer(const Value *Ptr, uint64_t Size,
268 const AAMDNodes &AAInfo) const {
269 for (const_iterator I = begin(), E = end(); I != E; ++I)
270 if (!I->Forward && I->aliasesPointer(Ptr, Size, AAInfo, AA))
275 bool AliasSetTracker::containsUnknown(const Instruction *Inst) const {
276 for (const_iterator I = begin(), E = end(); I != E; ++I)
277 if (!I->Forward && I->aliasesUnknownInst(Inst, AA))
282 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst,
283 ModRefInfo* MRcommonPtr) {
284 AliasSet *FoundSet = nullptr;
285 for (iterator I = begin(), E = end(); I != E;) {
287 ModRefInfo MR = MRI_NoModRef;
288 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA, &MR))
290 *MRcommonPtr = ModRefInfo(*MRcommonPtr | MR);
291 if (!FoundSet) // If this is the first alias set ptr can go into.
292 FoundSet = &*Cur; // Remember it.
293 else if (!Cur->Forward) // Otherwise, we must merge the sets.
294 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
302 /// getAliasSetForPointer - Return the alias set that the specified pointer
304 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
305 const AAMDNodes &AAInfo,
307 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
309 // Check to see if the pointer is already known.
310 if (Entry.hasAliasSet()) {
311 Entry.updateSizeAndAAInfo(Size, AAInfo);
313 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
316 ModRefInfo MR = MRI_NoModRef;
317 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, AAInfo, &MR)) {
318 // Add it to the alias set it aliases.
319 AS->addPointer(*this, Entry, Size, AAInfo, MR);
323 if (New) *New = true;
324 // Otherwise create a new alias set to hold the loaded pointer.
325 AliasSets.push_back(new AliasSet());
326 AliasSets.back().addPointer(*this, Entry, Size, AAInfo, MRI_NoModRef);
327 return AliasSets.back();
330 bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo) {
332 addPointer(Ptr, Size, AAInfo, MRI_NoModRef, NewPtr);
337 bool AliasSetTracker::add(LoadInst *LI) {
338 if (LI->getOrdering() > Monotonic) return addUnknown(LI);
341 LI->getAAMetadata(AAInfo);
343 AliasSet::AccessLattice Access = MRI_Ref;
345 const DataLayout &DL = LI->getModule()->getDataLayout();
346 AliasSet &AS = addPointer(LI->getOperand(0),
347 DL.getTypeStoreSize(LI->getType()),
348 AAInfo, Access, NewPtr);
349 if (LI->isVolatile()) AS.setVolatile();
353 bool AliasSetTracker::add(StoreInst *SI) {
354 if (SI->getOrdering() > Monotonic) return addUnknown(SI);
357 SI->getAAMetadata(AAInfo);
359 AliasSet::AccessLattice Access = MRI_Mod;
361 const DataLayout &DL = SI->getModule()->getDataLayout();
362 Value *Val = SI->getOperand(0);
363 AliasSet &AS = addPointer(SI->getOperand(1),
364 DL.getTypeStoreSize(Val->getType()),
365 AAInfo, Access, NewPtr);
366 if (SI->isVolatile()) AS.setVolatile();
370 bool AliasSetTracker::add(VAArgInst *VAAI) {
372 VAAI->getAAMetadata(AAInfo);
375 addPointer(VAAI->getOperand(0), MemoryLocation::UnknownSize, AAInfo,
381 bool AliasSetTracker::addUnknown(Instruction *Inst) {
382 if (isa<DbgInfoIntrinsic>(Inst))
383 return true; // Ignore DbgInfo Intrinsics.
384 if (!Inst->mayReadOrWriteMemory())
385 return true; // doesn't alias anything
387 ModRefInfo MR = MRI_NoModRef;
388 AliasSet *AS = findAliasSetForUnknownInst(Inst, &MR);
390 AS->addUnknownInst(Inst, AA, MR);
393 AliasSets.push_back(new AliasSet());
394 AS = &AliasSets.back();
395 AS->addUnknownInst(Inst, AA, MR);
399 bool AliasSetTracker::add(Instruction *I) {
400 // Dispatch to one of the other add methods.
401 if (LoadInst *LI = dyn_cast<LoadInst>(I))
403 if (StoreInst *SI = dyn_cast<StoreInst>(I))
405 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
407 return addUnknown(I);
410 void AliasSetTracker::add(BasicBlock &BB) {
415 void AliasSetTracker::add(const AliasSetTracker &AST) {
416 assert(&AA == &AST.AA &&
417 "Merging AliasSetTracker objects with different Alias Analyses!");
419 // Loop over all of the alias sets in AST, adding the pointers contained
420 // therein into the current alias sets. This can cause alias sets to be
421 // merged together in the current AST.
422 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
423 if (I->Forward) continue; // Ignore forwarding alias sets
425 AliasSet &AS = const_cast<AliasSet&>(*I);
427 // If there are any call sites in the alias set, add them to this AST.
428 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
429 add(AS.UnknownInsts[i]);
431 // Loop over all of the pointers in this alias set.
433 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
434 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
436 (AliasSet::AccessLattice)AS.Access, X);
437 if (AS.isVolatile()) NewAS.setVolatile();
442 /// remove - Remove the specified (potentially non-empty) alias set from the
444 void AliasSetTracker::remove(AliasSet &AS) {
445 // Drop all call sites.
446 if (!AS.UnknownInsts.empty())
448 AS.UnknownInsts.clear();
450 // Clear the alias set.
451 unsigned NumRefs = 0;
452 while (!AS.empty()) {
453 AliasSet::PointerRec *P = AS.PtrList;
455 Value *ValToRemove = P->getValue();
457 // Unlink and delete entry from the list of values.
460 // Remember how many references need to be dropped.
463 // Finally, remove the entry.
464 PointerMap.erase(ValToRemove);
467 // Stop using the alias set, removing it.
468 AS.RefCount -= NumRefs;
469 if (AS.RefCount == 0)
470 AS.removeFromTracker(*this);
474 AliasSetTracker::remove(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo) {
475 AliasSet *AS = findAliasSetForPointer(Ptr, Size, AAInfo);
476 if (!AS) return false;
481 bool AliasSetTracker::remove(LoadInst *LI) {
482 const DataLayout &DL = LI->getModule()->getDataLayout();
483 uint64_t Size = DL.getTypeStoreSize(LI->getType());
486 LI->getAAMetadata(AAInfo);
488 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, AAInfo);
489 if (!AS) return false;
494 bool AliasSetTracker::remove(StoreInst *SI) {
495 const DataLayout &DL = SI->getModule()->getDataLayout();
496 uint64_t Size = DL.getTypeStoreSize(SI->getOperand(0)->getType());
499 SI->getAAMetadata(AAInfo);
501 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, AAInfo);
502 if (!AS) return false;
507 bool AliasSetTracker::remove(VAArgInst *VAAI) {
509 VAAI->getAAMetadata(AAInfo);
511 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
512 MemoryLocation::UnknownSize, AAInfo);
513 if (!AS) return false;
518 bool AliasSetTracker::removeUnknown(Instruction *I) {
519 if (!I->mayReadOrWriteMemory())
520 return false; // doesn't alias anything
522 AliasSet *AS = findAliasSetForUnknownInst(I);
523 if (!AS) return false;
528 bool AliasSetTracker::remove(Instruction *I) {
529 // Dispatch to one of the other remove methods...
530 if (LoadInst *LI = dyn_cast<LoadInst>(I))
532 if (StoreInst *SI = dyn_cast<StoreInst>(I))
534 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
536 return removeUnknown(I);
540 // deleteValue method - This method is used to remove a pointer value from the
541 // AliasSetTracker entirely. It should be used when an instruction is deleted
542 // from the program to update the AST. If you don't use this, you would have
543 // dangling pointers to deleted instructions.
545 void AliasSetTracker::deleteValue(Value *PtrVal) {
546 // If this is a call instruction, remove the callsite from the appropriate
547 // AliasSet (if present).
548 if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) {
549 if (Inst->mayReadOrWriteMemory()) {
550 // Scan all the alias sets to see if this call site is contained.
551 for (iterator I = begin(), E = end(); I != E;) {
554 Cur->removeUnknownInst(*this, Inst);
559 // First, look up the PointerRec for this pointer.
560 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
561 if (I == PointerMap.end()) return; // Noop
563 // If we found one, remove the pointer from the alias set it is in.
564 AliasSet::PointerRec *PtrValEnt = I->second;
565 AliasSet *AS = PtrValEnt->getAliasSet(*this);
567 // Unlink and delete from the list of values.
568 PtrValEnt->eraseFromList();
570 // Stop using the alias set.
576 // copyValue - This method should be used whenever a preexisting value in the
577 // program is copied or cloned, introducing a new value. Note that it is ok for
578 // clients that use this method to introduce the same value multiple times: if
579 // the tracker already knows about a value, it will ignore the request.
581 void AliasSetTracker::copyValue(Value *From, Value *To) {
582 // First, look up the PointerRec for this pointer.
583 PointerMapType::iterator I = PointerMap.find_as(From);
584 if (I == PointerMap.end())
586 assert(I->second->hasAliasSet() && "Dead entry?");
588 AliasSet::PointerRec &Entry = getEntryFor(To);
589 if (Entry.hasAliasSet()) return; // Already in the tracker!
591 // Add it to the alias set it aliases...
592 I = PointerMap.find_as(From);
593 AliasSet *AS = I->second->getAliasSet(*this);
594 AS->addPointer(*this, Entry, I->second->getSize(),
595 I->second->getAAInfo(), MRI_NoModRef,
601 //===----------------------------------------------------------------------===//
602 // AliasSet/AliasSetTracker Printing Support
603 //===----------------------------------------------------------------------===//
605 void AliasSet::print(raw_ostream &OS) const {
606 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
607 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
609 case MRI_NoModRef: OS << "No access "; break;
610 case MRI_Ref: OS << "Ref "; break;
611 case MRI_Mod: OS << "Mod "; break;
612 case MRI_ModRef: OS << "Mod/Ref "; break;
613 default: llvm_unreachable("Bad value for Access!");
615 if (isVolatile()) OS << "[volatile] ";
617 OS << " forwarding to " << (void*)Forward;
622 for (iterator I = begin(), E = end(); I != E; ++I) {
623 if (I != begin()) OS << ", ";
624 I.getPointer()->printAsOperand(OS << "(");
625 OS << ", " << I.getSize() << ")";
628 if (!UnknownInsts.empty()) {
629 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
630 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
632 UnknownInsts[i]->printAsOperand(OS);
638 void AliasSetTracker::print(raw_ostream &OS) const {
639 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
640 << PointerMap.size() << " pointer values.\n";
641 for (const_iterator I = begin(), E = end(); I != E; ++I)
646 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
647 void AliasSet::dump() const { print(dbgs()); }
648 void AliasSetTracker::dump() const { print(dbgs()); }
651 //===----------------------------------------------------------------------===//
652 // ASTCallbackVH Class Implementation
653 //===----------------------------------------------------------------------===//
655 void AliasSetTracker::ASTCallbackVH::deleted() {
656 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
657 AST->deleteValue(getValPtr());
661 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
662 AST->copyValue(getValPtr(), V);
665 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
666 : CallbackVH(V), AST(ast) {}
668 AliasSetTracker::ASTCallbackVH &
669 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
670 return *this = ASTCallbackVH(V, AST);
673 //===----------------------------------------------------------------------===//
674 // AliasSetPrinter Pass
675 //===----------------------------------------------------------------------===//
678 class AliasSetPrinter : public FunctionPass {
679 AliasSetTracker *Tracker;
681 static char ID; // Pass identification, replacement for typeid
682 AliasSetPrinter() : FunctionPass(ID) {
683 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
686 void getAnalysisUsage(AnalysisUsage &AU) const override {
687 AU.setPreservesAll();
688 AU.addRequired<AAResultsWrapperPass>();
691 bool runOnFunction(Function &F) override {
692 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
693 Tracker = new AliasSetTracker(AAWP.getAAResults());
695 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
697 Tracker->print(errs());
704 char AliasSetPrinter::ID = 0;
705 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
706 "Alias Set Printer", false, true)
707 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
708 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
709 "Alias Set Printer", false, true)