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/Compiler.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "llvm/Support/Streams.h"
28 /// mergeSetIn - Merge the specified alias set into this alias set.
30 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
31 assert(!AS.Forward && "Alias set is already forwarding!");
32 assert(!Forward && "This set is a forwarding set!!");
34 // Update the alias and access types of this set...
35 AccessTy |= AS.AccessTy;
36 AliasTy |= AS.AliasTy;
38 if (AliasTy == MustAlias) {
39 // Check that these two merged sets really are must aliases. Since both
40 // used to be must-alias sets, we can just check any pointer from each set
42 AliasAnalysis &AA = AST.getAliasAnalysis();
43 PointerRec *L = getSomePointer();
44 PointerRec *R = AS.getSomePointer();
46 // If the pointers are not a must-alias pair, this set becomes a may alias.
47 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
48 != AliasAnalysis::MustAlias)
52 if (CallSites.empty()) { // Merge call sites...
53 if (!AS.CallSites.empty())
54 std::swap(CallSites, AS.CallSites);
55 } else if (!AS.CallSites.empty()) {
56 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
60 AS.Forward = this; // Forward across AS now...
61 addRef(); // AS is now pointing to us...
63 // Merge the list of constituent pointers...
65 *PtrListEnd = AS.PtrList;
66 AS.PtrList->setPrevInList(PtrListEnd);
67 PtrListEnd = AS.PtrListEnd;
70 AS.PtrListEnd = &AS.PtrList;
71 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
75 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
76 if (AliasSet *Fwd = AS->Forward) {
83 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
84 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
85 AST.removeAliasSet(this);
88 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
89 unsigned Size, bool KnownMustAlias) {
90 assert(!Entry.hasAliasSet() && "Entry already in set!");
92 // Check to see if we have to downgrade to _may_ alias.
93 if (isMustAlias() && !KnownMustAlias)
94 if (PointerRec *P = getSomePointer()) {
95 AliasAnalysis &AA = AST.getAliasAnalysis();
96 AliasAnalysis::AliasResult Result =
97 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
98 if (Result == AliasAnalysis::MayAlias)
100 else // First entry of must alias must have maximum size!
102 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
105 Entry.setAliasSet(this);
106 Entry.updateSize(Size);
108 // Add it to the end of the list...
109 assert(*PtrListEnd == 0 && "End of list is not null?");
110 *PtrListEnd = &Entry;
111 PtrListEnd = Entry.setPrevInList(PtrListEnd);
112 assert(*PtrListEnd == 0 && "End of list is not null?");
113 addRef(); // Entry points to alias set...
116 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
117 CallSites.push_back(CS);
119 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
120 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
122 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
128 // FIXME: This should use mod/ref information to make this not suck so bad
133 /// aliasesPointer - Return true if the specified pointer "may" (or must)
134 /// alias one of the members in the set.
136 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
137 AliasAnalysis &AA) const {
138 if (AliasTy == MustAlias) {
139 assert(CallSites.empty() && "Illegal must alias set!");
141 // If this is a set of MustAliases, only check to see if the pointer aliases
142 // SOME value in the set...
143 PointerRec *SomePtr = getSomePointer();
144 assert(SomePtr && "Empty must-alias set??");
145 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
148 // If this is a may-alias set, we have to check all of the pointers in the set
149 // to be sure it doesn't alias the set...
150 for (iterator I = begin(), E = end(); I != E; ++I)
151 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
154 // Check the call sites list and invoke list...
155 if (!CallSites.empty()) {
156 if (AA.hasNoModRefInfoForCalls())
159 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
160 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
161 != AliasAnalysis::NoModRef)
168 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
169 if (AA.doesNotAccessMemory(CS))
172 if (AA.hasNoModRefInfoForCalls())
175 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
176 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
177 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
180 for (iterator I = begin(), E = end(); I != E; ++I)
181 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
182 AliasAnalysis::NoModRef)
188 void AliasSetTracker::clear() {
189 // Delete all the PointerRec entries.
190 for (DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.begin(),
191 E = PointerMap.end(); I != E; ++I)
192 I->second->eraseFromList();
196 // The alias sets should all be clear now.
201 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
202 /// instruction referring to the pointer into. If there are multiple alias sets
203 /// that may alias the pointer, merge them together and return the unified set.
205 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
207 AliasSet *FoundSet = 0;
208 for (iterator I = begin(), E = end(); I != E; ++I)
209 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
210 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
211 FoundSet = I; // Remember it.
212 } else { // Otherwise, we must merge the sets.
213 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
220 /// containsPointer - Return true if the specified location is represented by
221 /// this alias set, false otherwise. This does not modify the AST object or
223 bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
224 for (const_iterator I = begin(), E = end(); I != E; ++I)
225 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
232 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
233 AliasSet *FoundSet = 0;
234 for (iterator I = begin(), E = end(); I != E; ++I)
235 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
236 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
237 FoundSet = I; // Remember it.
238 } else if (!I->Forward) { // Otherwise, we must merge the sets.
239 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
249 /// getAliasSetForPointer - Return the alias set that the specified pointer
251 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
253 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
255 // Check to see if the pointer is already known...
256 if (Entry.hasAliasSet()) {
257 Entry.updateSize(Size);
259 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
260 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
261 // Add it to the alias set it aliases...
262 AS->addPointer(*this, Entry, Size);
265 if (New) *New = true;
266 // Otherwise create a new alias set to hold the loaded pointer...
267 AliasSets.push_back(new AliasSet());
268 AliasSets.back().addPointer(*this, Entry, Size);
269 return AliasSets.back();
273 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
275 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
280 bool AliasSetTracker::add(LoadInst *LI) {
282 AliasSet &AS = addPointer(LI->getOperand(0),
283 AA.getTargetData().getTypeStoreSize(LI->getType()),
284 AliasSet::Refs, NewPtr);
285 if (LI->isVolatile()) AS.setVolatile();
289 bool AliasSetTracker::add(StoreInst *SI) {
291 Value *Val = SI->getOperand(0);
292 AliasSet &AS = addPointer(SI->getOperand(1),
293 AA.getTargetData().getTypeStoreSize(Val->getType()),
294 AliasSet::Mods, NewPtr);
295 if (SI->isVolatile()) AS.setVolatile();
299 bool AliasSetTracker::add(FreeInst *FI) {
301 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
305 bool AliasSetTracker::add(VAArgInst *VAAI) {
307 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
312 bool AliasSetTracker::add(CallSite CS) {
313 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
314 return true; // Ignore DbgInfo Intrinsics.
315 if (AA.doesNotAccessMemory(CS))
316 return true; // doesn't alias anything
318 AliasSet *AS = findAliasSetForCallSite(CS);
320 AliasSets.push_back(new AliasSet());
321 AS = &AliasSets.back();
322 AS->addCallSite(CS, AA);
325 AS->addCallSite(CS, AA);
330 bool AliasSetTracker::add(Instruction *I) {
331 // Dispatch to one of the other add methods...
332 if (LoadInst *LI = dyn_cast<LoadInst>(I))
334 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
336 else if (CallInst *CI = dyn_cast<CallInst>(I))
338 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
340 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
342 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
347 void AliasSetTracker::add(BasicBlock &BB) {
348 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
352 void AliasSetTracker::add(const AliasSetTracker &AST) {
353 assert(&AA == &AST.AA &&
354 "Merging AliasSetTracker objects with different Alias Analyses!");
356 // Loop over all of the alias sets in AST, adding the pointers contained
357 // therein into the current alias sets. This can cause alias sets to be
358 // merged together in the current AST.
359 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
360 if (!I->Forward) { // Ignore forwarding alias sets
361 AliasSet &AS = const_cast<AliasSet&>(*I);
363 // If there are any call sites in the alias set, add them to this AST.
364 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
365 add(AS.CallSites[i]);
367 // Loop over all of the pointers in this alias set...
368 AliasSet::iterator I = AS.begin(), E = AS.end();
370 for (; I != E; ++I) {
371 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
372 (AliasSet::AccessType)AS.AccessTy, X);
373 if (AS.isVolatile()) NewAS.setVolatile();
378 /// remove - Remove the specified (potentially non-empty) alias set from the
380 void AliasSetTracker::remove(AliasSet &AS) {
381 // Drop all call sites.
382 AS.CallSites.clear();
384 // Clear the alias set.
385 unsigned NumRefs = 0;
386 while (!AS.empty()) {
387 AliasSet::PointerRec *P = AS.PtrList;
389 Value *ValToRemove = P->getValue();
391 // Unlink and delete entry from the list of values.
394 // Remember how many references need to be dropped.
397 // Finally, remove the entry.
398 PointerMap.erase(ValToRemove);
401 // Stop using the alias set, removing it.
402 AS.RefCount -= NumRefs;
403 if (AS.RefCount == 0)
404 AS.removeFromTracker(*this);
407 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
408 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
409 if (!AS) return false;
414 bool AliasSetTracker::remove(LoadInst *LI) {
415 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
416 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
417 if (!AS) return false;
422 bool AliasSetTracker::remove(StoreInst *SI) {
424 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
425 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
426 if (!AS) return false;
431 bool AliasSetTracker::remove(FreeInst *FI) {
432 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
433 if (!AS) return false;
438 bool AliasSetTracker::remove(VAArgInst *VAAI) {
439 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
440 if (!AS) return false;
445 bool AliasSetTracker::remove(CallSite CS) {
446 if (AA.doesNotAccessMemory(CS))
447 return false; // doesn't alias anything
449 AliasSet *AS = findAliasSetForCallSite(CS);
450 if (!AS) return false;
455 bool AliasSetTracker::remove(Instruction *I) {
456 // Dispatch to one of the other remove methods...
457 if (LoadInst *LI = dyn_cast<LoadInst>(I))
459 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
461 else if (CallInst *CI = dyn_cast<CallInst>(I))
463 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
465 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
471 // deleteValue method - This method is used to remove a pointer value from the
472 // AliasSetTracker entirely. It should be used when an instruction is deleted
473 // from the program to update the AST. If you don't use this, you would have
474 // dangling pointers to deleted instructions.
476 void AliasSetTracker::deleteValue(Value *PtrVal) {
477 // Notify the alias analysis implementation that this value is gone.
478 AA.deleteValue(PtrVal);
480 // If this is a call instruction, remove the callsite from the appropriate
482 CallSite CS = CallSite::get(PtrVal);
483 if (CS.getInstruction())
484 if (!AA.doesNotAccessMemory(CS))
485 if (AliasSet *AS = findAliasSetForCallSite(CS))
486 AS->removeCallSite(CS);
488 // First, look up the PointerRec for this pointer.
489 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(PtrVal);
490 if (I == PointerMap.end()) return; // Noop
492 // If we found one, remove the pointer from the alias set it is in.
493 AliasSet::PointerRec *PtrValEnt = I->second;
494 AliasSet *AS = PtrValEnt->getAliasSet(*this);
496 // Unlink and delete from the list of values.
497 PtrValEnt->eraseFromList();
499 // Stop using the alias set.
505 // copyValue - This method should be used whenever a preexisting value in the
506 // program is copied or cloned, introducing a new value. Note that it is ok for
507 // clients that use this method to introduce the same value multiple times: if
508 // the tracker already knows about a value, it will ignore the request.
510 void AliasSetTracker::copyValue(Value *From, Value *To) {
511 // Notify the alias analysis implementation that this value is copied.
512 AA.copyValue(From, To);
514 // First, look up the PointerRec for this pointer.
515 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(From);
516 if (I == PointerMap.end())
518 assert(I->second->hasAliasSet() && "Dead entry?");
520 AliasSet::PointerRec &Entry = getEntryFor(To);
521 if (Entry.hasAliasSet()) return; // Already in the tracker!
523 // Add it to the alias set it aliases...
524 I = PointerMap.find(From);
525 AliasSet *AS = I->second->getAliasSet(*this);
526 AS->addPointer(*this, Entry, I->second->getSize(), true);
531 //===----------------------------------------------------------------------===//
532 // AliasSet/AliasSetTracker Printing Support
533 //===----------------------------------------------------------------------===//
535 void AliasSet::print(std::ostream &OS) const {
536 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
537 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
539 case NoModRef: OS << "No access "; break;
540 case Refs : OS << "Ref "; break;
541 case Mods : OS << "Mod "; break;
542 case ModRef : OS << "Mod/Ref "; break;
543 default: LLVM_UNREACHABLE("Bad value for AccessTy!");
545 if (isVolatile()) OS << "[volatile] ";
547 OS << " forwarding to " << (void*)Forward;
552 for (iterator I = begin(), E = end(); I != E; ++I) {
553 if (I != begin()) OS << ", ";
554 WriteAsOperand(OS << "(", I.getPointer());
555 OS << ", " << I.getSize() << ")";
558 if (!CallSites.empty()) {
559 OS << "\n " << CallSites.size() << " Call Sites: ";
560 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
562 WriteAsOperand(OS, CallSites[i].getCalledValue());
568 void AliasSetTracker::print(std::ostream &OS) const {
569 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
570 << PointerMap.size() << " pointer values.\n";
571 for (const_iterator I = begin(), E = end(); I != E; ++I)
576 void AliasSet::dump() const { print (cerr); }
577 void AliasSetTracker::dump() const { print(cerr); }
579 //===----------------------------------------------------------------------===//
580 // AliasSetPrinter Pass
581 //===----------------------------------------------------------------------===//
584 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
585 AliasSetTracker *Tracker;
587 static char ID; // Pass identification, replacement for typeid
588 AliasSetPrinter() : FunctionPass(&ID) {}
590 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
591 AU.setPreservesAll();
592 AU.addRequired<AliasAnalysis>();
595 virtual bool runOnFunction(Function &F) {
596 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
598 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
600 Tracker->print(cerr);
607 char AliasSetPrinter::ID = 0;
608 static RegisterPass<AliasSetPrinter>
609 X("print-alias-sets", "Alias Set Printer", false, true);