From b5b56ba9d4df47e618d4e0f9e1e09bf216733ee8 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 30 Jul 2009 20:21:41 +0000 Subject: [PATCH] Use CallbackVH in AliasSetTracker to avoid getting stuck with dangling Value*s. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77623 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Analysis/AliasSetTracker.h | 18 ++++++++++++++++-- lib/Analysis/AliasSetTracker.cpp | 24 ++++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/llvm/Analysis/AliasSetTracker.h b/include/llvm/Analysis/AliasSetTracker.h index 0a9e278b4d9..99d7ce80e5d 100644 --- a/include/llvm/Analysis/AliasSetTracker.h +++ b/include/llvm/Analysis/AliasSetTracker.h @@ -18,6 +18,7 @@ #define LLVM_ANALYSIS_ALIASSETTRACKER_H #include "llvm/Support/CallSite.h" +#include "llvm/Support/ValueHandle.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/iterator.h" #include "llvm/ADT/ilist.h" @@ -251,11 +252,24 @@ inline std::ostream& operator<<(std::ostream &OS, const AliasSet &AS) { class AliasSetTracker { + /// CallbackVH - A CallbackVH to arrange for AliasSetTracker to be + /// notified whenever a Value is deleted. + class ASTCallbackVH : public CallbackVH { + AliasSetTracker *AST; + virtual void deleted(); + public: + ASTCallbackVH(Value *V, AliasSetTracker *AST = 0); + }; + AliasAnalysis &AA; ilist AliasSets; + typedef DenseMap > + PointerMapType; + // Map from pointers to their node - DenseMap PointerMap; + PointerMapType PointerMap; + public: /// AliasSetTracker ctor - Create an empty collection of AliasSets, and use /// the specified alias analysis object to disambiguate load and store @@ -364,7 +378,7 @@ private: // getEntryFor - Just like operator[] on the map, except that it creates an // entry for the pointer if it doesn't already exist. AliasSet::PointerRec &getEntryFor(Value *V) { - AliasSet::PointerRec *&Entry = PointerMap[V]; + AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)]; if (Entry == 0) Entry = new AliasSet::PointerRec(V); return *Entry; diff --git a/lib/Analysis/AliasSetTracker.cpp b/lib/Analysis/AliasSetTracker.cpp index 841cd2be3b2..9f201c322df 100644 --- a/lib/Analysis/AliasSetTracker.cpp +++ b/lib/Analysis/AliasSetTracker.cpp @@ -187,8 +187,8 @@ bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const { void AliasSetTracker::clear() { // Delete all the PointerRec entries. - for (DenseMap::iterator I = PointerMap.begin(), - E = PointerMap.end(); I != E; ++I) + for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end(); + I != E; ++I) I->second->eraseFromList(); PointerMap.clear(); @@ -485,7 +485,7 @@ void AliasSetTracker::deleteValue(Value *PtrVal) { AS->removeCallSite(CS); // First, look up the PointerRec for this pointer. - DenseMap::iterator I = PointerMap.find(PtrVal); + PointerMapType::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. @@ -511,7 +511,7 @@ void AliasSetTracker::copyValue(Value *From, Value *To) { AA.copyValue(From, To); // First, look up the PointerRec for this pointer. - DenseMap::iterator I = PointerMap.find(From); + PointerMapType::iterator I = PointerMap.find(From); if (I == PointerMap.end()) return; // Noop assert(I->second->hasAliasSet() && "Dead entry?"); @@ -575,6 +575,22 @@ void AliasSetTracker::print(std::ostream &OS) const { void AliasSet::dump() const { print (cerr); } void AliasSetTracker::dump() const { print(cerr); } +//===----------------------------------------------------------------------===// +// ASTCallbackVH Class Implementation +//===----------------------------------------------------------------------===// + +void AliasSetTracker::ASTCallbackVH::deleted() { + assert(AST && "ASTCallbackVH called with a null AliasSetTracker!"); + AST->deleteValue(getValPtr()); + // this now dangles! +} + +AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast) + : CallbackVH(V == DenseMapInfo::getEmptyKey() ? 0 : + V == DenseMapInfo::getTombstoneKey() ? 0 : + V), + AST(ast) {} + //===----------------------------------------------------------------------===// // AliasSetPrinter Pass //===----------------------------------------------------------------------===// -- 2.34.1