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/InstIterator.h"
24 #include "llvm/Support/Streams.h"
27 /// mergeSetIn - Merge the specified alias set into this alias set.
29 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
30 assert(!AS.Forward && "Alias set is already forwarding!");
31 assert(!Forward && "This set is a forwarding set!!");
33 // Update the alias and access types of this set...
34 AccessTy |= AS.AccessTy;
35 AliasTy |= AS.AliasTy;
37 if (AliasTy == MustAlias) {
38 // Check that these two merged sets really are must aliases. Since both
39 // used to be must-alias sets, we can just check any pointer from each set
41 AliasAnalysis &AA = AST.getAliasAnalysis();
42 HashNodePair *L = getSomePointer();
43 HashNodePair *R = AS.getSomePointer();
45 // If the pointers are not a must-alias pair, this set becomes a may alias.
46 if (AA.alias(L->first, L->second.getSize(), R->first, R->second.getSize())
47 != AliasAnalysis::MustAlias)
51 if (CallSites.empty()) { // Merge call sites...
52 if (!AS.CallSites.empty())
53 std::swap(CallSites, AS.CallSites);
54 } else if (!AS.CallSites.empty()) {
55 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
59 AS.Forward = this; // Forward across AS now...
60 addRef(); // AS is now pointing to us...
62 // Merge the list of constituent pointers...
64 *PtrListEnd = AS.PtrList;
65 AS.PtrList->second.setPrevInList(PtrListEnd);
66 PtrListEnd = AS.PtrListEnd;
69 AS.PtrListEnd = &AS.PtrList;
70 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
74 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
75 if (AliasSet *Fwd = AS->Forward) {
82 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
83 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
84 AST.removeAliasSet(this);
87 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
88 unsigned Size, bool KnownMustAlias) {
89 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
91 // Check to see if we have to downgrade to _may_ alias.
92 if (isMustAlias() && !KnownMustAlias)
93 if (HashNodePair *P = getSomePointer()) {
94 AliasAnalysis &AA = AST.getAliasAnalysis();
95 AliasAnalysis::AliasResult Result =
96 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
97 if (Result == AliasAnalysis::MayAlias)
99 else // First entry of must alias must have maximum size!
100 P->second.updateSize(Size);
101 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
104 Entry.second.setAliasSet(this);
105 Entry.second.updateSize(Size);
107 // Add it to the end of the list...
108 assert(*PtrListEnd == 0 && "End of list is not null?");
109 *PtrListEnd = &Entry;
110 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
111 assert(*PtrListEnd == 0 && "End of list is not null?");
112 addRef(); // Entry points to alias set...
115 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
116 CallSites.push_back(CS);
118 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
119 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
121 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
127 // FIXME: This should use mod/ref information to make this not suck so bad
132 /// aliasesPointer - Return true if the specified pointer "may" (or must)
133 /// alias one of the members in the set.
135 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
136 AliasAnalysis &AA) const {
137 if (AliasTy == MustAlias) {
138 assert(CallSites.empty() && "Illegal must alias set!");
140 // If this is a set of MustAliases, only check to see if the pointer aliases
141 // SOME value in the set...
142 HashNodePair *SomePtr = getSomePointer();
143 assert(SomePtr && "Empty must-alias set??");
144 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
147 // If this is a may-alias set, we have to check all of the pointers in the set
148 // to be sure it doesn't alias the set...
149 for (iterator I = begin(), E = end(); I != E; ++I)
150 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
153 // Check the call sites list and invoke list...
154 if (!CallSites.empty()) {
155 if (AA.hasNoModRefInfoForCalls())
158 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
159 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
160 != AliasAnalysis::NoModRef)
167 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
168 if (AA.doesNotAccessMemory(CS))
171 if (AA.hasNoModRefInfoForCalls())
174 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
175 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
176 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
179 for (iterator I = begin(), E = end(); I != E; ++I)
180 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
181 AliasAnalysis::NoModRef)
188 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
189 /// instruction referring to the pointer into. If there are multiple alias sets
190 /// that may alias the pointer, merge them together and return the unified set.
192 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
194 AliasSet *FoundSet = 0;
195 for (iterator I = begin(), E = end(); I != E; ++I)
196 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
197 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
198 FoundSet = I; // Remember it.
199 } else { // Otherwise, we must merge the sets.
200 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
207 /// containsPointer - Return true if the specified location is represented by
208 /// this alias set, false otherwise. This does not modify the AST object or
210 bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
211 for (const_iterator I = begin(), E = end(); I != E; ++I)
212 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
219 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
220 AliasSet *FoundSet = 0;
221 for (iterator I = begin(), E = end(); I != E; ++I)
222 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
223 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
224 FoundSet = I; // Remember it.
225 } else if (!I->Forward) { // Otherwise, we must merge the sets.
226 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
236 /// getAliasSetForPointer - Return the alias set that the specified pointer
238 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
240 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
242 // Check to see if the pointer is already known...
243 if (Entry.second.hasAliasSet()) {
244 Entry.second.updateSize(Size);
246 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
247 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
248 // Add it to the alias set it aliases...
249 AS->addPointer(*this, Entry, Size);
252 if (New) *New = true;
253 // Otherwise create a new alias set to hold the loaded pointer...
254 AliasSets.push_back(new AliasSet());
255 AliasSets.back().addPointer(*this, Entry, Size);
256 return AliasSets.back();
260 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
262 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
267 bool AliasSetTracker::add(LoadInst *LI) {
269 AliasSet &AS = addPointer(LI->getOperand(0),
270 AA.getTargetData().getTypeStoreSize(LI->getType()),
271 AliasSet::Refs, NewPtr);
272 if (LI->isVolatile()) AS.setVolatile();
276 bool AliasSetTracker::add(StoreInst *SI) {
278 Value *Val = SI->getOperand(0);
279 AliasSet &AS = addPointer(SI->getOperand(1),
280 AA.getTargetData().getTypeStoreSize(Val->getType()),
281 AliasSet::Mods, NewPtr);
282 if (SI->isVolatile()) AS.setVolatile();
286 bool AliasSetTracker::add(FreeInst *FI) {
288 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
292 bool AliasSetTracker::add(VAArgInst *VAAI) {
294 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
299 bool AliasSetTracker::add(CallSite CS) {
300 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
301 return true; // Ignore DbgInfo Intrinsics.
302 if (AA.doesNotAccessMemory(CS))
303 return true; // doesn't alias anything
305 AliasSet *AS = findAliasSetForCallSite(CS);
307 AliasSets.push_back(new AliasSet());
308 AS = &AliasSets.back();
309 AS->addCallSite(CS, AA);
312 AS->addCallSite(CS, AA);
317 bool AliasSetTracker::add(Instruction *I) {
318 // Dispatch to one of the other add methods...
319 if (LoadInst *LI = dyn_cast<LoadInst>(I))
321 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
323 else if (CallInst *CI = dyn_cast<CallInst>(I))
325 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
327 else if (FreeInst *FI = dyn_cast<FreeInst>(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::HashNodePair *P = AS.PtrList;
376 // Unlink from the list of values.
377 P->second.removeFromList();
379 // Remember how many references need to be dropped.
382 // Finally, remove the entry.
383 Value *Remove = P->first; // Take a copy because it is invalid to pass
384 PointerMap.erase(Remove); // a reference to the data being erased.
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.getTargetData().getTypeStoreSize(LI->getType());
402 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
403 if (!AS) return false;
408 bool AliasSetTracker::remove(StoreInst *SI) {
410 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
411 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
412 if (!AS) return false;
417 bool AliasSetTracker::remove(FreeInst *FI) {
418 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
419 if (!AS) return false;
424 bool AliasSetTracker::remove(VAArgInst *VAAI) {
425 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
426 if (!AS) return false;
431 bool AliasSetTracker::remove(CallSite CS) {
432 if (AA.doesNotAccessMemory(CS))
433 return false; // doesn't alias anything
435 AliasSet *AS = findAliasSetForCallSite(CS);
436 if (!AS) return false;
441 bool AliasSetTracker::remove(Instruction *I) {
442 // Dispatch to one of the other remove methods...
443 if (LoadInst *LI = dyn_cast<LoadInst>(I))
445 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
447 else if (CallInst *CI = dyn_cast<CallInst>(I))
449 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
451 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
457 // deleteValue method - This method is used to remove a pointer value from the
458 // AliasSetTracker entirely. It should be used when an instruction is deleted
459 // from the program to update the AST. If you don't use this, you would have
460 // dangling pointers to deleted instructions.
462 void AliasSetTracker::deleteValue(Value *PtrVal) {
463 // Notify the alias analysis implementation that this value is gone.
464 AA.deleteValue(PtrVal);
466 // If this is a call instruction, remove the callsite from the appropriate
468 CallSite CS = CallSite::get(PtrVal);
469 if (CS.getInstruction())
470 if (!AA.doesNotAccessMemory(CS))
471 if (AliasSet *AS = findAliasSetForCallSite(CS))
472 AS->removeCallSite(CS);
474 // First, look up the PointerRec for this pointer.
475 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
476 if (I == PointerMap.end()) return; // Noop
478 // If we found one, remove the pointer from the alias set it is in.
479 AliasSet::HashNodePair &PtrValEnt = *I;
480 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
482 // Unlink from the list of values...
483 PtrValEnt.second.removeFromList();
484 // Stop using the alias set
489 // copyValue - This method should be used whenever a preexisting value in the
490 // program is copied or cloned, introducing a new value. Note that it is ok for
491 // clients that use this method to introduce the same value multiple times: if
492 // the tracker already knows about a value, it will ignore the request.
494 void AliasSetTracker::copyValue(Value *From, Value *To) {
495 // Notify the alias analysis implementation that this value is copied.
496 AA.copyValue(From, To);
498 // First, look up the PointerRec for this pointer.
499 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
500 if (I == PointerMap.end() || !I->second.hasAliasSet())
503 AliasSet::HashNodePair &Entry = getEntryFor(To);
504 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
506 // Add it to the alias set it aliases...
507 AliasSet *AS = I->second.getAliasSet(*this);
508 AS->addPointer(*this, Entry, I->second.getSize(), true);
513 //===----------------------------------------------------------------------===//
514 // AliasSet/AliasSetTracker Printing Support
515 //===----------------------------------------------------------------------===//
517 void AliasSet::print(std::ostream &OS) const {
518 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
519 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
521 case NoModRef: OS << "No access "; break;
522 case Refs : OS << "Ref "; break;
523 case Mods : OS << "Mod "; break;
524 case ModRef : OS << "Mod/Ref "; break;
525 default: assert(0 && "Bad value for AccessTy!");
527 if (isVolatile()) OS << "[volatile] ";
529 OS << " forwarding to " << (void*)Forward;
534 for (iterator I = begin(), E = end(); I != E; ++I) {
535 if (I != begin()) OS << ", ";
536 WriteAsOperand(OS << "(", I.getPointer());
537 OS << ", " << I.getSize() << ")";
540 if (!CallSites.empty()) {
541 OS << "\n " << CallSites.size() << " Call Sites: ";
542 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
544 WriteAsOperand(OS, CallSites[i].getCalledValue());
550 void AliasSetTracker::print(std::ostream &OS) const {
551 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
552 << PointerMap.size() << " pointer values.\n";
553 for (const_iterator I = begin(), E = end(); I != E; ++I)
558 void AliasSet::dump() const { print (cerr); }
559 void AliasSetTracker::dump() const { print(cerr); }
561 //===----------------------------------------------------------------------===//
562 // AliasSetPrinter Pass
563 //===----------------------------------------------------------------------===//
566 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
567 AliasSetTracker *Tracker;
569 static char ID; // Pass identification, replacement for typeid
570 AliasSetPrinter() : FunctionPass(&ID) {}
572 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
573 AU.setPreservesAll();
574 AU.addRequired<AliasAnalysis>();
577 virtual bool runOnFunction(Function &F) {
578 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
580 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
582 Tracker->print(cerr);
589 char AliasSetPrinter::ID = 0;
590 static RegisterPass<AliasSetPrinter>
591 X("print-alias-sets", "Alias Set Printer", false, true);