Privatize the MDNode uniquing table.

llvm-svn: 76126
This commit is contained in:
Owen Anderson 2009-07-16 23:44:30 +00:00
parent dc39f4fff8
commit 1c2b8cc518
6 changed files with 49 additions and 33 deletions

View File

@ -271,6 +271,7 @@ public:
// Methods for erasing constants // Methods for erasing constants
void erase(MDString *M); void erase(MDString *M);
void erase(MDNode *M);
}; };
/// FOR BACKWARDS COMPATIBILITY - Returns a global context. /// FOR BACKWARDS COMPATIBILITY - Returns a global context.

View File

@ -46,6 +46,8 @@ namespace llvm {
class MDNode : public Constant, public FoldingSetNode { class MDNode : public Constant, public FoldingSetNode {
MDNode(const MDNode &); // DO NOT IMPLEMENT MDNode(const MDNode &); // DO NOT IMPLEMENT
friend class LLVMContextImpl;
friend class ElementVH; friend class ElementVH;
struct ElementVH : public CallbackVH { struct ElementVH : public CallbackVH {
MDNode *OwningNode; MDNode *OwningNode;
@ -72,15 +74,12 @@ class MDNode : public Constant, public FoldingSetNode {
SmallVector<ElementVH, 4> Node; SmallVector<ElementVH, 4> Node;
typedef SmallVectorImpl<ElementVH>::iterator elem_iterator; typedef SmallVectorImpl<ElementVH>::iterator elem_iterator;
protected: protected:
explicit MDNode(Value*const* Vals, unsigned NumVals); explicit MDNode(Value*const* Vals, unsigned NumVals);
public: public:
typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator; typedef SmallVectorImpl<ElementVH>::const_iterator const_elem_iterator;
/// get() - Static factory methods - Return objects of the specified value.
///
static MDNode *get(Value*const* Vals, unsigned NumVals);
Value *getElement(unsigned i) const { Value *getElement(unsigned i) const {
return Node[i]; return Node[i];
} }

View File

@ -1443,8 +1443,6 @@ void MDString::destroyConstant() {
//---- MDNode::get() implementation //---- MDNode::get() implementation
// //
static ManagedStatic<FoldingSet<MDNode> > MDNodeSet;
MDNode::MDNode(Value*const* Vals, unsigned NumVals) MDNode::MDNode(Value*const* Vals, unsigned NumVals)
: Constant(Type::MetadataTy, MDNodeVal, 0, 0) { : Constant(Type::MetadataTy, MDNodeVal, 0, 0) {
for (unsigned i = 0; i != NumVals; ++i) for (unsigned i = 0; i != NumVals; ++i)
@ -1456,32 +1454,8 @@ void MDNode::Profile(FoldingSetNodeID &ID) const {
ID.AddPointer(*I); ID.AddPointer(*I);
} }
MDNode *MDNode::get(Value*const* Vals, unsigned NumVals) {
FoldingSetNodeID ID;
for (unsigned i = 0; i != NumVals; ++i)
ID.AddPointer(Vals[i]);
ConstantsLock->reader_acquire();
void *InsertPoint;
MDNode *N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
ConstantsLock->reader_release();
if (!N) {
sys::SmartScopedWriter<true> Writer(*ConstantsLock);
N = MDNodeSet->FindNodeOrInsertPos(ID, InsertPoint);
if (!N) {
// InsertPoint will have been set by the FindNodeOrInsertPos call.
N = new(0) MDNode(Vals, NumVals);
MDNodeSet->InsertNode(N, InsertPoint);
}
}
return N;
}
void MDNode::destroyConstant() { void MDNode::destroyConstant() {
sys::SmartScopedWriter<true> Writer(*ConstantsLock); getType()->getContext().erase(this);
MDNodeSet->RemoveNode(this);
destroyConstantImpl(); destroyConstantImpl();
} }
@ -2519,7 +2493,8 @@ void MDNode::replaceElement(Value *From, Value *To) {
Values.push_back(Val); Values.push_back(Val);
} }
MDNode *Replacement = MDNode::get(&Values[0], Values.size()); MDNode *Replacement =
getType()->getContext().getMDNode(&Values[0], Values.size());
assert(Replacement != this && "I didn't contain From!"); assert(Replacement != this && "I didn't contain From!");
uncheckedReplaceAllUsesWith(Replacement); uncheckedReplaceAllUsesWith(Replacement);

View File

@ -543,7 +543,7 @@ Constant* LLVMContext::getConstantVector(Constant* const* Vals,
// MDNode accessors // MDNode accessors
MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) { MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) {
return MDNode::get(Vals, NumVals); return pImpl->getMDNode(Vals, NumVals);
} }
// MDString accessors // MDString accessors
@ -640,4 +640,8 @@ const Type* LLVMContext::makeCmpResultType(const Type* opnd_type) {
void LLVMContext::erase(MDString *M) { void LLVMContext::erase(MDString *M) {
pImpl->erase(M); pImpl->erase(M);
}
void LLVMContext::erase(MDNode *M) {
pImpl->erase(M);
} }

View File

@ -16,6 +16,7 @@
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/LLVMContext.h" #include "llvm/LLVMContext.h"
#include "llvm/MDNode.h"
using namespace llvm; using namespace llvm;
// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap // Get a ConstantInt from an APInt. Note that the value stored in the DenseMap
@ -92,9 +93,38 @@ MDString *LLVMContextImpl::getMDString(const char *StrBegin,
return S; return S;
} }
MDNode *LLVMContextImpl::getMDNode(Value*const* Vals, unsigned NumVals) {
FoldingSetNodeID ID;
for (unsigned i = 0; i != NumVals; ++i)
ID.AddPointer(Vals[i]);
ConstantsLock.reader_acquire();
void *InsertPoint;
MDNode *N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
ConstantsLock.reader_release();
if (!N) {
sys::SmartScopedWriter<true> Writer(ConstantsLock);
N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
if (!N) {
// InsertPoint will have been set by the FindNodeOrInsertPos call.
N = new(0) MDNode(Vals, NumVals);
MDNodeSet.InsertNode(N, InsertPoint);
}
}
return N;
}
// *** erase methods *** // *** erase methods ***
void LLVMContextImpl::erase(MDString *M) { void LLVMContextImpl::erase(MDString *M) {
sys::SmartScopedWriter<true> Writer(ConstantsLock); sys::SmartScopedWriter<true> Writer(ConstantsLock);
MDStringCache.erase(MDStringCache.find(M->StrBegin, M->StrEnd)); MDStringCache.erase(MDStringCache.find(M->StrBegin, M->StrEnd));
} }
void LLVMContextImpl::erase(MDNode *M) {
sys::SmartScopedWriter<true> Writer(ConstantsLock);
MDNodeSet.RemoveNode(M);
}

View File

@ -19,6 +19,7 @@
#include "llvm/ADT/APFloat.h" #include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h" #include "llvm/ADT/APInt.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
namespace llvm { namespace llvm {
@ -26,8 +27,10 @@ namespace llvm {
class ConstantInt; class ConstantInt;
class ConstantFP; class ConstantFP;
class MDString; class MDString;
class MDNode;
class LLVMContext; class LLVMContext;
class Type; class Type;
class Value;
struct DenseMapAPIntKeyInfo { struct DenseMapAPIntKeyInfo {
struct KeyTy { struct KeyTy {
@ -94,6 +97,8 @@ class LLVMContextImpl {
StringMap<MDString*> MDStringCache; StringMap<MDString*> MDStringCache;
FoldingSet<MDNode> MDNodeSet;
LLVMContext &Context; LLVMContext &Context;
LLVMContextImpl(); LLVMContextImpl();
LLVMContextImpl(const LLVMContextImpl&); LLVMContextImpl(const LLVMContextImpl&);
@ -108,8 +113,10 @@ public:
MDString *getMDString(const char *StrBegin, const char *StrEnd); MDString *getMDString(const char *StrBegin, const char *StrEnd);
MDNode *getMDNode(Value*const* Vals, unsigned NumVals);
void erase(MDString *M); void erase(MDString *M);
void erase(MDNode *M);
}; };
} }