Don't be COMPLETELY pessimistic in the face of function calls
[oota-llvm.git] / lib / Analysis / AliasSetTracker.cpp
index 4a293448ed32106417384824cbef5461109c4e1b..d266c2a5a07992ecf48363ebb94551edcd883273 100644 (file)
@@ -20,8 +20,7 @@
 #include "llvm/Target/TargetData.h"
 #include "llvm/Assembly/Writer.h"
 #include "llvm/Support/InstIterator.h"
-
-namespace llvm {
+using namespace llvm;
 
 /// mergeSetIn - Merge the specified alias set into this alias set...
 ///
@@ -48,9 +47,14 @@ void AliasSet::mergeSetIn(AliasSet &AS) {
   RefCount++;         // AS is now pointing to us...
 
   // Merge the list of constituent pointers...
-  PtrListTail->second.setTail(AS.PtrListHead);
-  PtrListTail = AS.PtrListTail;
-  AS.PtrListHead = AS.PtrListTail = 0;
+  if (AS.PtrList) {
+    *PtrListEnd = AS.PtrList;
+    AS.PtrList->second.setPrevInList(PtrListEnd);
+    PtrListEnd = AS.PtrListEnd;
+
+    AS.PtrList = 0;
+    AS.PtrListEnd = &AS.PtrList;
+  }
 }
 
 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
@@ -83,17 +87,27 @@ void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
   Entry.second.updateSize(Size);
 
   // Add it to the end of the list...
-  if (PtrListTail)
-    PtrListTail->second.setTail(&Entry);
-  else
-    PtrListHead = &Entry;
-  PtrListTail = &Entry;
+  assert(*PtrListEnd == 0 && "End of list is not null?");
+  *PtrListEnd = &Entry;
+  PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
   RefCount++;               // Entry points to alias set...
 }
 
-void AliasSet::addCallSite(CallSite CS) {
+void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
   CallSites.push_back(CS);
-  AliasTy = MayAlias;         // FIXME: Too conservative?
+
+  if (Function *F = CS.getCalledFunction()) {
+    if (AA.doesNotAccessMemory(F))
+      return;
+    else if (AA.onlyReadsMemory(F)) {
+      AliasTy = MayAlias;
+      AccessTy = Refs;
+      return;
+    }
+  }
+
+  // FIXME: This should use mod/ref information to make this not suck so bad
+  AliasTy = MayAlias;
   AccessTy = ModRef;
 }
 
@@ -127,7 +141,11 @@ bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
 }
 
 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
-  // FIXME: Too conservative!
+  // FIXME: Use mod/ref information to prune this better!
+  if (Function *F = CS.getCalledFunction())
+    if (AA.doesNotAccessMemory(F))
+      return false;
+
   return true;
 }
 
@@ -190,23 +208,28 @@ AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
 }
 
 void AliasSetTracker::add(LoadInst *LI) {
-  addPointer(LI->getOperand(0),
-             AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
+  AliasSet &AS = 
+    addPointer(LI->getOperand(0),
+               AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
+  if (LI->isVolatile()) AS.setVolatile();
 }
 
 void AliasSetTracker::add(StoreInst *SI) {
-  addPointer(SI->getOperand(1),
-             AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
-             AliasSet::Mods);
+  AliasSet &AS = 
+    addPointer(SI->getOperand(1),
+               AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
+               AliasSet::Mods);
+  if (SI->isVolatile()) AS.setVolatile();
 }
 
+
 void AliasSetTracker::add(CallSite CS) {
   AliasSet *AS = findAliasSetForCallSite(CS);
   if (!AS) {
     AliasSets.push_back(AliasSet());
     AS = &AliasSets.back();
   }
-  AS->addCallSite(CS); 
+  AS->addCallSite(CS, AA); 
 }
 
 void AliasSetTracker::add(Instruction *I) {
@@ -249,6 +272,31 @@ void AliasSetTracker::add(const AliasSetTracker &AST) {
     }
 }
 
+
+// remove method - This method is used to remove a pointer value from the
+// AliasSetTracker entirely.  It should be used when an instruction is deleted
+// from the program to update the AST.  If you don't use this, you would have
+// dangling pointers to deleted instructions.
+//
+void AliasSetTracker::remove(Value *PtrVal) {
+  // First, look up the PointerRec for this pointer...
+  hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
+  if (I == PointerMap.end()) return;  // Noop
+
+  // If we found one, remove the pointer from the alias set it is in.
+  AliasSet::HashNodePair &PtrValEnt = *I;
+  AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
+
+  // Unlink from the list of values...
+  PtrValEnt.second.removeFromList();
+  // Stop using the alias set
+  if (--AS->RefCount == 0)
+    AS->removeFromTracker(*this);
+
+  PointerMap.erase(I);
+}
+
+
 //===----------------------------------------------------------------------===//
 //               AliasSet/AliasSetTracker Printing Support
 //===----------------------------------------------------------------------===//
@@ -263,6 +311,7 @@ void AliasSet::print(std::ostream &OS) const {
   case ModRef  : OS << "Mod/Ref   "; break;
   default: assert(0 && "Bad value for AccessTy!");
   }
+  if (isVolatile()) OS << "[volatile] ";
   if (Forward)
     OS << " forwarding to " << (void*)Forward;
 
@@ -329,5 +378,3 @@ namespace {
   RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
                                   PassInfo::Analysis | PassInfo::Optimization);
 }
-
-} // End llvm namespace