1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Target/TargetData.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/Support/InstIterator.h"
24 /// mergeSetIn - Merge the specified alias set into this alias set...
26 void AliasSet::mergeSetIn(AliasSet &AS) {
27 assert(!AS.Forward && "Alias set is already forwarding!");
28 assert(!Forward && "This set is a forwarding set!!");
30 // Update the alias and access types of this set...
31 AccessTy |= AS.AccessTy;
32 AliasTy |= AS.AliasTy;
34 if (CallSites.empty()) { // Merge call sites...
35 if (!AS.CallSites.empty())
36 std::swap(CallSites, AS.CallSites);
37 } else if (!AS.CallSites.empty()) {
38 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
42 AS.Forward = this; // Forward across AS now...
43 addRef(); // AS is now pointing to us...
45 // Merge the list of constituent pointers...
47 *PtrListEnd = AS.PtrList;
48 AS.PtrList->second.setPrevInList(PtrListEnd);
49 PtrListEnd = AS.PtrListEnd;
52 AS.PtrListEnd = &AS.PtrList;
53 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
57 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
58 if (AliasSet *Fwd = AS->Forward) {
65 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
66 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
67 AST.removeAliasSet(this);
70 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
71 unsigned Size, bool KnownMustAlias) {
72 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
74 // Check to see if we have to downgrade to _may_ alias.
75 if (isMustAlias() && !KnownMustAlias)
76 if (HashNodePair *P = getSomePointer()) {
77 AliasAnalysis &AA = AST.getAliasAnalysis();
78 AliasAnalysis::AliasResult Result =
79 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
80 if (Result == AliasAnalysis::MayAlias)
82 else // First entry of must alias must have maximum size!
83 P->second.updateSize(Size);
84 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
87 Entry.second.setAliasSet(this);
88 Entry.second.updateSize(Size);
90 // Add it to the end of the list...
91 assert(*PtrListEnd == 0 && "End of list is not null?");
93 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
94 assert(*PtrListEnd == 0 && "End of list is not null?");
95 addRef(); // Entry points to alias set...
98 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
99 CallSites.push_back(CS);
101 if (Function *F = CS.getCalledFunction()) {
102 if (AA.doesNotAccessMemory(F))
104 else if (AA.onlyReadsMemory(F)) {
111 // FIXME: This should use mod/ref information to make this not suck so bad
116 /// aliasesPointer - Return true if the specified pointer "may" (or must)
117 /// alias one of the members in the set.
119 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
120 AliasAnalysis &AA) const {
121 if (AliasTy == MustAlias) {
122 assert(CallSites.empty() && "Illegal must alias set!");
124 // If this is a set of MustAliases, only check to see if the pointer aliases
125 // SOME value in the set...
126 HashNodePair *SomePtr = getSomePointer();
127 assert(SomePtr && "Empty must-alias set??");
128 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
131 // If this is a may-alias set, we have to check all of the pointers in the set
132 // to be sure it doesn't alias the set...
133 for (iterator I = begin(), E = end(); I != E; ++I)
134 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
137 // Check the call sites list and invoke list...
138 if (!CallSites.empty()) {
139 if (AA.hasNoModRefInfoForCalls())
142 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
143 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
144 != AliasAnalysis::NoModRef)
151 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
152 if (Function *F = CS.getCalledFunction())
153 if (AA.doesNotAccessMemory(F))
156 if (AA.hasNoModRefInfoForCalls())
159 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
160 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
161 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
164 for (iterator I = begin(), E = end(); I != E; ++I)
165 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
166 AliasAnalysis::NoModRef)
173 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
174 /// instruction referring to the pointer into. If there are multiple alias sets
175 /// that may alias the pointer, merge them together and return the unified set.
177 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
179 AliasSet *FoundSet = 0;
180 for (iterator I = begin(), E = end(); I != E; ++I)
181 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
182 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
183 FoundSet = I; // Remember it.
184 } else { // Otherwise, we must merge the sets...
185 FoundSet->mergeSetIn(*I); // Merge in contents...
192 /// containsPointer - Return true if the specified location is represented by
193 /// this alias set, false otherwise. This does not modify the AST object or
195 bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
196 for (const_iterator I = begin(), E = end(); I != E; ++I)
197 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
204 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
205 AliasSet *FoundSet = 0;
206 for (iterator I = begin(), E = end(); I != E; ++I)
207 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
208 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
209 FoundSet = I; // Remember it.
210 } else if (!I->Forward) { // Otherwise, we must merge the sets...
211 FoundSet->mergeSetIn(*I); // Merge in contents...
221 /// getAliasSetForPointer - Return the alias set that the specified pointer
223 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
225 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
227 // Check to see if the pointer is already known...
228 if (Entry.second.hasAliasSet()) {
229 Entry.second.updateSize(Size);
231 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
232 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
233 // Add it to the alias set it aliases...
234 AS->addPointer(*this, Entry, Size);
237 if (New) *New = true;
238 // Otherwise create a new alias set to hold the loaded pointer...
239 AliasSets.push_back(new AliasSet());
240 AliasSets.back().addPointer(*this, Entry, Size);
241 return AliasSets.back();
245 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
247 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
252 bool AliasSetTracker::add(LoadInst *LI) {
254 AliasSet &AS = addPointer(LI->getOperand(0),
255 AA.getTargetData().getTypeSize(LI->getType()),
256 AliasSet::Refs, NewPtr);
257 if (LI->isVolatile()) AS.setVolatile();
261 bool AliasSetTracker::add(StoreInst *SI) {
263 Value *Val = SI->getOperand(0);
264 AliasSet &AS = addPointer(SI->getOperand(1),
265 AA.getTargetData().getTypeSize(Val->getType()),
266 AliasSet::Mods, NewPtr);
267 if (SI->isVolatile()) AS.setVolatile();
271 bool AliasSetTracker::add(FreeInst *FI) {
273 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
274 AliasSet::Mods, NewPtr);
279 bool AliasSetTracker::add(CallSite CS) {
280 if (Function *F = CS.getCalledFunction())
281 if (AA.doesNotAccessMemory(F))
282 return true; // doesn't alias anything
284 AliasSet *AS = findAliasSetForCallSite(CS);
286 AliasSets.push_back(new AliasSet());
287 AS = &AliasSets.back();
288 AS->addCallSite(CS, AA);
291 AS->addCallSite(CS, AA);
296 bool AliasSetTracker::add(Instruction *I) {
297 // Dispatch to one of the other add methods...
298 if (LoadInst *LI = dyn_cast<LoadInst>(I))
300 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
302 else if (CallInst *CI = dyn_cast<CallInst>(I))
304 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
306 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
311 void AliasSetTracker::add(BasicBlock &BB) {
312 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
316 void AliasSetTracker::add(const AliasSetTracker &AST) {
317 assert(&AA == &AST.AA &&
318 "Merging AliasSetTracker objects with different Alias Analyses!");
320 // Loop over all of the alias sets in AST, adding the pointers contained
321 // therein into the current alias sets. This can cause alias sets to be
322 // merged together in the current AST.
323 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
324 if (!I->Forward) { // Ignore forwarding alias sets
325 AliasSet &AS = const_cast<AliasSet&>(*I);
327 // If there are any call sites in the alias set, add them to this AST.
328 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
329 add(AS.CallSites[i]);
331 // Loop over all of the pointers in this alias set...
332 AliasSet::iterator I = AS.begin(), E = AS.end();
335 addPointer(I.getPointer(), I.getSize(),
336 (AliasSet::AccessType)AS.AccessTy, X);
340 /// remove - Remove the specified (potentially non-empty) alias set from the
342 void AliasSetTracker::remove(AliasSet &AS) {
345 AliasSet::iterator I = AS.begin();
346 Value *Ptr = I.getPointer(); ++I;
348 // deleteValue will delete the set automatically when the last pointer
349 // reference is destroyed. "Predict" when this will happen.
350 SetDead = I == AS.end();
351 deleteValue(Ptr); // Delete all of the pointers from the set
355 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
356 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
357 if (!AS) return false;
362 bool AliasSetTracker::remove(LoadInst *LI) {
363 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
364 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
365 if (!AS) return false;
370 bool AliasSetTracker::remove(StoreInst *SI) {
371 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
372 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
373 if (!AS) return false;
378 bool AliasSetTracker::remove(FreeInst *FI) {
379 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
380 if (!AS) return false;
385 bool AliasSetTracker::remove(CallSite CS) {
386 if (Function *F = CS.getCalledFunction())
387 if (AA.doesNotAccessMemory(F))
388 return false; // doesn't alias anything
390 AliasSet *AS = findAliasSetForCallSite(CS);
391 if (!AS) return false;
396 bool AliasSetTracker::remove(Instruction *I) {
397 // Dispatch to one of the other remove methods...
398 if (LoadInst *LI = dyn_cast<LoadInst>(I))
400 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
402 else if (CallInst *CI = dyn_cast<CallInst>(I))
404 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
410 // deleteValue method - This method is used to remove a pointer value from the
411 // AliasSetTracker entirely. It should be used when an instruction is deleted
412 // from the program to update the AST. If you don't use this, you would have
413 // dangling pointers to deleted instructions.
415 void AliasSetTracker::deleteValue(Value *PtrVal) {
416 // Notify the alias analysis implementation that this value is gone.
417 AA.deleteValue(PtrVal);
419 // First, look up the PointerRec for this pointer.
420 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
421 if (I == PointerMap.end()) return; // Noop
423 // If we found one, remove the pointer from the alias set it is in.
424 AliasSet::HashNodePair &PtrValEnt = *I;
425 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
427 // Unlink from the list of values...
428 PtrValEnt.second.removeFromList();
429 // Stop using the alias set
434 // copyValue - This method should be used whenever a preexisting value in the
435 // program is copied or cloned, introducing a new value. Note that it is ok for
436 // clients that use this method to introduce the same value multiple times: if
437 // the tracker already knows about a value, it will ignore the request.
439 void AliasSetTracker::copyValue(Value *From, Value *To) {
440 // Notify the alias analysis implementation that this value is copied.
441 AA.copyValue(From, To);
443 // First, look up the PointerRec for this pointer.
444 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
445 if (I == PointerMap.end() || !I->second.hasAliasSet())
448 AliasSet::HashNodePair &Entry = getEntryFor(To);
449 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
451 // Add it to the alias set it aliases...
452 AliasSet *AS = I->second.getAliasSet(*this);
453 AS->addPointer(*this, Entry, I->second.getSize(), true);
458 //===----------------------------------------------------------------------===//
459 // AliasSet/AliasSetTracker Printing Support
460 //===----------------------------------------------------------------------===//
462 void AliasSet::print(std::ostream &OS) const {
463 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
464 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
466 case NoModRef: OS << "No access "; break;
467 case Refs : OS << "Ref "; break;
468 case Mods : OS << "Mod "; break;
469 case ModRef : OS << "Mod/Ref "; break;
470 default: assert(0 && "Bad value for AccessTy!");
472 if (isVolatile()) OS << "[volatile] ";
474 OS << " forwarding to " << (void*)Forward;
477 if (begin() != end()) {
479 for (iterator I = begin(), E = end(); I != E; ++I) {
480 if (I != begin()) OS << ", ";
481 WriteAsOperand(OS << "(", I.getPointer());
482 OS << ", " << I.getSize() << ")";
485 if (!CallSites.empty()) {
486 OS << "\n " << CallSites.size() << " Call Sites: ";
487 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
489 WriteAsOperand(OS, CallSites[i].getCalledValue());
495 void AliasSetTracker::print(std::ostream &OS) const {
496 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
497 << PointerMap.size() << " pointer values.\n";
498 for (const_iterator I = begin(), E = end(); I != E; ++I)
503 void AliasSet::dump() const { print (std::cerr); }
504 void AliasSetTracker::dump() const { print(std::cerr); }
506 //===----------------------------------------------------------------------===//
507 // AliasSetPrinter Pass
508 //===----------------------------------------------------------------------===//
511 class AliasSetPrinter : public FunctionPass {
512 AliasSetTracker *Tracker;
514 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
515 AU.setPreservesAll();
516 AU.addRequired<AliasAnalysis>();
519 virtual bool runOnFunction(Function &F) {
520 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
522 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
527 /// print - Convert to human readable form
528 virtual void print(std::ostream &OS) const {
532 virtual void releaseMemory() {
536 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
537 PassInfo::Analysis | PassInfo::Optimization);