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 // FIXME: this is pessimistic!
148 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
149 // FIXME: Use mod/ref information to prune this better!
150 if (Function *F = CS.getCalledFunction())
151 if (AA.doesNotAccessMemory(F))
158 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
159 /// instruction referring to the pointer into. If there are multiple alias sets
160 /// that may alias the pointer, merge them together and return the unified set.
162 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
164 AliasSet *FoundSet = 0;
165 for (iterator I = begin(), E = end(); I != E; ++I)
166 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
167 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
168 FoundSet = I; // Remember it.
169 } else { // Otherwise, we must merge the sets...
170 FoundSet->mergeSetIn(*I); // Merge in contents...
177 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
178 AliasSet *FoundSet = 0;
179 for (iterator I = begin(), E = end(); I != E; ++I)
180 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
181 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
182 FoundSet = I; // Remember it.
183 } else if (!I->Forward) { // Otherwise, we must merge the sets...
184 FoundSet->mergeSetIn(*I); // Merge in contents...
194 /// getAliasSetForPointer - Return the alias set that the specified pointer
196 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
198 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
200 // Check to see if the pointer is already known...
201 if (Entry.second.hasAliasSet()) {
202 Entry.second.updateSize(Size);
204 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
205 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
206 // Add it to the alias set it aliases...
207 AS->addPointer(*this, Entry, Size);
210 if (New) *New = true;
211 // Otherwise create a new alias set to hold the loaded pointer...
212 AliasSets.push_back(new AliasSet());
213 AliasSets.back().addPointer(*this, Entry, Size);
214 return AliasSets.back();
218 bool AliasSetTracker::add(LoadInst *LI) {
220 AliasSet &AS = addPointer(LI->getOperand(0),
221 AA.getTargetData().getTypeSize(LI->getType()),
222 AliasSet::Refs, NewPtr);
223 if (LI->isVolatile()) AS.setVolatile();
227 bool AliasSetTracker::add(StoreInst *SI) {
229 Value *Val = SI->getOperand(0);
230 AliasSet &AS = addPointer(SI->getOperand(1),
231 AA.getTargetData().getTypeSize(Val->getType()),
232 AliasSet::Mods, NewPtr);
233 if (SI->isVolatile()) AS.setVolatile();
237 bool AliasSetTracker::add(FreeInst *FI) {
239 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
240 AliasSet::Mods, NewPtr);
245 bool AliasSetTracker::add(CallSite CS) {
247 if (Function *F = CS.getCalledFunction())
248 if (AA.doesNotAccessMemory(F))
249 return true; // doesn't alias anything
251 AliasSet *AS = findAliasSetForCallSite(CS);
253 AliasSets.push_back(new AliasSet());
254 AS = &AliasSets.back();
255 AS->addCallSite(CS, AA);
258 AS->addCallSite(CS, AA);
263 bool AliasSetTracker::add(Instruction *I) {
264 // Dispatch to one of the other add methods...
265 if (LoadInst *LI = dyn_cast<LoadInst>(I))
267 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
269 else if (CallInst *CI = dyn_cast<CallInst>(I))
271 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
273 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
278 void AliasSetTracker::add(BasicBlock &BB) {
279 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
283 void AliasSetTracker::add(const AliasSetTracker &AST) {
284 assert(&AA == &AST.AA &&
285 "Merging AliasSetTracker objects with different Alias Analyses!");
287 // Loop over all of the alias sets in AST, adding the pointers contained
288 // therein into the current alias sets. This can cause alias sets to be
289 // merged together in the current AST.
290 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
291 if (!I->Forward) { // Ignore forwarding alias sets
292 AliasSet &AS = const_cast<AliasSet&>(*I);
294 // If there are any call sites in the alias set, add them to this AST.
295 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
296 add(AS.CallSites[i]);
298 // Loop over all of the pointers in this alias set...
299 AliasSet::iterator I = AS.begin(), E = AS.end();
302 addPointer(I.getPointer(), I.getSize(),
303 (AliasSet::AccessType)AS.AccessTy, X);
307 /// remove - Remove the specified (potentially non-empty) alias set from the
309 void AliasSetTracker::remove(AliasSet &AS) {
312 AliasSet::iterator I = AS.begin();
313 Value *Ptr = I.getPointer(); ++I;
315 // deleteValue will delete the set automatically when the last pointer
316 // reference is destroyed. "Predict" when this will happen.
317 SetDead = I == AS.end();
318 deleteValue(Ptr); // Delete all of the pointers from the set
323 bool AliasSetTracker::remove(LoadInst *LI) {
324 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
325 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
326 if (!AS) return false;
331 bool AliasSetTracker::remove(StoreInst *SI) {
332 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
333 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
334 if (!AS) return false;
339 bool AliasSetTracker::remove(FreeInst *FI) {
340 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
341 if (!AS) return false;
346 bool AliasSetTracker::remove(CallSite CS) {
347 if (Function *F = CS.getCalledFunction())
348 if (AA.doesNotAccessMemory(F))
349 return false; // doesn't alias anything
351 AliasSet *AS = findAliasSetForCallSite(CS);
352 if (!AS) return false;
357 bool AliasSetTracker::remove(Instruction *I) {
358 // Dispatch to one of the other remove methods...
359 if (LoadInst *LI = dyn_cast<LoadInst>(I))
361 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
363 else if (CallInst *CI = dyn_cast<CallInst>(I))
365 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
371 // deleteValue method - This method is used to remove a pointer value from the
372 // AliasSetTracker entirely. It should be used when an instruction is deleted
373 // from the program to update the AST. If you don't use this, you would have
374 // dangling pointers to deleted instructions.
376 void AliasSetTracker::deleteValue(Value *PtrVal) {
377 // First, look up the PointerRec for this pointer...
378 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
379 if (I == PointerMap.end()) return; // Noop
381 // If we found one, remove the pointer from the alias set it is in.
382 AliasSet::HashNodePair &PtrValEnt = *I;
383 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
385 // Unlink from the list of values...
386 PtrValEnt.second.removeFromList();
387 // Stop using the alias set
393 //===----------------------------------------------------------------------===//
394 // AliasSet/AliasSetTracker Printing Support
395 //===----------------------------------------------------------------------===//
397 void AliasSet::print(std::ostream &OS) const {
398 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
399 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
401 case NoModRef: OS << "No access "; break;
402 case Refs : OS << "Ref "; break;
403 case Mods : OS << "Mod "; break;
404 case ModRef : OS << "Mod/Ref "; break;
405 default: assert(0 && "Bad value for AccessTy!");
407 if (isVolatile()) OS << "[volatile] ";
409 OS << " forwarding to " << (void*)Forward;
412 if (begin() != end()) {
414 for (iterator I = begin(), E = end(); I != E; ++I) {
415 if (I != begin()) OS << ", ";
416 WriteAsOperand(OS << "(", I.getPointer());
417 OS << ", " << I.getSize() << ")";
420 if (!CallSites.empty()) {
421 OS << "\n " << CallSites.size() << " Call Sites: ";
422 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
424 WriteAsOperand(OS, CallSites[i].getCalledValue());
430 void AliasSetTracker::print(std::ostream &OS) const {
431 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
432 << PointerMap.size() << " pointer values.\n";
433 for (const_iterator I = begin(), E = end(); I != E; ++I)
438 void AliasSet::dump() const { print (std::cerr); }
439 void AliasSetTracker::dump() const { print(std::cerr); }
441 //===----------------------------------------------------------------------===//
442 // AliasSetPrinter Pass
443 //===----------------------------------------------------------------------===//
446 class AliasSetPrinter : public FunctionPass {
447 AliasSetTracker *Tracker;
449 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
450 AU.setPreservesAll();
451 AU.addRequired<AliasAnalysis>();
454 virtual bool runOnFunction(Function &F) {
455 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
457 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
462 /// print - Convert to human readable form
463 virtual void print(std::ostream &OS) const {
467 virtual void releaseMemory() {
471 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
472 PassInfo::Analysis | PassInfo::Optimization);