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/iMemory.h"
17 #include "llvm/iOther.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/Support/InstIterator.h"
26 /// mergeSetIn - Merge the specified alias set into this alias set...
28 void AliasSet::mergeSetIn(AliasSet &AS) {
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 (CallSites.empty()) { // Merge call sites...
37 if (!AS.CallSites.empty())
38 std::swap(CallSites, AS.CallSites);
39 } else if (!AS.CallSites.empty()) {
40 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
44 // FIXME: If AS's refcount is zero, nuke it now...
45 assert(RefCount != 0);
47 AS.Forward = this; // Forward across AS now...
48 addRef(); // AS is now pointing to us...
50 // Merge the list of constituent pointers...
52 *PtrListEnd = AS.PtrList;
53 AS.PtrList->second.setPrevInList(PtrListEnd);
54 PtrListEnd = AS.PtrListEnd;
57 AS.PtrListEnd = &AS.PtrList;
61 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
62 if (AliasSet *Fwd = AS->Forward) {
69 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
70 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
71 AST.removeAliasSet(this);
74 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
76 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
78 AliasAnalysis &AA = AST.getAliasAnalysis();
80 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
81 if (HashNodePair *P = getSomePointer()) {
82 AliasAnalysis::AliasResult Result =
83 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
84 if (Result == AliasAnalysis::MayAlias)
86 else // First entry of must alias must have maximum size!
87 P->second.updateSize(Size);
88 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
91 Entry.second.setAliasSet(this);
92 Entry.second.updateSize(Size);
94 // Add it to the end of the list...
95 assert(*PtrListEnd == 0 && "End of list is not null?");
97 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
98 addRef(); // Entry points to alias set...
101 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
102 CallSites.push_back(CS);
104 if (Function *F = CS.getCalledFunction()) {
105 if (AA.doesNotAccessMemory(F))
107 else if (AA.onlyReadsMemory(F)) {
114 // FIXME: This should use mod/ref information to make this not suck so bad
119 /// aliasesPointer - Return true if the specified pointer "may" (or must)
120 /// alias one of the members in the set.
122 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
123 AliasAnalysis &AA) const {
124 if (AliasTy == MustAlias) {
125 assert(CallSites.empty() && "Illegal must alias set!");
127 // If this is a set of MustAliases, only check to see if the pointer aliases
128 // SOME value in the set...
129 HashNodePair *SomePtr = getSomePointer();
130 assert(SomePtr && "Empty must-alias set??");
131 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
134 // If this is a may-alias set, we have to check all of the pointers in the set
135 // to be sure it doesn't alias the set...
136 for (iterator I = begin(), E = end(); I != E; ++I)
137 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
140 // Check the call sites list and invoke list...
141 if (!CallSites.empty()) {
142 if (AA.hasNoModRefInfoForCalls())
145 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
146 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
147 != AliasAnalysis::NoModRef)
154 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
155 if (Function *F = CS.getCalledFunction())
156 if (AA.doesNotAccessMemory(F))
159 if (AA.hasNoModRefInfoForCalls())
162 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
163 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
164 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
167 for (iterator I = begin(), E = end(); I != E; ++I)
168 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
169 AliasAnalysis::NoModRef)
176 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
177 /// instruction referring to the pointer into. If there are multiple alias sets
178 /// that may alias the pointer, merge them together and return the unified set.
180 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
182 AliasSet *FoundSet = 0;
183 for (iterator I = begin(), E = end(); I != E; ++I)
184 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
185 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
186 FoundSet = I; // Remember it.
187 } else { // Otherwise, we must merge the sets...
188 FoundSet->mergeSetIn(*I); // Merge in contents...
195 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
196 AliasSet *FoundSet = 0;
197 for (iterator I = begin(), E = end(); I != E; ++I)
198 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
199 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
200 FoundSet = I; // Remember it.
201 } else if (!I->Forward) { // Otherwise, we must merge the sets...
202 FoundSet->mergeSetIn(*I); // Merge in contents...
212 /// getAliasSetForPointer - Return the alias set that the specified pointer
214 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
216 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
218 // Check to see if the pointer is already known...
219 if (Entry.second.hasAliasSet()) {
220 Entry.second.updateSize(Size);
222 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
223 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
224 // Add it to the alias set it aliases...
225 AS->addPointer(*this, Entry, Size);
228 if (New) *New = true;
229 // Otherwise create a new alias set to hold the loaded pointer...
230 AliasSets.push_back(new AliasSet());
231 AliasSets.back().addPointer(*this, Entry, Size);
232 return AliasSets.back();
236 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
238 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
243 bool AliasSetTracker::add(LoadInst *LI) {
245 AliasSet &AS = addPointer(LI->getOperand(0),
246 AA.getTargetData().getTypeSize(LI->getType()),
247 AliasSet::Refs, NewPtr);
248 if (LI->isVolatile()) AS.setVolatile();
252 bool AliasSetTracker::add(StoreInst *SI) {
254 Value *Val = SI->getOperand(0);
255 AliasSet &AS = addPointer(SI->getOperand(1),
256 AA.getTargetData().getTypeSize(Val->getType()),
257 AliasSet::Mods, NewPtr);
258 if (SI->isVolatile()) AS.setVolatile();
262 bool AliasSetTracker::add(FreeInst *FI) {
264 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
265 AliasSet::Mods, NewPtr);
270 bool AliasSetTracker::add(CallSite CS) {
272 if (Function *F = CS.getCalledFunction())
273 if (AA.doesNotAccessMemory(F))
274 return true; // doesn't alias anything
276 AliasSet *AS = findAliasSetForCallSite(CS);
278 AliasSets.push_back(new AliasSet());
279 AS = &AliasSets.back();
280 AS->addCallSite(CS, AA);
283 AS->addCallSite(CS, AA);
288 bool AliasSetTracker::add(Instruction *I) {
289 // Dispatch to one of the other add methods...
290 if (LoadInst *LI = dyn_cast<LoadInst>(I))
292 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
294 else if (CallInst *CI = dyn_cast<CallInst>(I))
296 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
298 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
303 void AliasSetTracker::add(BasicBlock &BB) {
304 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
308 void AliasSetTracker::add(const AliasSetTracker &AST) {
309 assert(&AA == &AST.AA &&
310 "Merging AliasSetTracker objects with different Alias Analyses!");
312 // Loop over all of the alias sets in AST, adding the pointers contained
313 // therein into the current alias sets. This can cause alias sets to be
314 // merged together in the current AST.
315 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
316 if (!I->Forward) { // Ignore forwarding alias sets
317 AliasSet &AS = const_cast<AliasSet&>(*I);
319 // If there are any call sites in the alias set, add them to this AST.
320 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
321 add(AS.CallSites[i]);
323 // Loop over all of the pointers in this alias set...
324 AliasSet::iterator I = AS.begin(), E = AS.end();
327 addPointer(I.getPointer(), I.getSize(),
328 (AliasSet::AccessType)AS.AccessTy, X);
332 /// remove - Remove the specified (potentially non-empty) alias set from the
334 void AliasSetTracker::remove(AliasSet &AS) {
337 AliasSet::iterator I = AS.begin();
338 Value *Ptr = I.getPointer(); ++I;
340 // deleteValue will delete the set automatically when the last pointer
341 // reference is destroyed. "Predict" when this will happen.
342 SetDead = I == AS.end();
343 deleteValue(Ptr); // Delete all of the pointers from the set
347 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
348 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
349 if (!AS) return false;
354 bool AliasSetTracker::remove(LoadInst *LI) {
355 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
356 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
357 if (!AS) return false;
362 bool AliasSetTracker::remove(StoreInst *SI) {
363 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
364 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
365 if (!AS) return false;
370 bool AliasSetTracker::remove(FreeInst *FI) {
371 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
372 if (!AS) return false;
377 bool AliasSetTracker::remove(CallSite CS) {
378 if (Function *F = CS.getCalledFunction())
379 if (AA.doesNotAccessMemory(F))
380 return false; // doesn't alias anything
382 AliasSet *AS = findAliasSetForCallSite(CS);
383 if (!AS) return false;
388 bool AliasSetTracker::remove(Instruction *I) {
389 // Dispatch to one of the other remove methods...
390 if (LoadInst *LI = dyn_cast<LoadInst>(I))
392 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
394 else if (CallInst *CI = dyn_cast<CallInst>(I))
396 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
402 // deleteValue method - This method is used to remove a pointer value from the
403 // AliasSetTracker entirely. It should be used when an instruction is deleted
404 // from the program to update the AST. If you don't use this, you would have
405 // dangling pointers to deleted instructions.
407 void AliasSetTracker::deleteValue(Value *PtrVal) {
408 // First, look up the PointerRec for this pointer...
409 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
410 if (I == PointerMap.end()) return; // Noop
412 // If we found one, remove the pointer from the alias set it is in.
413 AliasSet::HashNodePair &PtrValEnt = *I;
414 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
416 // Unlink from the list of values...
417 PtrValEnt.second.removeFromList();
418 // Stop using the alias set
424 //===----------------------------------------------------------------------===//
425 // AliasSet/AliasSetTracker Printing Support
426 //===----------------------------------------------------------------------===//
428 void AliasSet::print(std::ostream &OS) const {
429 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
430 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
432 case NoModRef: OS << "No access "; break;
433 case Refs : OS << "Ref "; break;
434 case Mods : OS << "Mod "; break;
435 case ModRef : OS << "Mod/Ref "; break;
436 default: assert(0 && "Bad value for AccessTy!");
438 if (isVolatile()) OS << "[volatile] ";
440 OS << " forwarding to " << (void*)Forward;
443 if (begin() != end()) {
445 for (iterator I = begin(), E = end(); I != E; ++I) {
446 if (I != begin()) OS << ", ";
447 WriteAsOperand(OS << "(", I.getPointer());
448 OS << ", " << I.getSize() << ")";
451 if (!CallSites.empty()) {
452 OS << "\n " << CallSites.size() << " Call Sites: ";
453 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
455 WriteAsOperand(OS, CallSites[i].getCalledValue());
461 void AliasSetTracker::print(std::ostream &OS) const {
462 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
463 << PointerMap.size() << " pointer values.\n";
464 for (const_iterator I = begin(), E = end(); I != E; ++I)
469 void AliasSet::dump() const { print (std::cerr); }
470 void AliasSetTracker::dump() const { print(std::cerr); }
472 //===----------------------------------------------------------------------===//
473 // AliasSetPrinter Pass
474 //===----------------------------------------------------------------------===//
477 class AliasSetPrinter : public FunctionPass {
478 AliasSetTracker *Tracker;
480 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
481 AU.setPreservesAll();
482 AU.addRequired<AliasAnalysis>();
485 virtual bool runOnFunction(Function &F) {
486 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
488 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
493 /// print - Convert to human readable form
494 virtual void print(std::ostream &OS) const {
498 virtual void releaseMemory() {
502 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
503 PassInfo::Analysis | PassInfo::Optimization);