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/Instructions.h"
17 #include "llvm/IntrinsicInst.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Type.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/InstIterator.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...
36 AccessTy |= AS.AccessTy;
37 AliasTy |= AS.AliasTy;
38 Volatile |= AS.Volatile;
40 if (AliasTy == MustAlias) {
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(AliasAnalysis::Location(L->getValue(),
52 AliasAnalysis::Location(R->getValue(),
55 != AliasAnalysis::MustAlias)
59 if (UnknownInsts.empty()) { // Merge call sites...
60 if (!AS.UnknownInsts.empty())
61 std::swap(UnknownInsts, AS.UnknownInsts);
62 } else if (!AS.UnknownInsts.empty()) {
63 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
64 AS.UnknownInsts.clear();
67 AS.Forward = this; // Forward across AS now...
68 addRef(); // AS is now pointing to us...
70 // Merge the list of constituent pointers...
72 *PtrListEnd = AS.PtrList;
73 AS.PtrList->setPrevInList(PtrListEnd);
74 PtrListEnd = AS.PtrListEnd;
77 AS.PtrListEnd = &AS.PtrList;
78 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
82 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
83 if (AliasSet *Fwd = AS->Forward) {
90 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
91 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
92 AST.removeAliasSet(this);
95 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
96 uint64_t Size, const MDNode *TBAAInfo,
97 bool KnownMustAlias) {
98 assert(!Entry.hasAliasSet() && "Entry already in set!");
100 // Check to see if we have to downgrade to _may_ alias.
101 if (isMustAlias() && !KnownMustAlias)
102 if (PointerRec *P = getSomePointer()) {
103 AliasAnalysis &AA = AST.getAliasAnalysis();
104 AliasAnalysis::AliasResult Result =
105 AA.alias(AliasAnalysis::Location(P->getValue(), P->getSize(),
107 AliasAnalysis::Location(Entry.getValue(), Size, TBAAInfo));
108 if (Result != AliasAnalysis::MustAlias)
110 else // First entry of must alias must have maximum size!
111 P->updateSizeAndTBAAInfo(Size, TBAAInfo);
112 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
115 Entry.setAliasSet(this);
116 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
118 // Add it to the end of the list...
119 assert(*PtrListEnd == 0 && "End of list is not null?");
120 *PtrListEnd = &Entry;
121 PtrListEnd = Entry.setPrevInList(PtrListEnd);
122 assert(*PtrListEnd == 0 && "End of list is not null?");
123 addRef(); // Entry points to alias set.
126 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
127 UnknownInsts.push_back(I);
129 if (!I->mayWriteToMemory()) {
135 // FIXME: This should use mod/ref information to make this not suck so bad
140 /// aliasesPointer - Return true if the specified pointer "may" (or must)
141 /// alias one of the members in the set.
143 bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
144 const MDNode *TBAAInfo,
145 AliasAnalysis &AA) const {
146 if (AliasTy == MustAlias) {
147 assert(UnknownInsts.empty() && "Illegal must alias set!");
149 // If this is a set of MustAliases, only check to see if the pointer aliases
150 // SOME value in the set.
151 PointerRec *SomePtr = getSomePointer();
152 assert(SomePtr && "Empty must-alias set??");
153 return AA.alias(AliasAnalysis::Location(SomePtr->getValue(),
155 SomePtr->getTBAAInfo()),
156 AliasAnalysis::Location(Ptr, Size, TBAAInfo));
159 // If this is a may-alias set, we have to check all of the pointers in the set
160 // to be sure it doesn't alias the set...
161 for (iterator I = begin(), E = end(); I != E; ++I)
162 if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
163 AliasAnalysis::Location(I.getPointer(), I.getSize(),
167 // Check the unknown instructions...
168 if (!UnknownInsts.empty()) {
169 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
170 if (AA.getModRefInfo(UnknownInsts[i],
171 AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
172 AliasAnalysis::NoModRef)
179 bool AliasSet::aliasesUnknownInst(Instruction *Inst, AliasAnalysis &AA) const {
180 if (!Inst->mayReadOrWriteMemory())
183 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
184 CallSite C1 = getUnknownInst(i), C2 = Inst;
186 AA.getModRefInfo(C1, C2) != AliasAnalysis::NoModRef ||
187 AA.getModRefInfo(C2, C1) != AliasAnalysis::NoModRef)
191 for (iterator I = begin(), E = end(); I != E; ++I)
192 if (AA.getModRefInfo(Inst, AliasAnalysis::Location(I.getPointer(),
195 AliasAnalysis::NoModRef)
201 void AliasSetTracker::clear() {
202 // Delete all the PointerRec entries.
203 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
205 I->second->eraseFromList();
209 // The alias sets should all be clear now.
214 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
215 /// instruction referring to the pointer into. If there are multiple alias sets
216 /// that may alias the pointer, merge them together and return the unified set.
218 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
220 const MDNode *TBAAInfo) {
221 AliasSet *FoundSet = 0;
222 for (iterator I = begin(), E = end(); I != E; ++I) {
223 if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
225 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
226 FoundSet = I; // Remember it.
227 } else { // Otherwise, we must merge the sets.
228 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
235 /// containsPointer - Return true if the specified location is represented by
236 /// this alias set, false otherwise. This does not modify the AST object or
238 bool AliasSetTracker::containsPointer(Value *Ptr, uint64_t Size,
239 const MDNode *TBAAInfo) const {
240 for (const_iterator I = begin(), E = end(); I != E; ++I)
241 if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
248 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
249 AliasSet *FoundSet = 0;
250 for (iterator I = begin(), E = end(); I != E; ++I) {
251 if (I->Forward || !I->aliasesUnknownInst(Inst, AA))
254 if (FoundSet == 0) // If this is the first alias set ptr can go into.
255 FoundSet = I; // Remember it.
256 else if (!I->Forward) // Otherwise, we must merge the sets.
257 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
265 /// getAliasSetForPointer - Return the alias set that the specified pointer
267 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
268 const MDNode *TBAAInfo,
270 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
272 // Check to see if the pointer is already known.
273 if (Entry.hasAliasSet()) {
274 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
276 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
279 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
280 // Add it to the alias set it aliases.
281 AS->addPointer(*this, Entry, Size, TBAAInfo);
285 if (New) *New = true;
286 // Otherwise create a new alias set to hold the loaded pointer.
287 AliasSets.push_back(new AliasSet());
288 AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
289 return AliasSets.back();
292 bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
294 addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
299 bool AliasSetTracker::add(LoadInst *LI) {
300 if (LI->getOrdering() > Monotonic) return addUnknown(LI);
301 AliasSet::AccessType ATy = AliasSet::Refs;
302 if (!LI->isUnordered()) ATy = AliasSet::ModRef;
304 AliasSet &AS = addPointer(LI->getOperand(0),
305 AA.getTypeStoreSize(LI->getType()),
306 LI->getMetadata(LLVMContext::MD_tbaa),
308 if (LI->isVolatile()) AS.setVolatile();
312 bool AliasSetTracker::add(StoreInst *SI) {
313 if (SI->getOrdering() > Monotonic) return addUnknown(SI);
314 AliasSet::AccessType ATy = AliasSet::Mods;
315 if (!SI->isUnordered()) ATy = AliasSet::ModRef;
317 Value *Val = SI->getOperand(0);
318 AliasSet &AS = addPointer(SI->getOperand(1),
319 AA.getTypeStoreSize(Val->getType()),
320 SI->getMetadata(LLVMContext::MD_tbaa),
322 if (SI->isVolatile()) AS.setVolatile();
326 bool AliasSetTracker::add(VAArgInst *VAAI) {
328 addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize,
329 VAAI->getMetadata(LLVMContext::MD_tbaa),
330 AliasSet::ModRef, NewPtr);
335 bool AliasSetTracker::addUnknown(Instruction *Inst) {
336 if (isa<DbgInfoIntrinsic>(Inst))
337 return true; // Ignore DbgInfo Intrinsics.
338 if (!Inst->mayReadOrWriteMemory())
339 return true; // doesn't alias anything
341 AliasSet *AS = findAliasSetForUnknownInst(Inst);
343 AS->addUnknownInst(Inst, AA);
346 AliasSets.push_back(new AliasSet());
347 AS = &AliasSets.back();
348 AS->addUnknownInst(Inst, AA);
352 bool AliasSetTracker::add(Instruction *I) {
353 // Dispatch to one of the other add methods.
354 if (LoadInst *LI = dyn_cast<LoadInst>(I))
356 if (StoreInst *SI = dyn_cast<StoreInst>(I))
358 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
360 return addUnknown(I);
363 void AliasSetTracker::add(BasicBlock &BB) {
364 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
368 void AliasSetTracker::add(const AliasSetTracker &AST) {
369 assert(&AA == &AST.AA &&
370 "Merging AliasSetTracker objects with different Alias Analyses!");
372 // Loop over all of the alias sets in AST, adding the pointers contained
373 // therein into the current alias sets. This can cause alias sets to be
374 // merged together in the current AST.
375 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
376 if (I->Forward) continue; // Ignore forwarding alias sets
378 AliasSet &AS = const_cast<AliasSet&>(*I);
380 // If there are any call sites in the alias set, add them to this AST.
381 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
382 add(AS.UnknownInsts[i]);
384 // Loop over all of the pointers in this alias set.
386 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
387 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
389 (AliasSet::AccessType)AS.AccessTy, X);
390 if (AS.isVolatile()) NewAS.setVolatile();
395 /// remove - Remove the specified (potentially non-empty) alias set from the
397 void AliasSetTracker::remove(AliasSet &AS) {
398 // Drop all call sites.
399 AS.UnknownInsts.clear();
401 // Clear the alias set.
402 unsigned NumRefs = 0;
403 while (!AS.empty()) {
404 AliasSet::PointerRec *P = AS.PtrList;
406 Value *ValToRemove = P->getValue();
408 // Unlink and delete entry from the list of values.
411 // Remember how many references need to be dropped.
414 // Finally, remove the entry.
415 PointerMap.erase(ValToRemove);
418 // Stop using the alias set, removing it.
419 AS.RefCount -= NumRefs;
420 if (AS.RefCount == 0)
421 AS.removeFromTracker(*this);
425 AliasSetTracker::remove(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
426 AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
427 if (!AS) return false;
432 bool AliasSetTracker::remove(LoadInst *LI) {
433 uint64_t Size = AA.getTypeStoreSize(LI->getType());
434 const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
435 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
436 if (!AS) return false;
441 bool AliasSetTracker::remove(StoreInst *SI) {
442 uint64_t Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
443 const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
444 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
445 if (!AS) return false;
450 bool AliasSetTracker::remove(VAArgInst *VAAI) {
451 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
452 AliasAnalysis::UnknownSize,
453 VAAI->getMetadata(LLVMContext::MD_tbaa));
454 if (!AS) return false;
459 bool AliasSetTracker::removeUnknown(Instruction *I) {
460 if (!I->mayReadOrWriteMemory())
461 return false; // doesn't alias anything
463 AliasSet *AS = findAliasSetForUnknownInst(I);
464 if (!AS) return false;
469 bool AliasSetTracker::remove(Instruction *I) {
470 // Dispatch to one of the other remove methods...
471 if (LoadInst *LI = dyn_cast<LoadInst>(I))
473 if (StoreInst *SI = dyn_cast<StoreInst>(I))
475 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
477 return removeUnknown(I);
481 // deleteValue method - This method is used to remove a pointer value from the
482 // AliasSetTracker entirely. It should be used when an instruction is deleted
483 // from the program to update the AST. If you don't use this, you would have
484 // dangling pointers to deleted instructions.
486 void AliasSetTracker::deleteValue(Value *PtrVal) {
487 // Notify the alias analysis implementation that this value is gone.
488 AA.deleteValue(PtrVal);
490 // If this is a call instruction, remove the callsite from the appropriate
491 // AliasSet (if present).
492 if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) {
493 if (Inst->mayReadOrWriteMemory()) {
494 // Scan all the alias sets to see if this call site is contained.
495 for (iterator I = begin(), E = end(); I != E; ++I) {
496 if (I->Forward) continue;
498 I->removeUnknownInst(Inst);
503 // First, look up the PointerRec for this pointer.
504 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
505 if (I == PointerMap.end()) return; // Noop
507 // If we found one, remove the pointer from the alias set it is in.
508 AliasSet::PointerRec *PtrValEnt = I->second;
509 AliasSet *AS = PtrValEnt->getAliasSet(*this);
511 // Unlink and delete from the list of values.
512 PtrValEnt->eraseFromList();
514 // Stop using the alias set.
520 // copyValue - This method should be used whenever a preexisting value in the
521 // program is copied or cloned, introducing a new value. Note that it is ok for
522 // clients that use this method to introduce the same value multiple times: if
523 // the tracker already knows about a value, it will ignore the request.
525 void AliasSetTracker::copyValue(Value *From, Value *To) {
526 // Notify the alias analysis implementation that this value is copied.
527 AA.copyValue(From, To);
529 // First, look up the PointerRec for this pointer.
530 PointerMapType::iterator I = PointerMap.find_as(From);
531 if (I == PointerMap.end())
533 assert(I->second->hasAliasSet() && "Dead entry?");
535 AliasSet::PointerRec &Entry = getEntryFor(To);
536 if (Entry.hasAliasSet()) return; // Already in the tracker!
538 // Add it to the alias set it aliases...
539 I = PointerMap.find_as(From);
540 AliasSet *AS = I->second->getAliasSet(*this);
541 AS->addPointer(*this, Entry, I->second->getSize(),
542 I->second->getTBAAInfo(),
548 //===----------------------------------------------------------------------===//
549 // AliasSet/AliasSetTracker Printing Support
550 //===----------------------------------------------------------------------===//
552 void AliasSet::print(raw_ostream &OS) const {
553 OS << " AliasSet[" << (void*)this << ", " << RefCount << "] ";
554 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
556 case NoModRef: OS << "No access "; break;
557 case Refs : OS << "Ref "; break;
558 case Mods : OS << "Mod "; break;
559 case ModRef : OS << "Mod/Ref "; break;
560 default: llvm_unreachable("Bad value for AccessTy!");
562 if (isVolatile()) OS << "[volatile] ";
564 OS << " forwarding to " << (void*)Forward;
569 for (iterator I = begin(), E = end(); I != E; ++I) {
570 if (I != begin()) OS << ", ";
571 WriteAsOperand(OS << "(", I.getPointer());
572 OS << ", " << I.getSize() << ")";
575 if (!UnknownInsts.empty()) {
576 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
577 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
579 WriteAsOperand(OS, UnknownInsts[i]);
585 void AliasSetTracker::print(raw_ostream &OS) const {
586 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
587 << PointerMap.size() << " pointer values.\n";
588 for (const_iterator I = begin(), E = end(); I != E; ++I)
593 void AliasSet::dump() const { print(dbgs()); }
594 void AliasSetTracker::dump() const { print(dbgs()); }
596 //===----------------------------------------------------------------------===//
597 // ASTCallbackVH Class Implementation
598 //===----------------------------------------------------------------------===//
600 void AliasSetTracker::ASTCallbackVH::deleted() {
601 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
602 AST->deleteValue(getValPtr());
606 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
607 AST->copyValue(getValPtr(), V);
610 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
611 : CallbackVH(V), AST(ast) {}
613 AliasSetTracker::ASTCallbackVH &
614 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
615 return *this = ASTCallbackVH(V, AST);
618 //===----------------------------------------------------------------------===//
619 // AliasSetPrinter Pass
620 //===----------------------------------------------------------------------===//
623 class AliasSetPrinter : public FunctionPass {
624 AliasSetTracker *Tracker;
626 static char ID; // Pass identification, replacement for typeid
627 AliasSetPrinter() : FunctionPass(ID) {
628 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
631 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
632 AU.setPreservesAll();
633 AU.addRequired<AliasAnalysis>();
636 virtual bool runOnFunction(Function &F) {
637 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
639 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
641 Tracker->print(errs());
648 char AliasSetPrinter::ID = 0;
649 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
650 "Alias Set Printer", false, true)
651 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
652 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
653 "Alias Set Printer", false, true)