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/Pass.h"
19 #include "llvm/Type.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "llvm/Support/Format.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;
39 if (AliasTy == MustAlias) {
40 // Check that these two merged sets really are must aliases. Since both
41 // used to be must-alias sets, we can just check any pointer from each set
43 AliasAnalysis &AA = AST.getAliasAnalysis();
44 PointerRec *L = getSomePointer();
45 PointerRec *R = AS.getSomePointer();
47 // If the pointers are not a must-alias pair, this set becomes a may alias.
48 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
49 != AliasAnalysis::MustAlias)
53 if (CallSites.empty()) { // Merge call sites...
54 if (!AS.CallSites.empty())
55 std::swap(CallSites, AS.CallSites);
56 } else if (!AS.CallSites.empty()) {
57 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
61 AS.Forward = this; // Forward across AS now...
62 addRef(); // AS is now pointing to us...
64 // Merge the list of constituent pointers...
66 *PtrListEnd = AS.PtrList;
67 AS.PtrList->setPrevInList(PtrListEnd);
68 PtrListEnd = AS.PtrListEnd;
71 AS.PtrListEnd = &AS.PtrList;
72 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
76 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
77 if (AliasSet *Fwd = AS->Forward) {
84 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
85 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
86 AST.removeAliasSet(this);
89 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
90 unsigned Size, bool KnownMustAlias) {
91 assert(!Entry.hasAliasSet() && "Entry already in set!");
93 // Check to see if we have to downgrade to _may_ alias.
94 if (isMustAlias() && !KnownMustAlias)
95 if (PointerRec *P = getSomePointer()) {
96 AliasAnalysis &AA = AST.getAliasAnalysis();
97 AliasAnalysis::AliasResult Result =
98 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
99 if (Result == AliasAnalysis::MayAlias)
101 else // First entry of must alias must have maximum size!
103 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
106 Entry.setAliasSet(this);
107 Entry.updateSize(Size);
109 // Add it to the end of the list...
110 assert(*PtrListEnd == 0 && "End of list is not null?");
111 *PtrListEnd = &Entry;
112 PtrListEnd = Entry.setPrevInList(PtrListEnd);
113 assert(*PtrListEnd == 0 && "End of list is not null?");
114 addRef(); // Entry points to alias set...
117 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
118 CallSites.push_back(CS);
120 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
121 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
123 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
129 // FIXME: This should use mod/ref information to make this not suck so bad
134 /// aliasesPointer - Return true if the specified pointer "may" (or must)
135 /// alias one of the members in the set.
137 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
138 AliasAnalysis &AA) const {
139 if (AliasTy == MustAlias) {
140 assert(CallSites.empty() && "Illegal must alias set!");
142 // If this is a set of MustAliases, only check to see if the pointer aliases
143 // SOME value in the set...
144 PointerRec *SomePtr = getSomePointer();
145 assert(SomePtr && "Empty must-alias set??");
146 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
149 // If this is a may-alias set, we have to check all of the pointers in the set
150 // to be sure it doesn't alias the set...
151 for (iterator I = begin(), E = end(); I != E; ++I)
152 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
155 // Check the call sites list and invoke list...
156 if (!CallSites.empty()) {
157 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
158 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
159 != AliasAnalysis::NoModRef)
166 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
167 if (AA.doesNotAccessMemory(CS))
170 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
171 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
172 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
175 for (iterator I = begin(), E = end(); I != E; ++I)
176 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
177 AliasAnalysis::NoModRef)
183 void AliasSetTracker::clear() {
184 // Delete all the PointerRec entries.
185 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
187 I->second->eraseFromList();
191 // The alias sets should all be clear now.
196 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
197 /// instruction referring to the pointer into. If there are multiple alias sets
198 /// that may alias the pointer, merge them together and return the unified set.
200 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
202 AliasSet *FoundSet = 0;
203 for (iterator I = begin(), E = end(); I != E; ++I)
204 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
205 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
206 FoundSet = I; // Remember it.
207 } else { // Otherwise, we must merge the sets.
208 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
215 /// containsPointer - Return true if the specified location is represented by
216 /// this alias set, false otherwise. This does not modify the AST object or
218 bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
219 for (const_iterator I = begin(), E = end(); I != E; ++I)
220 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
227 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
228 AliasSet *FoundSet = 0;
229 for (iterator I = begin(), E = end(); I != E; ++I)
230 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
231 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
232 FoundSet = I; // Remember it.
233 } else if (!I->Forward) { // Otherwise, we must merge the sets.
234 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
244 /// getAliasSetForPointer - Return the alias set that the specified pointer
246 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
248 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
250 // Check to see if the pointer is already known...
251 if (Entry.hasAliasSet()) {
252 Entry.updateSize(Size);
254 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
255 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
256 // Add it to the alias set it aliases...
257 AS->addPointer(*this, Entry, Size);
260 if (New) *New = true;
261 // Otherwise create a new alias set to hold the loaded pointer...
262 AliasSets.push_back(new AliasSet());
263 AliasSets.back().addPointer(*this, Entry, Size);
264 return AliasSets.back();
268 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
270 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
275 bool AliasSetTracker::add(LoadInst *LI) {
277 AliasSet &AS = addPointer(LI->getOperand(0),
278 AA.getTypeStoreSize(LI->getType()),
279 AliasSet::Refs, NewPtr);
280 if (LI->isVolatile()) AS.setVolatile();
284 bool AliasSetTracker::add(StoreInst *SI) {
286 Value *Val = SI->getOperand(0);
287 AliasSet &AS = addPointer(SI->getOperand(1),
288 AA.getTypeStoreSize(Val->getType()),
289 AliasSet::Mods, NewPtr);
290 if (SI->isVolatile()) AS.setVolatile();
294 bool AliasSetTracker::add(VAArgInst *VAAI) {
296 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
301 bool AliasSetTracker::add(CallSite CS) {
302 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
303 return true; // Ignore DbgInfo Intrinsics.
304 if (AA.doesNotAccessMemory(CS))
305 return true; // doesn't alias anything
307 AliasSet *AS = findAliasSetForCallSite(CS);
309 AliasSets.push_back(new AliasSet());
310 AS = &AliasSets.back();
311 AS->addCallSite(CS, AA);
314 AS->addCallSite(CS, AA);
319 bool AliasSetTracker::add(Instruction *I) {
320 // Dispatch to one of the other add methods...
321 if (LoadInst *LI = dyn_cast<LoadInst>(I))
323 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
325 else if (CallInst *CI = dyn_cast<CallInst>(I))
327 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
329 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
334 void AliasSetTracker::add(BasicBlock &BB) {
335 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
339 void AliasSetTracker::add(const AliasSetTracker &AST) {
340 assert(&AA == &AST.AA &&
341 "Merging AliasSetTracker objects with different Alias Analyses!");
343 // Loop over all of the alias sets in AST, adding the pointers contained
344 // therein into the current alias sets. This can cause alias sets to be
345 // merged together in the current AST.
346 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
347 if (!I->Forward) { // Ignore forwarding alias sets
348 AliasSet &AS = const_cast<AliasSet&>(*I);
350 // If there are any call sites in the alias set, add them to this AST.
351 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
352 add(AS.CallSites[i]);
354 // Loop over all of the pointers in this alias set...
355 AliasSet::iterator I = AS.begin(), E = AS.end();
357 for (; I != E; ++I) {
358 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
359 (AliasSet::AccessType)AS.AccessTy, X);
360 if (AS.isVolatile()) NewAS.setVolatile();
365 /// remove - Remove the specified (potentially non-empty) alias set from the
367 void AliasSetTracker::remove(AliasSet &AS) {
368 // Drop all call sites.
369 AS.CallSites.clear();
371 // Clear the alias set.
372 unsigned NumRefs = 0;
373 while (!AS.empty()) {
374 AliasSet::PointerRec *P = AS.PtrList;
376 Value *ValToRemove = P->getValue();
378 // Unlink and delete entry from the list of values.
381 // Remember how many references need to be dropped.
384 // Finally, remove the entry.
385 PointerMap.erase(ValToRemove);
388 // Stop using the alias set, removing it.
389 AS.RefCount -= NumRefs;
390 if (AS.RefCount == 0)
391 AS.removeFromTracker(*this);
394 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
395 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
396 if (!AS) return false;
401 bool AliasSetTracker::remove(LoadInst *LI) {
402 unsigned Size = AA.getTypeStoreSize(LI->getType());
403 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
404 if (!AS) return false;
409 bool AliasSetTracker::remove(StoreInst *SI) {
410 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
411 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
412 if (!AS) return false;
417 bool AliasSetTracker::remove(VAArgInst *VAAI) {
418 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
419 if (!AS) return false;
424 bool AliasSetTracker::remove(CallSite CS) {
425 if (AA.doesNotAccessMemory(CS))
426 return false; // doesn't alias anything
428 AliasSet *AS = findAliasSetForCallSite(CS);
429 if (!AS) return false;
434 bool AliasSetTracker::remove(Instruction *I) {
435 // Dispatch to one of the other remove methods...
436 if (LoadInst *LI = dyn_cast<LoadInst>(I))
438 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
440 else if (CallInst *CI = dyn_cast<CallInst>(I))
442 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
448 // deleteValue method - This method is used to remove a pointer value from the
449 // AliasSetTracker entirely. It should be used when an instruction is deleted
450 // from the program to update the AST. If you don't use this, you would have
451 // dangling pointers to deleted instructions.
453 void AliasSetTracker::deleteValue(Value *PtrVal) {
454 // Notify the alias analysis implementation that this value is gone.
455 AA.deleteValue(PtrVal);
457 // If this is a call instruction, remove the callsite from the appropriate
459 CallSite CS = CallSite::get(PtrVal);
460 if (CS.getInstruction())
461 if (!AA.doesNotAccessMemory(CS))
462 if (AliasSet *AS = findAliasSetForCallSite(CS))
463 AS->removeCallSite(CS);
465 // First, look up the PointerRec for this pointer.
466 PointerMapType::iterator I = PointerMap.find(PtrVal);
467 if (I == PointerMap.end()) return; // Noop
469 // If we found one, remove the pointer from the alias set it is in.
470 AliasSet::PointerRec *PtrValEnt = I->second;
471 AliasSet *AS = PtrValEnt->getAliasSet(*this);
473 // Unlink and delete from the list of values.
474 PtrValEnt->eraseFromList();
476 // Stop using the alias set.
482 // copyValue - This method should be used whenever a preexisting value in the
483 // program is copied or cloned, introducing a new value. Note that it is ok for
484 // clients that use this method to introduce the same value multiple times: if
485 // the tracker already knows about a value, it will ignore the request.
487 void AliasSetTracker::copyValue(Value *From, Value *To) {
488 // Notify the alias analysis implementation that this value is copied.
489 AA.copyValue(From, To);
491 // First, look up the PointerRec for this pointer.
492 PointerMapType::iterator I = PointerMap.find(From);
493 if (I == PointerMap.end())
495 assert(I->second->hasAliasSet() && "Dead entry?");
497 AliasSet::PointerRec &Entry = getEntryFor(To);
498 if (Entry.hasAliasSet()) return; // Already in the tracker!
500 // Add it to the alias set it aliases...
501 I = PointerMap.find(From);
502 AliasSet *AS = I->second->getAliasSet(*this);
503 AS->addPointer(*this, Entry, I->second->getSize(), true);
508 //===----------------------------------------------------------------------===//
509 // AliasSet/AliasSetTracker Printing Support
510 //===----------------------------------------------------------------------===//
512 void AliasSet::print(raw_ostream &OS) const {
513 OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] ";
514 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
516 case NoModRef: OS << "No access "; break;
517 case Refs : OS << "Ref "; break;
518 case Mods : OS << "Mod "; break;
519 case ModRef : OS << "Mod/Ref "; break;
520 default: llvm_unreachable("Bad value for AccessTy!");
522 if (isVolatile()) OS << "[volatile] ";
524 OS << " forwarding to " << (void*)Forward;
529 for (iterator I = begin(), E = end(); I != E; ++I) {
530 if (I != begin()) OS << ", ";
531 WriteAsOperand(OS << "(", I.getPointer());
532 OS << ", " << I.getSize() << ")";
535 if (!CallSites.empty()) {
536 OS << "\n " << CallSites.size() << " Call Sites: ";
537 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
539 WriteAsOperand(OS, CallSites[i].getCalledValue());
545 void AliasSetTracker::print(raw_ostream &OS) const {
546 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
547 << PointerMap.size() << " pointer values.\n";
548 for (const_iterator I = begin(), E = end(); I != E; ++I)
553 void AliasSet::dump() const { print(dbgs()); }
554 void AliasSetTracker::dump() const { print(dbgs()); }
556 //===----------------------------------------------------------------------===//
557 // ASTCallbackVH Class Implementation
558 //===----------------------------------------------------------------------===//
560 void AliasSetTracker::ASTCallbackVH::deleted() {
561 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
562 AST->deleteValue(getValPtr());
566 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
567 : CallbackVH(V), AST(ast) {}
569 AliasSetTracker::ASTCallbackVH &
570 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
571 return *this = ASTCallbackVH(V, AST);
574 //===----------------------------------------------------------------------===//
575 // AliasSetPrinter Pass
576 //===----------------------------------------------------------------------===//
579 class AliasSetPrinter : public FunctionPass {
580 AliasSetTracker *Tracker;
582 static char ID; // Pass identification, replacement for typeid
583 AliasSetPrinter() : FunctionPass(&ID) {}
585 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
586 AU.setPreservesAll();
587 AU.addRequired<AliasAnalysis>();
590 virtual bool runOnFunction(Function &F) {
591 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
593 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
595 Tracker->print(errs());
602 char AliasSetPrinter::ID = 0;
603 static RegisterPass<AliasSetPrinter>
604 X("print-alias-sets", "Alias Set Printer", false, true);