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/ErrorHandling.h"
23 #include "llvm/Support/InstIterator.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/raw_ostream.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 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
157 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
158 != AliasAnalysis::NoModRef)
165 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
166 if (AA.doesNotAccessMemory(CS))
169 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
170 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
171 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
174 for (iterator I = begin(), E = end(); I != E; ++I)
175 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
176 AliasAnalysis::NoModRef)
182 void AliasSetTracker::clear() {
183 // Delete all the PointerRec entries.
184 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
186 I->second->eraseFromList();
190 // The alias sets should all be clear now.
195 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
196 /// instruction referring to the pointer into. If there are multiple alias sets
197 /// that may alias the pointer, merge them together and return the unified set.
199 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
201 AliasSet *FoundSet = 0;
202 for (iterator I = begin(), E = end(); I != E; ++I)
203 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
204 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
205 FoundSet = I; // Remember it.
206 } else { // Otherwise, we must merge the sets.
207 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
214 /// containsPointer - Return true if the specified location is represented by
215 /// this alias set, false otherwise. This does not modify the AST object or
217 bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
218 for (const_iterator I = begin(), E = end(); I != E; ++I)
219 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
226 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
227 AliasSet *FoundSet = 0;
228 for (iterator I = begin(), E = end(); I != E; ++I)
229 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
230 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
231 FoundSet = I; // Remember it.
232 } else if (!I->Forward) { // Otherwise, we must merge the sets.
233 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
243 /// getAliasSetForPointer - Return the alias set that the specified pointer
245 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
247 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
249 // Check to see if the pointer is already known...
250 if (Entry.hasAliasSet()) {
251 Entry.updateSize(Size);
253 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
254 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
255 // Add it to the alias set it aliases...
256 AS->addPointer(*this, Entry, Size);
259 if (New) *New = true;
260 // Otherwise create a new alias set to hold the loaded pointer...
261 AliasSets.push_back(new AliasSet());
262 AliasSets.back().addPointer(*this, Entry, Size);
263 return AliasSets.back();
267 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
269 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
274 bool AliasSetTracker::add(LoadInst *LI) {
276 AliasSet &AS = addPointer(LI->getOperand(0),
277 AA.getTypeStoreSize(LI->getType()),
278 AliasSet::Refs, NewPtr);
279 if (LI->isVolatile()) AS.setVolatile();
283 bool AliasSetTracker::add(StoreInst *SI) {
285 Value *Val = SI->getOperand(0);
286 AliasSet &AS = addPointer(SI->getOperand(1),
287 AA.getTypeStoreSize(Val->getType()),
288 AliasSet::Mods, NewPtr);
289 if (SI->isVolatile()) AS.setVolatile();
293 bool AliasSetTracker::add(VAArgInst *VAAI) {
295 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
300 bool AliasSetTracker::add(CallSite CS) {
301 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
302 return true; // Ignore DbgInfo Intrinsics.
303 if (AA.doesNotAccessMemory(CS))
304 return true; // doesn't alias anything
306 AliasSet *AS = findAliasSetForCallSite(CS);
308 AliasSets.push_back(new AliasSet());
309 AS = &AliasSets.back();
310 AS->addCallSite(CS, AA);
313 AS->addCallSite(CS, AA);
318 bool AliasSetTracker::add(Instruction *I) {
319 // Dispatch to one of the other add methods...
320 if (LoadInst *LI = dyn_cast<LoadInst>(I))
322 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
324 else if (CallInst *CI = dyn_cast<CallInst>(I))
326 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
328 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
333 void AliasSetTracker::add(BasicBlock &BB) {
334 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
338 void AliasSetTracker::add(const AliasSetTracker &AST) {
339 assert(&AA == &AST.AA &&
340 "Merging AliasSetTracker objects with different Alias Analyses!");
342 // Loop over all of the alias sets in AST, adding the pointers contained
343 // therein into the current alias sets. This can cause alias sets to be
344 // merged together in the current AST.
345 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
346 if (!I->Forward) { // Ignore forwarding alias sets
347 AliasSet &AS = const_cast<AliasSet&>(*I);
349 // If there are any call sites in the alias set, add them to this AST.
350 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
351 add(AS.CallSites[i]);
353 // Loop over all of the pointers in this alias set...
354 AliasSet::iterator I = AS.begin(), E = AS.end();
356 for (; I != E; ++I) {
357 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
358 (AliasSet::AccessType)AS.AccessTy, X);
359 if (AS.isVolatile()) NewAS.setVolatile();
364 /// remove - Remove the specified (potentially non-empty) alias set from the
366 void AliasSetTracker::remove(AliasSet &AS) {
367 // Drop all call sites.
368 AS.CallSites.clear();
370 // Clear the alias set.
371 unsigned NumRefs = 0;
372 while (!AS.empty()) {
373 AliasSet::PointerRec *P = AS.PtrList;
375 Value *ValToRemove = P->getValue();
377 // Unlink and delete entry from the list of values.
380 // Remember how many references need to be dropped.
383 // Finally, remove the entry.
384 PointerMap.erase(ValToRemove);
387 // Stop using the alias set, removing it.
388 AS.RefCount -= NumRefs;
389 if (AS.RefCount == 0)
390 AS.removeFromTracker(*this);
393 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
394 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
395 if (!AS) return false;
400 bool AliasSetTracker::remove(LoadInst *LI) {
401 unsigned Size = AA.getTypeStoreSize(LI->getType());
402 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
403 if (!AS) return false;
408 bool AliasSetTracker::remove(StoreInst *SI) {
409 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
410 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
411 if (!AS) return false;
416 bool AliasSetTracker::remove(VAArgInst *VAAI) {
417 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
418 if (!AS) return false;
423 bool AliasSetTracker::remove(CallSite CS) {
424 if (AA.doesNotAccessMemory(CS))
425 return false; // doesn't alias anything
427 AliasSet *AS = findAliasSetForCallSite(CS);
428 if (!AS) return false;
433 bool AliasSetTracker::remove(Instruction *I) {
434 // Dispatch to one of the other remove methods...
435 if (LoadInst *LI = dyn_cast<LoadInst>(I))
437 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
439 else if (CallInst *CI = dyn_cast<CallInst>(I))
441 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
447 // deleteValue method - This method is used to remove a pointer value from the
448 // AliasSetTracker entirely. It should be used when an instruction is deleted
449 // from the program to update the AST. If you don't use this, you would have
450 // dangling pointers to deleted instructions.
452 void AliasSetTracker::deleteValue(Value *PtrVal) {
453 // Notify the alias analysis implementation that this value is gone.
454 AA.deleteValue(PtrVal);
456 // If this is a call instruction, remove the callsite from the appropriate
458 CallSite CS = CallSite::get(PtrVal);
459 if (CS.getInstruction())
460 if (!AA.doesNotAccessMemory(CS))
461 if (AliasSet *AS = findAliasSetForCallSite(CS))
462 AS->removeCallSite(CS);
464 // First, look up the PointerRec for this pointer.
465 PointerMapType::iterator I = PointerMap.find(PtrVal);
466 if (I == PointerMap.end()) return; // Noop
468 // If we found one, remove the pointer from the alias set it is in.
469 AliasSet::PointerRec *PtrValEnt = I->second;
470 AliasSet *AS = PtrValEnt->getAliasSet(*this);
472 // Unlink and delete from the list of values.
473 PtrValEnt->eraseFromList();
475 // Stop using the alias set.
481 // copyValue - This method should be used whenever a preexisting value in the
482 // program is copied or cloned, introducing a new value. Note that it is ok for
483 // clients that use this method to introduce the same value multiple times: if
484 // the tracker already knows about a value, it will ignore the request.
486 void AliasSetTracker::copyValue(Value *From, Value *To) {
487 // Notify the alias analysis implementation that this value is copied.
488 AA.copyValue(From, To);
490 // First, look up the PointerRec for this pointer.
491 PointerMapType::iterator I = PointerMap.find(From);
492 if (I == PointerMap.end())
494 assert(I->second->hasAliasSet() && "Dead entry?");
496 AliasSet::PointerRec &Entry = getEntryFor(To);
497 if (Entry.hasAliasSet()) return; // Already in the tracker!
499 // Add it to the alias set it aliases...
500 I = PointerMap.find(From);
501 AliasSet *AS = I->second->getAliasSet(*this);
502 AS->addPointer(*this, Entry, I->second->getSize(), true);
507 //===----------------------------------------------------------------------===//
508 // AliasSet/AliasSetTracker Printing Support
509 //===----------------------------------------------------------------------===//
511 void AliasSet::print(raw_ostream &OS) const {
512 OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] ";
513 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
515 case NoModRef: OS << "No access "; break;
516 case Refs : OS << "Ref "; break;
517 case Mods : OS << "Mod "; break;
518 case ModRef : OS << "Mod/Ref "; break;
519 default: llvm_unreachable("Bad value for AccessTy!");
521 if (isVolatile()) OS << "[volatile] ";
523 OS << " forwarding to " << (void*)Forward;
528 for (iterator I = begin(), E = end(); I != E; ++I) {
529 if (I != begin()) OS << ", ";
530 WriteAsOperand(OS << "(", I.getPointer());
531 OS << ", " << I.getSize() << ")";
534 if (!CallSites.empty()) {
535 OS << "\n " << CallSites.size() << " Call Sites: ";
536 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
538 WriteAsOperand(OS, CallSites[i].getCalledValue());
544 void AliasSetTracker::print(raw_ostream &OS) const {
545 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
546 << PointerMap.size() << " pointer values.\n";
547 for (const_iterator I = begin(), E = end(); I != E; ++I)
552 void AliasSet::dump() const { print(errs()); }
553 void AliasSetTracker::dump() const { print(errs()); }
555 //===----------------------------------------------------------------------===//
556 // ASTCallbackVH Class Implementation
557 //===----------------------------------------------------------------------===//
559 void AliasSetTracker::ASTCallbackVH::deleted() {
560 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
561 AST->deleteValue(getValPtr());
565 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
566 : CallbackVH(V), AST(ast) {}
568 AliasSetTracker::ASTCallbackVH &
569 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
570 return *this = ASTCallbackVH(V, AST);
573 //===----------------------------------------------------------------------===//
574 // AliasSetPrinter Pass
575 //===----------------------------------------------------------------------===//
578 class AliasSetPrinter : public FunctionPass {
579 AliasSetTracker *Tracker;
581 static char ID; // Pass identification, replacement for typeid
582 AliasSetPrinter() : FunctionPass(&ID) {}
584 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
585 AU.setPreservesAll();
586 AU.addRequired<AliasAnalysis>();
589 virtual bool runOnFunction(Function &F) {
590 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
592 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
594 Tracker->print(errs());
601 char AliasSetPrinter::ID = 0;
602 static RegisterPass<AliasSetPrinter>
603 X("print-alias-sets", "Alias Set Printer", false, true);