From 081134741b40b342fb2f85722c9cea5d412489a8 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 29 Dec 2009 09:01:33 +0000 Subject: [PATCH] Final step in the metadata API restructuring: move the getMDKindID/getMDKindNames methods to LLVMContext (and add convenience methods to Module), eliminating MetadataContext. Move the state that it maintains out to LLVMContext. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92259 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Instruction.h | 2 - include/llvm/LLVMContext.h | 20 +- include/llvm/Metadata.h | 23 -- include/llvm/Module.h | 11 +- include/llvm/Value.h | 1 - lib/Analysis/DebugInfo.cpp | 3 +- lib/AsmParser/LLParser.cpp | 5 +- lib/Bitcode/Reader/BitcodeReader.cpp | 3 +- lib/Bitcode/Reader/BitcodeReader.h | 2 +- lib/Bitcode/Writer/BitcodeWriter.cpp | 5 +- lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 6 +- lib/Transforms/IPO/StripSymbols.cpp | 6 +- lib/Transforms/Utils/CloneFunction.cpp | 3 +- lib/VMCore/AsmWriter.cpp | 3 +- lib/VMCore/IRBuilder.cpp | 3 +- lib/VMCore/LLVMContext.cpp | 5 - lib/VMCore/LLVMContextImpl.h | 16 +- lib/VMCore/Metadata.cpp | 232 ++++++------------ lib/VMCore/Module.cpp | 14 ++ 19 files changed, 143 insertions(+), 220 deletions(-) diff --git a/include/llvm/Instruction.h b/include/llvm/Instruction.h index d7161cf3f2f..d45da974bc2 100644 --- a/include/llvm/Instruction.h +++ b/include/llvm/Instruction.h @@ -22,7 +22,6 @@ namespace llvm { class LLVMContext; class MDNode; -class MetadataContextImpl; template class SymbolTableListTraits; @@ -316,7 +315,6 @@ private: return Value::getSubclassDataFromValue(); } - friend class MetadataContextImpl; void setHasMetadata(bool V) { setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) | (V ? HasMetadataBit : 0)); diff --git a/include/llvm/LLVMContext.h b/include/llvm/LLVMContext.h index b9ffeb002b5..6d36d5eb1e3 100644 --- a/include/llvm/LLVMContext.h +++ b/include/llvm/LLVMContext.h @@ -18,7 +18,8 @@ namespace llvm { class LLVMContextImpl; -class MetadataContext; +class StringRef; +template class SmallVectorImpl; /// This is an important class for using LLVM in a threaded context. It /// (opaquely) owns and manages the core "global" data of LLVM's core @@ -31,14 +32,23 @@ class LLVMContext { void operator=(LLVMContext&); public: - LLVMContextImpl* const pImpl; - MetadataContext &getMetadata(); + LLVMContextImpl *const pImpl; LLVMContext(); ~LLVMContext(); + + /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. + /// This ID is uniqued across modules in the current LLVMContext. + unsigned getMDKindID(StringRef Name) const; + + /// getMDKindNames - Populate client supplied SmallVector with the name for + /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, + /// so it is filled in as an empty string. + void getMDKindNames(SmallVectorImpl &Result) const; }; -/// FOR BACKWARDS COMPATIBILITY - Returns a global context. -extern LLVMContext& getGlobalContext(); +/// getGlobalContext - Returns a global context. This is for LLVM clients that +/// only care about operating on a single thread. +extern LLVMContext &getGlobalContext(); } diff --git a/include/llvm/Metadata.h b/include/llvm/Metadata.h index c25ce22511e..86eba354e2c 100644 --- a/include/llvm/Metadata.h +++ b/include/llvm/Metadata.h @@ -25,7 +25,6 @@ class Constant; class Instruction; class LLVMContext; class Module; -class MetadataContextImpl; template class SmallVectorImpl; //===----------------------------------------------------------------------===// @@ -204,28 +203,6 @@ public: } }; -//===----------------------------------------------------------------------===// -/// MetadataContext - MetadataContext handles uniquing and assignment of IDs for -/// custom metadata types. -/// -class MetadataContext { - MetadataContext(MetadataContext&); // DO NOT IMPLEMENT - void operator=(MetadataContext&); // DO NOT IMPLEMENT - - MetadataContextImpl *const pImpl; - friend class Instruction; -public: - MetadataContext(); - ~MetadataContext(); - - /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. - unsigned getMDKindID(StringRef Name) const; - - /// getMDKindNames - Populate client supplied SmallVector with the name for - /// each custom metadata ID. ID #0 is not used, so it is filled in as empty. - void getMDKindNames(SmallVectorImpl &) const; -}; - } // end llvm namespace #endif diff --git a/include/llvm/Module.h b/include/llvm/Module.h index 895ee01f837..9a8b53ac586 100644 --- a/include/llvm/Module.h +++ b/include/llvm/Module.h @@ -184,7 +184,7 @@ public: /// Get the global data context. /// @returns LLVMContext - a container for LLVM's global information - LLVMContext& getContext() const { return Context; } + LLVMContext &getContext() const { return Context; } /// Get any module-scope inline assembly blocks. /// @returns a string containing the module-scope inline assembly blocks. @@ -222,6 +222,15 @@ public: /// if a global with the specified name is not found. GlobalValue *getNamedValue(StringRef Name) const; + /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. + /// This ID is uniqued across modules in the current LLVMContext. + unsigned getMDKindID(StringRef Name) const; + + /// getMDKindNames - Populate client supplied SmallVector with the name for + /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, + /// so it is filled in as an empty string. + void getMDKindNames(SmallVectorImpl &Result) const; + /// @} /// @name Function Accessors /// @{ diff --git a/include/llvm/Value.h b/include/llvm/Value.h index 1be6ffd4882..4d94a301916 100644 --- a/include/llvm/Value.h +++ b/include/llvm/Value.h @@ -41,7 +41,6 @@ class raw_ostream; class AssemblyAnnotationWriter; class ValueHandleBase; class LLVMContext; -class MetadataContextImpl; class Twine; //===----------------------------------------------------------------------===// diff --git a/lib/Analysis/DebugInfo.cpp b/lib/Analysis/DebugInfo.cpp index 256a7d1c364..32554a6a788 100644 --- a/lib/Analysis/DebugInfo.cpp +++ b/lib/Analysis/DebugInfo.cpp @@ -18,7 +18,6 @@ #include "llvm/Intrinsics.h" #include "llvm/IntrinsicInst.h" #include "llvm/Instructions.h" -#include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/ADT/SmallPtrSet.h" @@ -1117,7 +1116,7 @@ Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, Value *Offset, /// processModule - Process entire module and collect debug info. void DebugInfoFinder::processModule(Module &M) { - unsigned MDDbgKind = M.getContext().getMetadata().getMDKindID("dbg"); + unsigned MDDbgKind = M.getMDKindID("dbg"); for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI) diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index d9d2a4b2add..351245d8e87 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -18,8 +18,6 @@ #include "llvm/DerivedTypes.h" #include "llvm/InlineAsm.h" #include "llvm/Instructions.h" -#include "llvm/LLVMContext.h" -#include "llvm/Metadata.h" #include "llvm/Module.h" #include "llvm/Operator.h" #include "llvm/ValueSymbolTable.h" @@ -1122,8 +1120,7 @@ bool LLParser::ParseOptionalCustomMetadata() { MetadataBase *Node; if (ParseMDNode(Node)) return true; - MetadataContext &TheMetadata = M->getContext().getMetadata(); - unsigned MDK = TheMetadata.getMDKindID(Name.c_str()); + unsigned MDK = M->getMDKindID(Name.c_str()); MDsOnInst.push_back(std::make_pair(MDK, cast(Node))); return false; } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 568968d9276..7dffafa8314 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -17,7 +17,6 @@ #include "llvm/DerivedTypes.h" #include "llvm/InlineAsm.h" #include "llvm/IntrinsicInst.h" -#include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "llvm/Operator.h" #include "llvm/AutoUpgrade.h" @@ -840,7 +839,7 @@ bool BitcodeReader::ParseMetadata() { for (unsigned i = 1; i != RecordLength; ++i) Name[i-1] = Record[i]; - unsigned NewKind = Context.getMetadata().getMDKindID(Name.str()); + unsigned NewKind = TheModule->getMDKindID(Name.str()); assert(Kind == NewKind && "FIXME: Unable to handle custom metadata mismatch!");(void)NewKind; break; diff --git a/lib/Bitcode/Reader/BitcodeReader.h b/lib/Bitcode/Reader/BitcodeReader.h index 7b3a1ae893c..bb3961a086f 100644 --- a/lib/Bitcode/Reader/BitcodeReader.h +++ b/lib/Bitcode/Reader/BitcodeReader.h @@ -170,7 +170,7 @@ class BitcodeReader : public ModuleProvider { DenseMap > BlockAddrFwdRefs; public: - explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C) + explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C) : Context(C), Buffer(buffer), ErrorString(0), ValueList(C), MDValueList(C) { HasReversedFunctionsWithBodies = false; } diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 21548cd18d0..8454e54f652 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -19,8 +19,6 @@ #include "llvm/DerivedTypes.h" #include "llvm/InlineAsm.h" #include "llvm/Instructions.h" -#include "llvm/LLVMContext.h" -#include "llvm/Metadata.h" #include "llvm/Module.h" #include "llvm/Operator.h" #include "llvm/TypeSymbolTable.h" @@ -595,9 +593,8 @@ static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) { // Write metadata kinds // METADATA_KIND - [n x [id, name]] - MetadataContext &TheMetadata = M->getContext().getMetadata(); SmallVector Names; - TheMetadata.getMDKindNames(Names); + M->getMDKindNames(Names); assert(Names[0] == "" && "MDKind #0 is invalid"); if (Names.size() == 1) return; diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index a46aad7e488..05669c0ec9a 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -395,8 +395,7 @@ void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, BasicBlock::iterator End, bool &HadTailCall) { SDB->setCurrentBasicBlock(BB); - MetadataContext &TheMetadata =LLVMBB->getParent()->getContext().getMetadata(); - unsigned MDDbgKind = TheMetadata.getMDKindID("dbg"); + unsigned MDDbgKind = LLVMBB->getContext().getMDKindID("dbg"); // Lower all of the non-terminator instructions. If a call is emitted // as a tail call, cease emitting nodes for this block. @@ -676,8 +675,7 @@ void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn, #endif ); - MetadataContext &TheMetadata = Fn.getContext().getMetadata(); - unsigned MDDbgKind = TheMetadata.getMDKindID("dbg"); + unsigned MDDbgKind = Fn.getContext().getMDKindID("dbg"); // Iterate over all basic blocks in the function. for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { diff --git a/lib/Transforms/IPO/StripSymbols.cpp b/lib/Transforms/IPO/StripSymbols.cpp index 0d756267cf5..ae88d9ecd0f 100644 --- a/lib/Transforms/IPO/StripSymbols.cpp +++ b/lib/Transforms/IPO/StripSymbols.cpp @@ -24,7 +24,6 @@ #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/Instructions.h" -#include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "llvm/Pass.h" #include "llvm/Analysis/DebugInfo.h" @@ -220,9 +219,8 @@ static bool StripDebugInfo(Module &M) { Changed = true; NMD->eraseFromParent(); } - MetadataContext &TheMetadata = M.getContext().getMetadata(); - unsigned MDDbgKind = TheMetadata.getMDKindID("dbg"); - + + unsigned MDDbgKind = M.getMDKindID("dbg"); for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) diff --git a/lib/Transforms/Utils/CloneFunction.cpp b/lib/Transforms/Utils/CloneFunction.cpp index 09af8ae925b..038e7ba08ac 100644 --- a/lib/Transforms/Utils/CloneFunction.cpp +++ b/lib/Transforms/Utils/CloneFunction.cpp @@ -420,8 +420,7 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, // BasicBlock::iterator I = NewBB->begin(); - // FIXME: Only use of context. - unsigned DbgKind = OldFunc->getContext().getMetadata().getMDKindID("dbg"); + unsigned DbgKind = OldFunc->getContext().getMDKindID("dbg"); MDNode *TheCallMD = NULL; SmallVector MDVs; if (TheCall && TheCall->hasMetadata()) diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index e82b3d95621..a95a5498c1b 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -22,7 +22,6 @@ #include "llvm/DerivedTypes.h" #include "llvm/InlineAsm.h" #include "llvm/IntrinsicInst.h" -#include "llvm/LLVMContext.h" #include "llvm/Operator.h" #include "llvm/Module.h" #include "llvm/ValueSymbolTable.h" @@ -1338,7 +1337,7 @@ public: AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M); // FIXME: Provide MDPrinter if (M) - M->getContext().getMetadata().getMDKindNames(MDNames); + M->getMDKindNames(MDNames); } void write(const Module *M) { printModule(M); } diff --git a/lib/VMCore/IRBuilder.cpp b/lib/VMCore/IRBuilder.cpp index 4c0299c6fcf..699bf0f6535 100644 --- a/lib/VMCore/IRBuilder.cpp +++ b/lib/VMCore/IRBuilder.cpp @@ -15,7 +15,6 @@ #include "llvm/Support/IRBuilder.h" #include "llvm/GlobalVariable.h" #include "llvm/Function.h" -#include "llvm/Metadata.h" #include "llvm/LLVMContext.h" using namespace llvm; @@ -37,7 +36,7 @@ Value *IRBuilderBase::CreateGlobalString(const char *Str, const Twine &Name) { /// information. void IRBuilderBase::SetCurrentDebugLocation(MDNode *L) { if (DbgMDKind == 0) - DbgMDKind = Context.getMetadata().getMDKindID("dbg"); + DbgMDKind = Context.getMDKindID("dbg"); CurDbgLocation = L; } diff --git a/lib/VMCore/LLVMContext.cpp b/lib/VMCore/LLVMContext.cpp index 3e326050fca..5a8ea5cf6d3 100644 --- a/lib/VMCore/LLVMContext.cpp +++ b/lib/VMCore/LLVMContext.cpp @@ -17,9 +17,7 @@ #include "llvm/Constants.h" #include "llvm/Instruction.h" #include "llvm/Support/ManagedStatic.h" -#include "llvm/Support/ValueHandle.h" #include "LLVMContextImpl.h" - using namespace llvm; static ManagedStatic GlobalContext; @@ -44,6 +42,3 @@ GetElementPtrConstantExpr::GetElementPtrConstantExpr OperandList[i+1] = IdxList[i]; } -MetadataContext &LLVMContext::getMetadata() { - return pImpl->TheMetadata; -} diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h index 2ea2d5e910e..ccca7892095 100644 --- a/lib/VMCore/LLVMContextImpl.h +++ b/lib/VMCore/LLVMContextImpl.h @@ -23,6 +23,7 @@ #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/Assembly/Writer.h" +#include "llvm/Support/ValueHandle.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/DenseMap.h" @@ -171,7 +172,17 @@ public: typedef DenseMap ValueHandlesTy; ValueHandlesTy ValueHandles; - MetadataContext TheMetadata; + /// CustomMDKindNames - Map to hold the metadata string to ID mapping. + StringMap CustomMDKindNames; + + typedef std::pair > MDPairTy; + typedef SmallVector MDMapTy; + + /// MetadataStore - Collection of per-instruction metadata used in this + /// context. + DenseMap MetadataStore; + + LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0), VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID), @@ -187,8 +198,7 @@ public: Int32Ty(C, 32), Int64Ty(C, 64) { } - ~LLVMContextImpl() - { + ~LLVMContextImpl() { ExprConstants.freeConstants(); ArrayConstants.freeConstants(); StructConstants.freeConstants(); diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index 216d8a20af2..d5c1dba2d7b 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -242,152 +242,9 @@ void NamedMDNode::dropAllReferences() { } -//===----------------------------------------------------------------------===// -// MetadataContextImpl implementation. -// -namespace llvm { -class MetadataContextImpl { -public: - typedef std::pair > MDPairTy; - typedef SmallVector MDMapTy; - typedef DenseMap MDStoreTy; - friend class BitcodeReader; -private: - - /// MetadataStore - Collection of metadata used in this context. - MDStoreTy MetadataStore; - - /// MDHandlerNames - Map to hold metadata handler names. - StringMap MDHandlerNames; - -public: - // Name <-> ID mapping methods. - unsigned getMDKindID(StringRef Name); - void getMDKindNames(SmallVectorImpl &) const; - - - // Instruction metadata methods. - MDNode *getMetadata(const Instruction *Inst, unsigned Kind); - void getAllMetadata(const Instruction *Inst, - SmallVectorImpl > &MDs)const; - - void setMetadata(Instruction *Inst, unsigned Kind, MDNode *Node); - - /// removeAllMetadata - Remove all metadata attached to an instruction. - void removeAllMetadata(Instruction *Inst); -}; -} - -/// getMDKindID - Return a unique non-zero ID for the specified metadata kind. -unsigned MetadataContextImpl::getMDKindID(StringRef Name) { - unsigned &Entry = MDHandlerNames[Name]; - - // If this is new, assign it its ID. - if (Entry == 0) Entry = MDHandlerNames.size(); - return Entry; -} - -/// getHandlerNames - Populate client supplied smallvector using custome -/// metadata name and ID. -void MetadataContextImpl:: -getMDKindNames(SmallVectorImpl &Names) const { - Names.resize(MDHandlerNames.size()+1); - Names[0] = ""; - for (StringMap::const_iterator I = MDHandlerNames.begin(), - E = MDHandlerNames.end(); I != E; ++I) - // MD Handlers are numbered from 1. - Names[I->second] = I->first(); -} - - -/// getMetadata - Get the metadata of given kind attached to an Instruction. -/// If the metadata is not found then return 0. -MDNode *MetadataContextImpl:: -getMetadata(const Instruction *Inst, unsigned MDKind) { - MDMapTy &Info = MetadataStore[Inst]; - assert(Inst->hasMetadata() && !Info.empty() && "Shouldn't have called this"); - - for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I) - if (I->first == MDKind) - return I->second; - return 0; -} - -/// getAllMetadata - Get all of the metadata attached to an Instruction. -void MetadataContextImpl:: -getAllMetadata(const Instruction *Inst, - SmallVectorImpl > &Result) const { - assert(Inst->hasMetadata() && MetadataStore.find(Inst) != MetadataStore.end() - && "Shouldn't have called this"); - const MDMapTy &Info = MetadataStore.find(Inst)->second; - assert(!Info.empty() && "Shouldn't have called this"); - - Result.clear(); - Result.append(Info.begin(), Info.end()); - - // Sort the resulting array so it is stable. - if (Result.size() > 1) - array_pod_sort(Result.begin(), Result.end()); -} - - -void MetadataContextImpl::setMetadata(Instruction *Inst, unsigned Kind, - MDNode *Node) { - // Handle the case when we're adding/updating metadata on an instruction. - if (Node) { - MDMapTy &Info = MetadataStore[Inst]; - assert(!Info.empty() == Inst->hasMetadata() && "HasMetadata bit is wonked"); - if (Info.empty()) { - Inst->setHasMetadata(true); - } else { - // Handle replacement of an existing value. - for (unsigned i = 0, e = Info.size(); i != e; ++i) - if (Info[i].first == Kind) { - Info[i].second = Node; - return; - } - } - - // No replacement, just add it to the list. - Info.push_back(std::make_pair(Kind, Node)); - return; - } - - // Otherwise, we're removing metadata from an instruction. - assert(Inst->hasMetadata() && MetadataStore.count(Inst) && - "HasMetadata bit out of date!"); - MDMapTy &Info = MetadataStore[Inst]; - - // Common case is removing the only entry. - if (Info.size() == 1 && Info[0].first == Kind) { - MetadataStore.erase(Inst); - Inst->setHasMetadata(false); - return; - } - - // Handle replacement of an existing value. - for (unsigned i = 0, e = Info.size(); i != e; ++i) - if (Info[i].first == Kind) { - Info[i] = Info.back(); - Info.pop_back(); - assert(!Info.empty() && "Removing last entry should be handled above"); - return; - } - // Otherwise, removing an entry that doesn't exist on the instruction. -} - -/// removeAllMetadata - Remove all metadata attached with an instruction. -void MetadataContextImpl::removeAllMetadata(Instruction *Inst) { - MetadataStore.erase(Inst); - Inst->setHasMetadata(false); -} - - //===----------------------------------------------------------------------===// // MetadataContext implementation. // -MetadataContext::MetadataContext() : pImpl(new MetadataContextImpl()) { } -MetadataContext::~MetadataContext() { delete pImpl; } #ifndef NDEBUG /// isValidName - Return true if Name is a valid custom metadata handler name. @@ -408,15 +265,25 @@ static bool isValidName(StringRef MDName) { #endif /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. -unsigned MetadataContext::getMDKindID(StringRef Name) const { +unsigned LLVMContext::getMDKindID(StringRef Name) const { assert(isValidName(Name) && "Invalid MDNode name"); - return pImpl->getMDKindID(Name); + + unsigned &Entry = pImpl->CustomMDKindNames[Name]; + + // If this is new, assign it its ID. + if (Entry == 0) Entry = pImpl->CustomMDKindNames.size(); + return Entry; } /// getHandlerNames - Populate client supplied smallvector using custome /// metadata name and ID. -void MetadataContext::getMDKindNames(SmallVectorImpl &N) const { - pImpl->getMDKindNames(N); +void LLVMContext::getMDKindNames(SmallVectorImpl &Names) const { + Names.resize(pImpl->CustomMDKindNames.size()+1); + Names[0] = ""; + for (StringMap::const_iterator I = pImpl->CustomMDKindNames.begin(), + E = pImpl->CustomMDKindNames.end(); I != E; ++I) + // MD Handlers are numbered from 1. + Names[I->second] = I->first(); } //===----------------------------------------------------------------------===// @@ -425,11 +292,11 @@ void MetadataContext::getMDKindNames(SmallVectorImpl &N) const { void Instruction::setMetadata(const char *Kind, MDNode *Node) { if (Node == 0 && !hasMetadata()) return; - setMetadata(getContext().getMetadata().getMDKindID(Kind), Node); + setMetadata(getContext().getMDKindID(Kind), Node); } MDNode *Instruction::getMetadataImpl(const char *Kind) const { - return getMetadataImpl(getContext().getMetadata().getMDKindID(Kind)); + return getMetadataImpl(getContext().getMDKindID(Kind)); } /// setMetadata - Set the metadata of of the specified kind to the specified @@ -438,21 +305,80 @@ MDNode *Instruction::getMetadataImpl(const char *Kind) const { void Instruction::setMetadata(unsigned KindID, MDNode *Node) { if (Node == 0 && !hasMetadata()) return; - getContext().getMetadata().pImpl->setMetadata(this, KindID, Node); + // Handle the case when we're adding/updating metadata on an instruction. + if (Node) { + LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; + assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked"); + if (Info.empty()) { + setHasMetadata(true); + } else { + // Handle replacement of an existing value. + for (unsigned i = 0, e = Info.size(); i != e; ++i) + if (Info[i].first == KindID) { + Info[i].second = Node; + return; + } + } + + // No replacement, just add it to the list. + Info.push_back(std::make_pair(KindID, Node)); + return; + } + + // Otherwise, we're removing metadata from an instruction. + assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) && + "HasMetadata bit out of date!"); + LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; + + // Common case is removing the only entry. + if (Info.size() == 1 && Info[0].first == KindID) { + getContext().pImpl->MetadataStore.erase(this); + setHasMetadata(false); + return; + } + + // Handle replacement of an existing value. + for (unsigned i = 0, e = Info.size(); i != e; ++i) + if (Info[i].first == KindID) { + Info[i] = Info.back(); + Info.pop_back(); + assert(!Info.empty() && "Removing last entry should be handled above"); + return; + } + // Otherwise, removing an entry that doesn't exist on the instruction. } MDNode *Instruction::getMetadataImpl(unsigned KindID) const { - return getContext().getMetadata().pImpl->getMetadata(this, KindID); + LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; + assert(hasMetadata() && !Info.empty() && "Shouldn't have called this"); + + for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end(); + I != E; ++I) + if (I->first == KindID) + return I->second; + return 0; } void Instruction::getAllMetadataImpl(SmallVectorImpl > &Result)const { - getContext().getMetadata().pImpl->getAllMetadata(this, Result); + assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) && + "Shouldn't have called this"); + const LLVMContextImpl::MDMapTy &Info = + getContext().pImpl->MetadataStore.find(this)->second; + assert(!Info.empty() && "Shouldn't have called this"); + + Result.clear(); + Result.append(Info.begin(), Info.end()); + + // Sort the resulting array so it is stable. + if (Result.size() > 1) + array_pod_sort(Result.begin(), Result.end()); } /// removeAllMetadata - Remove all metadata from this instruction. void Instruction::removeAllMetadata() { assert(hasMetadata() && "Caller should check"); - getContext().getMetadata().pImpl->removeAllMetadata(this); + getContext().pImpl->MetadataStore.erase(this); + setHasMetadata(false); } diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp index ce281eb549a..a7f503bacb9 100644 --- a/lib/VMCore/Module.cpp +++ b/lib/VMCore/Module.cpp @@ -118,6 +118,20 @@ GlobalValue *Module::getNamedValue(StringRef Name) const { return cast_or_null(getValueSymbolTable().lookup(Name)); } +/// getMDKindID - Return a unique non-zero ID for the specified metadata kind. +/// This ID is uniqued across modules in the current LLVMContext. +unsigned Module::getMDKindID(StringRef Name) const { + return Context.getMDKindID(Name); +} + +/// getMDKindNames - Populate client supplied SmallVector with the name for +/// custom metadata IDs registered in this LLVMContext. ID #0 is not used, +/// so it is filled in as an empty string. +void Module::getMDKindNames(SmallVectorImpl &Result) const { + return Context.getMDKindNames(Result); +} + + //===----------------------------------------------------------------------===// // Methods for easy access to the functions in the module. //