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/Pass.h"
18 #include "llvm/Type.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/InstIterator.h"
23 #include "llvm/Support/Streams.h"
26 /// mergeSetIn - Merge the specified alias set into this alias set.
28 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
29 assert(!AS.Forward && "Alias set is already forwarding!");
30 assert(!Forward && "This set is a forwarding set!!");
32 // Update the alias and access types of this set...
33 AccessTy |= AS.AccessTy;
34 AliasTy |= AS.AliasTy;
36 if (AliasTy == MustAlias) {
37 // Check that these two merged sets really are must aliases. Since both
38 // used to be must-alias sets, we can just check any pointer from each set
40 AliasAnalysis &AA = AST.getAliasAnalysis();
41 HashNodePair *L = getSomePointer();
42 HashNodePair *R = AS.getSomePointer();
44 // If the pointers are not a must-alias pair, this set becomes a may alias.
45 if (AA.alias(L->first, L->second.getSize(), R->first, R->second.getSize())
46 != AliasAnalysis::MustAlias)
50 if (CallSites.empty()) { // Merge call sites...
51 if (!AS.CallSites.empty())
52 std::swap(CallSites, AS.CallSites);
53 } else if (!AS.CallSites.empty()) {
54 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
58 AS.Forward = this; // Forward across AS now...
59 addRef(); // AS is now pointing to us...
61 // Merge the list of constituent pointers...
63 *PtrListEnd = AS.PtrList;
64 AS.PtrList->second.setPrevInList(PtrListEnd);
65 PtrListEnd = AS.PtrListEnd;
68 AS.PtrListEnd = &AS.PtrList;
69 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
73 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
74 if (AliasSet *Fwd = AS->Forward) {
81 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
82 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
83 AST.removeAliasSet(this);
86 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
87 unsigned Size, bool KnownMustAlias) {
88 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
90 // Check to see if we have to downgrade to _may_ alias.
91 if (isMustAlias() && !KnownMustAlias)
92 if (HashNodePair *P = getSomePointer()) {
93 AliasAnalysis &AA = AST.getAliasAnalysis();
94 AliasAnalysis::AliasResult Result =
95 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
96 if (Result == AliasAnalysis::MayAlias)
98 else // First entry of must alias must have maximum size!
99 P->second.updateSize(Size);
100 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
103 Entry.second.setAliasSet(this);
104 Entry.second.updateSize(Size);
106 // Add it to the end of the list...
107 assert(*PtrListEnd == 0 && "End of list is not null?");
108 *PtrListEnd = &Entry;
109 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
110 assert(*PtrListEnd == 0 && "End of list is not null?");
111 addRef(); // Entry points to alias set...
114 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
115 CallSites.push_back(CS);
117 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
118 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
120 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
126 // FIXME: This should use mod/ref information to make this not suck so bad
131 /// aliasesPointer - Return true if the specified pointer "may" (or must)
132 /// alias one of the members in the set.
134 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
135 AliasAnalysis &AA) const {
136 if (AliasTy == MustAlias) {
137 assert(CallSites.empty() && "Illegal must alias set!");
139 // If this is a set of MustAliases, only check to see if the pointer aliases
140 // SOME value in the set...
141 HashNodePair *SomePtr = getSomePointer();
142 assert(SomePtr && "Empty must-alias set??");
143 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
146 // If this is a may-alias set, we have to check all of the pointers in the set
147 // to be sure it doesn't alias the set...
148 for (iterator I = begin(), E = end(); I != E; ++I)
149 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
152 // Check the call sites list and invoke list...
153 if (!CallSites.empty()) {
154 if (AA.hasNoModRefInfoForCalls())
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 if (AA.hasNoModRefInfoForCalls())
173 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
174 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
175 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
178 for (iterator I = begin(), E = end(); I != E; ++I)
179 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
180 AliasAnalysis::NoModRef)
187 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
188 /// instruction referring to the pointer into. If there are multiple alias sets
189 /// that may alias the pointer, merge them together and return the unified set.
191 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
193 AliasSet *FoundSet = 0;
194 for (iterator I = begin(), E = end(); I != E; ++I)
195 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
196 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
197 FoundSet = I; // Remember it.
198 } else { // Otherwise, we must merge the sets.
199 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
206 /// containsPointer - Return true if the specified location is represented by
207 /// this alias set, false otherwise. This does not modify the AST object or
209 bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
210 for (const_iterator I = begin(), E = end(); I != E; ++I)
211 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
218 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
219 AliasSet *FoundSet = 0;
220 for (iterator I = begin(), E = end(); I != E; ++I)
221 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
222 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
223 FoundSet = I; // Remember it.
224 } else if (!I->Forward) { // Otherwise, we must merge the sets.
225 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
235 /// getAliasSetForPointer - Return the alias set that the specified pointer
237 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
239 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
241 // Check to see if the pointer is already known...
242 if (Entry.second.hasAliasSet()) {
243 Entry.second.updateSize(Size);
245 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
246 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
247 // Add it to the alias set it aliases...
248 AS->addPointer(*this, Entry, Size);
251 if (New) *New = true;
252 // Otherwise create a new alias set to hold the loaded pointer...
253 AliasSets.push_back(new AliasSet());
254 AliasSets.back().addPointer(*this, Entry, Size);
255 return AliasSets.back();
259 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
261 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
266 bool AliasSetTracker::add(LoadInst *LI) {
268 AliasSet &AS = addPointer(LI->getOperand(0),
269 AA.getTargetData().getTypeStoreSize(LI->getType()),
270 AliasSet::Refs, NewPtr);
271 if (LI->isVolatile()) AS.setVolatile();
275 bool AliasSetTracker::add(StoreInst *SI) {
277 Value *Val = SI->getOperand(0);
278 AliasSet &AS = addPointer(SI->getOperand(1),
279 AA.getTargetData().getTypeStoreSize(Val->getType()),
280 AliasSet::Mods, NewPtr);
281 if (SI->isVolatile()) AS.setVolatile();
285 bool AliasSetTracker::add(FreeInst *FI) {
287 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
288 AliasSet::Mods, NewPtr);
290 // Free operations are volatile ops (cannot be moved).
296 bool AliasSetTracker::add(CallSite CS) {
297 if (AA.doesNotAccessMemory(CS))
298 return true; // doesn't alias anything
300 AliasSet *AS = findAliasSetForCallSite(CS);
302 AliasSets.push_back(new AliasSet());
303 AS = &AliasSets.back();
304 AS->addCallSite(CS, AA);
307 AS->addCallSite(CS, AA);
312 bool AliasSetTracker::add(Instruction *I) {
313 // Dispatch to one of the other add methods...
314 if (LoadInst *LI = dyn_cast<LoadInst>(I))
316 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
318 else if (CallInst *CI = dyn_cast<CallInst>(I))
320 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
322 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
327 void AliasSetTracker::add(BasicBlock &BB) {
328 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
332 void AliasSetTracker::add(const AliasSetTracker &AST) {
333 assert(&AA == &AST.AA &&
334 "Merging AliasSetTracker objects with different Alias Analyses!");
336 // Loop over all of the alias sets in AST, adding the pointers contained
337 // therein into the current alias sets. This can cause alias sets to be
338 // merged together in the current AST.
339 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
340 if (!I->Forward) { // Ignore forwarding alias sets
341 AliasSet &AS = const_cast<AliasSet&>(*I);
343 // If there are any call sites in the alias set, add them to this AST.
344 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
345 add(AS.CallSites[i]);
347 // Loop over all of the pointers in this alias set...
348 AliasSet::iterator I = AS.begin(), E = AS.end();
350 for (; I != E; ++I) {
351 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
352 (AliasSet::AccessType)AS.AccessTy, X);
353 if (AS.isVolatile()) NewAS.setVolatile();
358 /// remove - Remove the specified (potentially non-empty) alias set from the
360 void AliasSetTracker::remove(AliasSet &AS) {
361 // Drop all call sites.
362 AS.CallSites.clear();
364 // Clear the alias set.
365 unsigned NumRefs = 0;
366 while (!AS.empty()) {
367 AliasSet::HashNodePair *P = AS.PtrList;
369 // Unlink from the list of values.
370 P->second.removeFromList();
372 // Remember how many references need to be dropped.
375 // Finally, remove the entry.
376 Value *Remove = P->first; // Take a copy because it is invalid to pass
377 PointerMap.erase(Remove); // a reference to the data being erased.
380 // Stop using the alias set, removing it.
381 AS.RefCount -= NumRefs;
382 if (AS.RefCount == 0)
383 AS.removeFromTracker(*this);
386 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
387 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
388 if (!AS) return false;
393 bool AliasSetTracker::remove(LoadInst *LI) {
394 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
395 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
396 if (!AS) return false;
401 bool AliasSetTracker::remove(StoreInst *SI) {
403 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
404 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
405 if (!AS) return false;
410 bool AliasSetTracker::remove(FreeInst *FI) {
411 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
412 if (!AS) return false;
417 bool AliasSetTracker::remove(CallSite CS) {
418 if (AA.doesNotAccessMemory(CS))
419 return false; // doesn't alias anything
421 AliasSet *AS = findAliasSetForCallSite(CS);
422 if (!AS) return false;
427 bool AliasSetTracker::remove(Instruction *I) {
428 // Dispatch to one of the other remove methods...
429 if (LoadInst *LI = dyn_cast<LoadInst>(I))
431 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
433 else if (CallInst *CI = dyn_cast<CallInst>(I))
435 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
441 // deleteValue method - This method is used to remove a pointer value from the
442 // AliasSetTracker entirely. It should be used when an instruction is deleted
443 // from the program to update the AST. If you don't use this, you would have
444 // dangling pointers to deleted instructions.
446 void AliasSetTracker::deleteValue(Value *PtrVal) {
447 // Notify the alias analysis implementation that this value is gone.
448 AA.deleteValue(PtrVal);
450 // If this is a call instruction, remove the callsite from the appropriate
452 CallSite CS = CallSite::get(PtrVal);
453 if (CS.getInstruction())
454 if (!AA.doesNotAccessMemory(CS))
455 if (AliasSet *AS = findAliasSetForCallSite(CS))
456 AS->removeCallSite(CS);
458 // First, look up the PointerRec for this pointer.
459 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
460 if (I == PointerMap.end()) return; // Noop
462 // If we found one, remove the pointer from the alias set it is in.
463 AliasSet::HashNodePair &PtrValEnt = *I;
464 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
466 // Unlink from the list of values...
467 PtrValEnt.second.removeFromList();
468 // Stop using the alias set
473 // copyValue - This method should be used whenever a preexisting value in the
474 // program is copied or cloned, introducing a new value. Note that it is ok for
475 // clients that use this method to introduce the same value multiple times: if
476 // the tracker already knows about a value, it will ignore the request.
478 void AliasSetTracker::copyValue(Value *From, Value *To) {
479 // Notify the alias analysis implementation that this value is copied.
480 AA.copyValue(From, To);
482 // First, look up the PointerRec for this pointer.
483 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
484 if (I == PointerMap.end() || !I->second.hasAliasSet())
487 AliasSet::HashNodePair &Entry = getEntryFor(To);
488 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
490 // Add it to the alias set it aliases...
491 AliasSet *AS = I->second.getAliasSet(*this);
492 AS->addPointer(*this, Entry, I->second.getSize(), true);
497 //===----------------------------------------------------------------------===//
498 // AliasSet/AliasSetTracker Printing Support
499 //===----------------------------------------------------------------------===//
501 void AliasSet::print(std::ostream &OS) const {
502 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
503 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
505 case NoModRef: OS << "No access "; break;
506 case Refs : OS << "Ref "; break;
507 case Mods : OS << "Mod "; break;
508 case ModRef : OS << "Mod/Ref "; break;
509 default: assert(0 && "Bad value for AccessTy!");
511 if (isVolatile()) OS << "[volatile] ";
513 OS << " forwarding to " << (void*)Forward;
518 for (iterator I = begin(), E = end(); I != E; ++I) {
519 if (I != begin()) OS << ", ";
520 WriteAsOperand(OS << "(", I.getPointer());
521 OS << ", " << I.getSize() << ")";
524 if (!CallSites.empty()) {
525 OS << "\n " << CallSites.size() << " Call Sites: ";
526 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
528 WriteAsOperand(OS, CallSites[i].getCalledValue());
534 void AliasSetTracker::print(std::ostream &OS) const {
535 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
536 << PointerMap.size() << " pointer values.\n";
537 for (const_iterator I = begin(), E = end(); I != E; ++I)
542 void AliasSet::dump() const { print (cerr); }
543 void AliasSetTracker::dump() const { print(cerr); }
545 //===----------------------------------------------------------------------===//
546 // AliasSetPrinter Pass
547 //===----------------------------------------------------------------------===//
550 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
551 AliasSetTracker *Tracker;
553 static char ID; // Pass identification, replacement for typeid
554 AliasSetPrinter() : FunctionPass((intptr_t)&ID) {}
556 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
557 AU.setPreservesAll();
558 AU.addRequired<AliasAnalysis>();
561 virtual bool runOnFunction(Function &F) {
562 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
564 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
566 Tracker->print(cerr);
571 char AliasSetPrinter::ID = 0;
572 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer");