Add InsertBranch() hook for tail mergeing
[llvm/msp430.git] / lib / Analysis / AliasSetTracker.cpp
blob18c2b66505f636811c4338b0974bb3bb1b3464e6
1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
25 using namespace llvm;
27 /// mergeSetIn - Merge the specified alias set into this alias set.
28 ///
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
40 // for aliasing.
41 AliasAnalysis &AA = AST.getAliasAnalysis();
42 PointerRec *L = getSomePointer();
43 PointerRec *R = AS.getSomePointer();
45 // If the pointers are not a must-alias pair, this set becomes a may alias.
46 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
47 != AliasAnalysis::MustAlias)
48 AliasTy = MayAlias;
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());
56 AS.CallSites.clear();
59 AS.Forward = this; // Forward across AS now...
60 addRef(); // AS is now pointing to us...
62 // Merge the list of constituent pointers...
63 if (AS.PtrList) {
64 *PtrListEnd = AS.PtrList;
65 AS.PtrList->setPrevInList(PtrListEnd);
66 PtrListEnd = AS.PtrListEnd;
68 AS.PtrList = 0;
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) {
76 Fwd->dropRef(*this);
77 AS->Forward = 0;
79 AliasSets.erase(AS);
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, PointerRec &Entry,
88 unsigned Size, bool KnownMustAlias) {
89 assert(!Entry.hasAliasSet() && "Entry already in set!");
91 // Check to see if we have to downgrade to _may_ alias.
92 if (isMustAlias() && !KnownMustAlias)
93 if (PointerRec *P = getSomePointer()) {
94 AliasAnalysis &AA = AST.getAliasAnalysis();
95 AliasAnalysis::AliasResult Result =
96 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
97 if (Result == AliasAnalysis::MayAlias)
98 AliasTy = MayAlias;
99 else // First entry of must alias must have maximum size!
100 P->updateSize(Size);
101 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
104 Entry.setAliasSet(this);
105 Entry.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.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)
120 return;
121 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
122 AliasTy = MayAlias;
123 AccessTy |= Refs;
124 return;
127 // FIXME: This should use mod/ref information to make this not suck so bad
128 AliasTy = MayAlias;
129 AccessTy = ModRef;
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 PointerRec *SomePtr = getSomePointer();
143 assert(SomePtr && "Empty must-alias set??");
144 return AA.alias(SomePtr->getValue(), SomePtr->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()))
151 return true;
153 // Check the call sites list and invoke list...
154 if (!CallSites.empty()) {
155 if (AA.hasNoModRefInfoForCalls())
156 return true;
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)
161 return true;
164 return false;
167 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
168 if (AA.doesNotAccessMemory(CS))
169 return false;
171 if (AA.hasNoModRefInfoForCalls())
172 return true;
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)
177 return true;
179 for (iterator I = begin(), E = end(); I != E; ++I)
180 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
181 AliasAnalysis::NoModRef)
182 return true;
184 return false;
187 void AliasSetTracker::clear() {
188 // Delete all the PointerRec entries.
189 for (DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.begin(),
190 E = PointerMap.end(); I != E; ++I)
191 I->second->eraseFromList();
193 PointerMap.clear();
195 // The alias sets should all be clear now.
196 AliasSets.clear();
200 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
201 /// instruction referring to the pointer into. If there are multiple alias sets
202 /// that may alias the pointer, merge them together and return the unified set.
204 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
205 unsigned Size) {
206 AliasSet *FoundSet = 0;
207 for (iterator I = begin(), E = end(); I != E; ++I)
208 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
209 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
210 FoundSet = I; // Remember it.
211 } else { // Otherwise, we must merge the sets.
212 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
216 return FoundSet;
219 /// containsPointer - Return true if the specified location is represented by
220 /// this alias set, false otherwise. This does not modify the AST object or
221 /// alias sets.
222 bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
223 for (const_iterator I = begin(), E = end(); I != E; ++I)
224 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
225 return true;
226 return false;
231 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
232 AliasSet *FoundSet = 0;
233 for (iterator I = begin(), E = end(); I != E; ++I)
234 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
235 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
236 FoundSet = I; // Remember it.
237 } else if (!I->Forward) { // Otherwise, we must merge the sets.
238 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
242 return FoundSet;
248 /// getAliasSetForPointer - Return the alias set that the specified pointer
249 /// lives in.
250 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
251 bool *New) {
252 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
254 // Check to see if the pointer is already known...
255 if (Entry.hasAliasSet()) {
256 Entry.updateSize(Size);
257 // Return the set!
258 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
259 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
260 // Add it to the alias set it aliases...
261 AS->addPointer(*this, Entry, Size);
262 return *AS;
263 } else {
264 if (New) *New = true;
265 // Otherwise create a new alias set to hold the loaded pointer...
266 AliasSets.push_back(new AliasSet());
267 AliasSets.back().addPointer(*this, Entry, Size);
268 return AliasSets.back();
272 bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
273 bool NewPtr;
274 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
275 return NewPtr;
279 bool AliasSetTracker::add(LoadInst *LI) {
280 bool NewPtr;
281 AliasSet &AS = addPointer(LI->getOperand(0),
282 AA.getTargetData().getTypeStoreSize(LI->getType()),
283 AliasSet::Refs, NewPtr);
284 if (LI->isVolatile()) AS.setVolatile();
285 return NewPtr;
288 bool AliasSetTracker::add(StoreInst *SI) {
289 bool NewPtr;
290 Value *Val = SI->getOperand(0);
291 AliasSet &AS = addPointer(SI->getOperand(1),
292 AA.getTargetData().getTypeStoreSize(Val->getType()),
293 AliasSet::Mods, NewPtr);
294 if (SI->isVolatile()) AS.setVolatile();
295 return NewPtr;
298 bool AliasSetTracker::add(FreeInst *FI) {
299 bool NewPtr;
300 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
301 return NewPtr;
304 bool AliasSetTracker::add(VAArgInst *VAAI) {
305 bool NewPtr;
306 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
307 return NewPtr;
311 bool AliasSetTracker::add(CallSite CS) {
312 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
313 return true; // Ignore DbgInfo Intrinsics.
314 if (AA.doesNotAccessMemory(CS))
315 return true; // doesn't alias anything
317 AliasSet *AS = findAliasSetForCallSite(CS);
318 if (!AS) {
319 AliasSets.push_back(new AliasSet());
320 AS = &AliasSets.back();
321 AS->addCallSite(CS, AA);
322 return true;
323 } else {
324 AS->addCallSite(CS, AA);
325 return false;
329 bool AliasSetTracker::add(Instruction *I) {
330 // Dispatch to one of the other add methods...
331 if (LoadInst *LI = dyn_cast<LoadInst>(I))
332 return add(LI);
333 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
334 return add(SI);
335 else if (CallInst *CI = dyn_cast<CallInst>(I))
336 return add(CI);
337 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
338 return add(II);
339 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
340 return add(FI);
341 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
342 return add(VAAI);
343 return true;
346 void AliasSetTracker::add(BasicBlock &BB) {
347 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
348 add(I);
351 void AliasSetTracker::add(const AliasSetTracker &AST) {
352 assert(&AA == &AST.AA &&
353 "Merging AliasSetTracker objects with different Alias Analyses!");
355 // Loop over all of the alias sets in AST, adding the pointers contained
356 // therein into the current alias sets. This can cause alias sets to be
357 // merged together in the current AST.
358 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
359 if (!I->Forward) { // Ignore forwarding alias sets
360 AliasSet &AS = const_cast<AliasSet&>(*I);
362 // If there are any call sites in the alias set, add them to this AST.
363 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
364 add(AS.CallSites[i]);
366 // Loop over all of the pointers in this alias set...
367 AliasSet::iterator I = AS.begin(), E = AS.end();
368 bool X;
369 for (; I != E; ++I) {
370 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
371 (AliasSet::AccessType)AS.AccessTy, X);
372 if (AS.isVolatile()) NewAS.setVolatile();
377 /// remove - Remove the specified (potentially non-empty) alias set from the
378 /// tracker.
379 void AliasSetTracker::remove(AliasSet &AS) {
380 // Drop all call sites.
381 AS.CallSites.clear();
383 // Clear the alias set.
384 unsigned NumRefs = 0;
385 while (!AS.empty()) {
386 AliasSet::PointerRec *P = AS.PtrList;
388 Value *ValToRemove = P->getValue();
390 // Unlink and delete entry from the list of values.
391 P->eraseFromList();
393 // Remember how many references need to be dropped.
394 ++NumRefs;
396 // Finally, remove the entry.
397 PointerMap.erase(ValToRemove);
400 // Stop using the alias set, removing it.
401 AS.RefCount -= NumRefs;
402 if (AS.RefCount == 0)
403 AS.removeFromTracker(*this);
406 bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
407 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
408 if (!AS) return false;
409 remove(*AS);
410 return true;
413 bool AliasSetTracker::remove(LoadInst *LI) {
414 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
415 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
416 if (!AS) return false;
417 remove(*AS);
418 return true;
421 bool AliasSetTracker::remove(StoreInst *SI) {
422 unsigned Size =
423 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
424 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
425 if (!AS) return false;
426 remove(*AS);
427 return true;
430 bool AliasSetTracker::remove(FreeInst *FI) {
431 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
432 if (!AS) return false;
433 remove(*AS);
434 return true;
437 bool AliasSetTracker::remove(VAArgInst *VAAI) {
438 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
439 if (!AS) return false;
440 remove(*AS);
441 return true;
444 bool AliasSetTracker::remove(CallSite CS) {
445 if (AA.doesNotAccessMemory(CS))
446 return false; // doesn't alias anything
448 AliasSet *AS = findAliasSetForCallSite(CS);
449 if (!AS) return false;
450 remove(*AS);
451 return true;
454 bool AliasSetTracker::remove(Instruction *I) {
455 // Dispatch to one of the other remove methods...
456 if (LoadInst *LI = dyn_cast<LoadInst>(I))
457 return remove(LI);
458 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
459 return remove(SI);
460 else if (CallInst *CI = dyn_cast<CallInst>(I))
461 return remove(CI);
462 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
463 return remove(FI);
464 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
465 return remove(VAAI);
466 return true;
470 // deleteValue method - This method is used to remove a pointer value from the
471 // AliasSetTracker entirely. It should be used when an instruction is deleted
472 // from the program to update the AST. If you don't use this, you would have
473 // dangling pointers to deleted instructions.
475 void AliasSetTracker::deleteValue(Value *PtrVal) {
476 // Notify the alias analysis implementation that this value is gone.
477 AA.deleteValue(PtrVal);
479 // If this is a call instruction, remove the callsite from the appropriate
480 // AliasSet.
481 CallSite CS = CallSite::get(PtrVal);
482 if (CS.getInstruction())
483 if (!AA.doesNotAccessMemory(CS))
484 if (AliasSet *AS = findAliasSetForCallSite(CS))
485 AS->removeCallSite(CS);
487 // First, look up the PointerRec for this pointer.
488 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(PtrVal);
489 if (I == PointerMap.end()) return; // Noop
491 // If we found one, remove the pointer from the alias set it is in.
492 AliasSet::PointerRec *PtrValEnt = I->second;
493 AliasSet *AS = PtrValEnt->getAliasSet(*this);
495 // Unlink and delete from the list of values.
496 PtrValEnt->eraseFromList();
498 // Stop using the alias set.
499 AS->dropRef(*this);
501 PointerMap.erase(I);
504 // copyValue - This method should be used whenever a preexisting value in the
505 // program is copied or cloned, introducing a new value. Note that it is ok for
506 // clients that use this method to introduce the same value multiple times: if
507 // the tracker already knows about a value, it will ignore the request.
509 void AliasSetTracker::copyValue(Value *From, Value *To) {
510 // Notify the alias analysis implementation that this value is copied.
511 AA.copyValue(From, To);
513 // First, look up the PointerRec for this pointer.
514 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(From);
515 if (I == PointerMap.end())
516 return; // Noop
517 assert(I->second->hasAliasSet() && "Dead entry?");
519 AliasSet::PointerRec &Entry = getEntryFor(To);
520 if (Entry.hasAliasSet()) return; // Already in the tracker!
522 // Add it to the alias set it aliases...
523 I = PointerMap.find(From);
524 AliasSet *AS = I->second->getAliasSet(*this);
525 AS->addPointer(*this, Entry, I->second->getSize(), true);
530 //===----------------------------------------------------------------------===//
531 // AliasSet/AliasSetTracker Printing Support
532 //===----------------------------------------------------------------------===//
534 void AliasSet::print(std::ostream &OS) const {
535 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
536 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
537 switch (AccessTy) {
538 case NoModRef: OS << "No access "; break;
539 case Refs : OS << "Ref "; break;
540 case Mods : OS << "Mod "; break;
541 case ModRef : OS << "Mod/Ref "; break;
542 default: assert(0 && "Bad value for AccessTy!");
544 if (isVolatile()) OS << "[volatile] ";
545 if (Forward)
546 OS << " forwarding to " << (void*)Forward;
549 if (!empty()) {
550 OS << "Pointers: ";
551 for (iterator I = begin(), E = end(); I != E; ++I) {
552 if (I != begin()) OS << ", ";
553 WriteAsOperand(OS << "(", I.getPointer());
554 OS << ", " << I.getSize() << ")";
557 if (!CallSites.empty()) {
558 OS << "\n " << CallSites.size() << " Call Sites: ";
559 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
560 if (i) OS << ", ";
561 WriteAsOperand(OS, CallSites[i].getCalledValue());
564 OS << "\n";
567 void AliasSetTracker::print(std::ostream &OS) const {
568 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
569 << PointerMap.size() << " pointer values.\n";
570 for (const_iterator I = begin(), E = end(); I != E; ++I)
571 I->print(OS);
572 OS << "\n";
575 void AliasSet::dump() const { print (cerr); }
576 void AliasSetTracker::dump() const { print(cerr); }
578 //===----------------------------------------------------------------------===//
579 // AliasSetPrinter Pass
580 //===----------------------------------------------------------------------===//
582 namespace {
583 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
584 AliasSetTracker *Tracker;
585 public:
586 static char ID; // Pass identification, replacement for typeid
587 AliasSetPrinter() : FunctionPass(&ID) {}
589 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
590 AU.setPreservesAll();
591 AU.addRequired<AliasAnalysis>();
594 virtual bool runOnFunction(Function &F) {
595 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
597 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
598 Tracker->add(&*I);
599 Tracker->print(cerr);
600 delete Tracker;
601 return false;
606 char AliasSetPrinter::ID = 0;
607 static RegisterPass<AliasSetPrinter>
608 X("print-alias-sets", "Alias Set Printer", false, true);