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;
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(L->getValue(), L->getSize(), R->getValue(), R->getSize())
50 != AliasAnalysis::MustAlias)
54 if (CallSites.empty()) { // Merge call sites...
55 if (!AS.CallSites.empty())
56 std::swap(CallSites, AS.CallSites);
57 } else if (!AS.CallSites.empty()) {
58 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
62 AS.Forward = this; // Forward across AS now...
63 addRef(); // AS is now pointing to us...
65 // Merge the list of constituent pointers...
67 *PtrListEnd = AS.PtrList;
68 AS.PtrList->setPrevInList(PtrListEnd);
69 PtrListEnd = AS.PtrListEnd;
72 AS.PtrListEnd = &AS.PtrList;
73 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
77 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
78 if (AliasSet *Fwd = AS->Forward) {
85 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
86 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
87 AST.removeAliasSet(this);
90 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
91 unsigned Size, bool KnownMustAlias) {
92 assert(!Entry.hasAliasSet() && "Entry already in set!");
94 // Check to see if we have to downgrade to _may_ alias.
95 if (isMustAlias() && !KnownMustAlias)
96 if (PointerRec *P = getSomePointer()) {
97 AliasAnalysis &AA = AST.getAliasAnalysis();
98 AliasAnalysis::AliasResult Result =
99 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
100 if (Result == AliasAnalysis::MayAlias)
102 else // First entry of must alias must have maximum size!
104 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
107 Entry.setAliasSet(this);
108 Entry.updateSize(Size);
110 // Add it to the end of the list...
111 assert(*PtrListEnd == 0 && "End of list is not null?");
112 *PtrListEnd = &Entry;
113 PtrListEnd = Entry.setPrevInList(PtrListEnd);
114 assert(*PtrListEnd == 0 && "End of list is not null?");
115 addRef(); // Entry points to alias set.
118 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
119 CallSites.push_back(CS);
121 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
122 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
124 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
130 // FIXME: This should use mod/ref information to make this not suck so bad
135 /// aliasesPointer - Return true if the specified pointer "may" (or must)
136 /// alias one of the members in the set.
138 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
139 AliasAnalysis &AA) const {
140 if (AliasTy == MustAlias) {
141 assert(CallSites.empty() && "Illegal must alias set!");
143 // If this is a set of MustAliases, only check to see if the pointer aliases
144 // SOME value in the set.
145 PointerRec *SomePtr = getSomePointer();
146 assert(SomePtr && "Empty must-alias set??");
147 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
150 // If this is a may-alias set, we have to check all of the pointers in the set
151 // to be sure it doesn't alias the set...
152 for (iterator I = begin(), E = end(); I != E; ++I)
153 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
156 // Check the call sites list and invoke list...
157 if (!CallSites.empty()) {
158 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
159 if (AA.getModRefInfo(CallSites[i], Ptr, Size) != 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)) continue;
206 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
207 FoundSet = I; // Remember it.
208 } else { // Otherwise, we must merge the sets.
209 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
216 /// containsPointer - Return true if the specified location is represented by
217 /// this alias set, false otherwise. This does not modify the AST object or
219 bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
220 for (const_iterator I = begin(), E = end(); I != E; ++I)
221 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
228 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
229 AliasSet *FoundSet = 0;
230 for (iterator I = begin(), E = end(); I != E; ++I) {
231 if (I->Forward || !I->aliasesCallSite(CS, AA))
234 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
235 FoundSet = I; // Remember it.
236 } else if (!I->Forward) { // Otherwise, we must merge the sets.
237 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
246 /// getAliasSetForPointer - Return the alias set that the specified pointer
248 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
250 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
252 // Check to see if the pointer is already known.
253 if (Entry.hasAliasSet()) {
254 Entry.updateSize(Size);
256 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
259 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
260 // Add it to the alias set it aliases.
261 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();
272 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
274 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
279 bool AliasSetTracker::add(LoadInst *LI) {
281 AliasSet &AS = addPointer(LI->getOperand(0),
282 AA.getTypeStoreSize(LI->getType()),
283 AliasSet::Refs, NewPtr);
284 if (LI->isVolatile()) AS.setVolatile();
288 bool AliasSetTracker::add(StoreInst *SI) {
290 Value *Val = SI->getOperand(0);
291 AliasSet &AS = addPointer(SI->getOperand(1),
292 AA.getTypeStoreSize(Val->getType()),
293 AliasSet::Mods, NewPtr);
294 if (SI->isVolatile()) AS.setVolatile();
298 bool AliasSetTracker::add(VAArgInst *VAAI) {
300 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
305 bool AliasSetTracker::add(CallSite CS) {
306 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
307 return true; // Ignore DbgInfo Intrinsics.
308 if (AA.doesNotAccessMemory(CS))
309 return true; // doesn't alias anything
311 AliasSet *AS = findAliasSetForCallSite(CS);
313 AS->addCallSite(CS, AA);
316 AliasSets.push_back(new AliasSet());
317 AS = &AliasSets.back();
318 AS->addCallSite(CS, AA);
322 bool AliasSetTracker::add(Instruction *I) {
323 // Dispatch to one of the other add methods.
324 if (LoadInst *LI = dyn_cast<LoadInst>(I))
326 if (StoreInst *SI = dyn_cast<StoreInst>(I))
328 if (CallInst *CI = dyn_cast<CallInst>(I))
330 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
332 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
337 void AliasSetTracker::add(BasicBlock &BB) {
338 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
342 void AliasSetTracker::add(const AliasSetTracker &AST) {
343 assert(&AA == &AST.AA &&
344 "Merging AliasSetTracker objects with different Alias Analyses!");
346 // Loop over all of the alias sets in AST, adding the pointers contained
347 // therein into the current alias sets. This can cause alias sets to be
348 // merged together in the current AST.
349 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
350 if (I->Forward) continue; // Ignore forwarding alias sets
352 AliasSet &AS = const_cast<AliasSet&>(*I);
354 // If there are any call sites in the alias set, add them to this AST.
355 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
356 add(AS.CallSites[i]);
358 // Loop over all of the pointers in this alias set.
360 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
361 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
362 (AliasSet::AccessType)AS.AccessTy, X);
363 if (AS.isVolatile()) NewAS.setVolatile();
368 /// remove - Remove the specified (potentially non-empty) alias set from the
370 void AliasSetTracker::remove(AliasSet &AS) {
371 // Drop all call sites.
372 AS.CallSites.clear();
374 // Clear the alias set.
375 unsigned NumRefs = 0;
376 while (!AS.empty()) {
377 AliasSet::PointerRec *P = AS.PtrList;
379 Value *ValToRemove = P->getValue();
381 // Unlink and delete entry from the list of values.
384 // Remember how many references need to be dropped.
387 // Finally, remove the entry.
388 PointerMap.erase(ValToRemove);
391 // Stop using the alias set, removing it.
392 AS.RefCount -= NumRefs;
393 if (AS.RefCount == 0)
394 AS.removeFromTracker(*this);
397 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
398 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
399 if (!AS) return false;
404 bool AliasSetTracker::remove(LoadInst *LI) {
405 unsigned Size = AA.getTypeStoreSize(LI->getType());
406 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
407 if (!AS) return false;
412 bool AliasSetTracker::remove(StoreInst *SI) {
413 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
414 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
415 if (!AS) return false;
420 bool AliasSetTracker::remove(VAArgInst *VAAI) {
421 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
422 if (!AS) return false;
427 bool AliasSetTracker::remove(CallSite CS) {
428 if (AA.doesNotAccessMemory(CS))
429 return false; // doesn't alias anything
431 AliasSet *AS = findAliasSetForCallSite(CS);
432 if (!AS) return false;
437 bool AliasSetTracker::remove(Instruction *I) {
438 // Dispatch to one of the other remove methods...
439 if (LoadInst *LI = dyn_cast<LoadInst>(I))
441 if (StoreInst *SI = dyn_cast<StoreInst>(I))
443 if (CallInst *CI = dyn_cast<CallInst>(I))
445 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
451 // deleteValue method - This method is used to remove a pointer value from the
452 // AliasSetTracker entirely. It should be used when an instruction is deleted
453 // from the program to update the AST. If you don't use this, you would have
454 // dangling pointers to deleted instructions.
456 void AliasSetTracker::deleteValue(Value *PtrVal) {
457 // Notify the alias analysis implementation that this value is gone.
458 AA.deleteValue(PtrVal);
460 // If this is a call instruction, remove the callsite from the appropriate
462 if (CallSite CS = PtrVal)
463 if (!AA.doesNotAccessMemory(CS))
464 if (AliasSet *AS = findAliasSetForCallSite(CS))
465 AS->removeCallSite(CS);
467 // First, look up the PointerRec for this pointer.
468 PointerMapType::iterator I = PointerMap.find(PtrVal);
469 if (I == PointerMap.end()) return; // Noop
471 // If we found one, remove the pointer from the alias set it is in.
472 AliasSet::PointerRec *PtrValEnt = I->second;
473 AliasSet *AS = PtrValEnt->getAliasSet(*this);
475 // Unlink and delete from the list of values.
476 PtrValEnt->eraseFromList();
478 // Stop using the alias set.
484 // copyValue - This method should be used whenever a preexisting value in the
485 // program is copied or cloned, introducing a new value. Note that it is ok for
486 // clients that use this method to introduce the same value multiple times: if
487 // the tracker already knows about a value, it will ignore the request.
489 void AliasSetTracker::copyValue(Value *From, Value *To) {
490 // Notify the alias analysis implementation that this value is copied.
491 AA.copyValue(From, To);
493 // First, look up the PointerRec for this pointer.
494 PointerMapType::iterator I = PointerMap.find(From);
495 if (I == PointerMap.end())
497 assert(I->second->hasAliasSet() && "Dead entry?");
499 AliasSet::PointerRec &Entry = getEntryFor(To);
500 if (Entry.hasAliasSet()) return; // Already in the tracker!
502 // Add it to the alias set it aliases...
503 I = PointerMap.find(From);
504 AliasSet *AS = I->second->getAliasSet(*this);
505 AS->addPointer(*this, Entry, I->second->getSize(), true);
510 //===----------------------------------------------------------------------===//
511 // AliasSet/AliasSetTracker Printing Support
512 //===----------------------------------------------------------------------===//
514 void AliasSet::print(raw_ostream &OS) const {
515 OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] ";
516 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
518 case NoModRef: OS << "No access "; break;
519 case Refs : OS << "Ref "; break;
520 case Mods : OS << "Mod "; break;
521 case ModRef : OS << "Mod/Ref "; break;
522 default: llvm_unreachable("Bad value for AccessTy!");
524 if (isVolatile()) OS << "[volatile] ";
526 OS << " forwarding to " << (void*)Forward;
531 for (iterator I = begin(), E = end(); I != E; ++I) {
532 if (I != begin()) OS << ", ";
533 WriteAsOperand(OS << "(", I.getPointer());
534 OS << ", " << I.getSize() << ")";
537 if (!CallSites.empty()) {
538 OS << "\n " << CallSites.size() << " Call Sites: ";
539 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
541 WriteAsOperand(OS, CallSites[i].getCalledValue());
547 void AliasSetTracker::print(raw_ostream &OS) const {
548 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
549 << PointerMap.size() << " pointer values.\n";
550 for (const_iterator I = begin(), E = end(); I != E; ++I)
555 void AliasSet::dump() const { print(dbgs()); }
556 void AliasSetTracker::dump() const { print(dbgs()); }
558 //===----------------------------------------------------------------------===//
559 // ASTCallbackVH Class Implementation
560 //===----------------------------------------------------------------------===//
562 void AliasSetTracker::ASTCallbackVH::deleted() {
563 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
564 AST->deleteValue(getValPtr());
568 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
569 : CallbackVH(V), AST(ast) {}
571 AliasSetTracker::ASTCallbackVH &
572 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
573 return *this = ASTCallbackVH(V, AST);
576 //===----------------------------------------------------------------------===//
577 // AliasSetPrinter Pass
578 //===----------------------------------------------------------------------===//
581 class AliasSetPrinter : public FunctionPass {
582 AliasSetTracker *Tracker;
584 static char ID; // Pass identification, replacement for typeid
585 AliasSetPrinter() : FunctionPass(ID) {}
587 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
588 AU.setPreservesAll();
589 AU.addRequired<AliasAnalysis>();
592 virtual bool runOnFunction(Function &F) {
593 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
595 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
597 Tracker->print(errs());
604 char AliasSetPrinter::ID = 0;
605 INITIALIZE_PASS(AliasSetPrinter, "print-alias-sets",
606 "Alias Set Printer", false, true);