diff --git a/llvm/include/llvm/LLVMContext.h b/llvm/include/llvm/LLVMContext.h index 9150e6160ec4..038ee35aee03 100644 --- a/llvm/include/llvm/LLVMContext.h +++ b/llvm/include/llvm/LLVMContext.h @@ -61,20 +61,11 @@ class LLVMContext { friend class ConstantArray; friend class ConstantVector; friend class ConstantAggregateZero; + friend class MDNode; + friend class MDString; public: LLVMContext(); ~LLVMContext(); - - // MDNode accessors - MDNode* getMDNode(Value* const* Vals, unsigned NumVals); - - // MDString accessors - MDString* getMDString(const StringRef &Str); - - - // Methods for erasing constants - void erase(MDString *M); - void erase(MDNode *M); }; /// FOR BACKWARDS COMPATIBILITY - Returns a global context. diff --git a/llvm/include/llvm/Metadata.h b/llvm/include/llvm/Metadata.h index 75269191563d..95100c27496a 100644 --- a/llvm/include/llvm/Metadata.h +++ b/llvm/include/llvm/Metadata.h @@ -27,6 +27,7 @@ namespace llvm { class Constant; +class LLVMContext; //===----------------------------------------------------------------------===// // MetadataBase - A base class for MDNode, MDString and NamedMDNode. @@ -64,13 +65,14 @@ public: class MDString : public MetadataBase { MDString(const MDString &); // DO NOT IMPLEMENT StringRef Str; - friend class LLVMContextImpl; protected: explicit MDString(const char *begin, unsigned l) : MetadataBase(Type::MetadataTy, Value::MDStringVal), Str(begin, l) {} public: + static MDString *get(LLVMContext &Context, const StringRef &Str); + StringRef getString() const { return Str; } unsigned length() const { return Str.size(); } @@ -97,14 +99,15 @@ public: class MDNode : public MetadataBase, public FoldingSetNode { MDNode(const MDNode &); // DO NOT IMPLEMENT - friend class LLVMContextImpl; - SmallVector Node; typedef SmallVectorImpl::iterator elem_iterator; protected: explicit MDNode(Value*const* Vals, unsigned NumVals); public: + static MDNode *get(LLVMContext &Context, + Value* const* Vals, unsigned NumVals); + typedef SmallVectorImpl::const_iterator const_elem_iterator; Value *getElement(unsigned i) const { diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp index ac3570216d56..0ba93b131a38 100644 --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -374,7 +374,7 @@ bool LLParser::ParseNamedGlobal() { bool LLParser::ParseMDString(MetadataBase *&MDS) { std::string Str; if (ParseStringConstant(Str)) return true; - MDS = Context.getMDString(Str); + MDS = MDString::get(Context, Str); return false; } @@ -403,8 +403,8 @@ bool LLParser::ParseMDNode(MetadataBase *&Node) { // Create MDNode forward reference SmallVector Elts; std::string FwdRefName = "llvm.mdnode.fwdref." + utostr(MID); - Elts.push_back(Context.getMDString(FwdRefName)); - MDNode *FwdNode = Context.getMDNode(Elts.data(), Elts.size()); + Elts.push_back(MDString::get(Context, FwdRefName)); + MDNode *FwdNode = MDNode::get(Context, Elts.data(), Elts.size()); ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc()); Node = FwdNode; return false; @@ -474,7 +474,7 @@ bool LLParser::ParseStandaloneMetadata() { || ParseToken(lltok::rbrace, "expected end of metadata node")) return true; - MDNode *Init = Context.getMDNode(Elts.data(), Elts.size()); + MDNode *Init = MDNode::get(Context, Elts.data(), Elts.size()); MetadataCache[MetadataID] = Init; std::map >::iterator FI = ForwardRefMDNodes.find(MetadataID); @@ -1729,7 +1729,7 @@ bool LLParser::ParseValID(ValID &ID) { ParseToken(lltok::rbrace, "expected end of metadata node")) return true; - ID.MetadataVal = Context.getMDNode(Elts.data(), Elts.size()); + ID.MetadataVal = MDNode::get(Context, Elts.data(), Elts.size()); return false; } diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 40920d87e9aa..81fdbd29afb9 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -774,7 +774,7 @@ bool BitcodeReader::ParseMetadata() { else Elts.push_back(NULL); } - Value *V = Context.getMDNode(&Elts[0], Elts.size()); + Value *V = MDNode::get(Context, &Elts[0], Elts.size()); ValueList.AssignValue(V, NextValueNo++); break; } @@ -784,7 +784,8 @@ bool BitcodeReader::ParseMetadata() { String.resize(MDStringLength); for (unsigned i = 0; i != MDStringLength; ++i) String[i] = Record[i]; - Value *V = Context.getMDString(StringRef(String.data(), String.size())); + Value *V = MDString::get(Context, + StringRef(String.data(), String.size())); ValueList.AssignValue(V, NextValueNo++); break; } diff --git a/llvm/lib/VMCore/LLVMContext.cpp b/llvm/lib/VMCore/LLVMContext.cpp index 107905f40aa6..3cd715866e50 100644 --- a/llvm/lib/VMCore/LLVMContext.cpp +++ b/llvm/lib/VMCore/LLVMContext.cpp @@ -31,21 +31,3 @@ LLVMContext& llvm::getGlobalContext() { LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { } LLVMContext::~LLVMContext() { delete pImpl; } - -// MDNode accessors -MDNode* LLVMContext::getMDNode(Value* const* Vals, unsigned NumVals) { - return pImpl->getMDNode(Vals, NumVals); -} - -// MDString accessors -MDString* LLVMContext::getMDString(const StringRef &Str) { - return pImpl->getMDString(Str.data(), Str.size()); -} - -void LLVMContext::erase(MDString *M) { - pImpl->erase(M); -} - -void LLVMContext::erase(MDNode *M) { - pImpl->erase(M); -} diff --git a/llvm/lib/VMCore/LLVMContextImpl.cpp b/llvm/lib/VMCore/LLVMContextImpl.cpp index bba5861cc0f7..b29868ae480d 100644 --- a/llvm/lib/VMCore/LLVMContextImpl.cpp +++ b/llvm/lib/VMCore/LLVMContextImpl.cpp @@ -20,51 +20,4 @@ using namespace llvm; LLVMContextImpl::LLVMContextImpl(LLVMContext &C) : - Context(C), TheTrueVal(0), TheFalseVal(0) { } - -MDString *LLVMContextImpl::getMDString(const char *StrBegin, - unsigned StrLength) { - sys::SmartScopedWriter Writer(ConstantsLock); - StringMapEntry &Entry = - MDStringCache.GetOrCreateValue(StringRef(StrBegin, StrLength)); - MDString *&S = Entry.getValue(); - if (!S) S = new MDString(Entry.getKeyData(), - Entry.getKeyLength()); - - 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 Writer(ConstantsLock); - N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); - if (!N) { - // InsertPoint will have been set by the FindNodeOrInsertPos call. - N = new MDNode(Vals, NumVals); - MDNodeSet.InsertNode(N, InsertPoint); - } - } - - return N; -} - -// *** erase methods *** - -void LLVMContextImpl::erase(MDString *M) { - sys::SmartScopedWriter Writer(ConstantsLock); - MDStringCache.erase(MDStringCache.find(M->getString())); -} - -void LLVMContextImpl::erase(MDNode *M) { - sys::SmartScopedWriter Writer(ConstantsLock); - MDNodeSet.RemoveNode(M); -} + Context(C), TheTrueVal(0), TheFalseVal(0) { } \ No newline at end of file diff --git a/llvm/lib/VMCore/LLVMContextImpl.h b/llvm/lib/VMCore/LLVMContextImpl.h index b20b65d7f216..7a29a8421f4e 100644 --- a/llvm/lib/VMCore/LLVMContextImpl.h +++ b/llvm/lib/VMCore/LLVMContextImpl.h @@ -462,15 +462,10 @@ class LLVMContextImpl { friend class ConstantArray; friend class ConstantVector; friend class ConstantAggregateZero; + friend class MDNode; + friend class MDString; public: LLVMContextImpl(LLVMContext &C); - - MDString *getMDString(const char *StrBegin, unsigned StrLength); - - MDNode *getMDNode(Value*const* Vals, unsigned NumVals); - - void erase(MDString *M); - void erase(MDNode *M); }; } diff --git a/llvm/lib/VMCore/Metadata.cpp b/llvm/lib/VMCore/Metadata.cpp index 48db0b4f0b3a..bf0611f9e468 100644 --- a/llvm/lib/VMCore/Metadata.cpp +++ b/llvm/lib/VMCore/Metadata.cpp @@ -11,11 +11,28 @@ // //===----------------------------------------------------------------------===// +#include "LLVMContextImpl.h" #include "llvm/Metadata.h" +#include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "SymbolTableListTraitsImpl.h" using namespace llvm; +//===----------------------------------------------------------------------===// +//MDString implementation +// +MDString *MDString::get(LLVMContext &Context, const StringRef &Str) { + LLVMContextImpl *pImpl = Context.pImpl; + sys::SmartScopedWriter Writer(pImpl->ConstantsLock); + StringMapEntry &Entry = + pImpl->MDStringCache.GetOrCreateValue(Str); + MDString *&S = Entry.getValue(); + if (!S) S = new MDString(Entry.getKeyData(), + Entry.getKeyLength()); + + return S; +} + //===----------------------------------------------------------------------===// //MDNode implementation // @@ -30,6 +47,30 @@ void MDNode::Profile(FoldingSetNodeID &ID) const { ID.AddPointer(*I); } +MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) { + LLVMContextImpl *pImpl = Context.pImpl; + FoldingSetNodeID ID; + for (unsigned i = 0; i != NumVals; ++i) + ID.AddPointer(Vals[i]); + + pImpl->ConstantsLock.reader_acquire(); + void *InsertPoint; + MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); + pImpl->ConstantsLock.reader_release(); + + if (!N) { + sys::SmartScopedWriter Writer(pImpl->ConstantsLock); + N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint); + if (!N) { + // InsertPoint will have been set by the FindNodeOrInsertPos call. + N = new MDNode(Vals, NumVals); + pImpl->MDNodeSet.InsertNode(N, InsertPoint); + } + } + + return N; +} + //===----------------------------------------------------------------------===// //NamedMDNode implementation //