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.

llvm-svn: 92259
This commit is contained in:
Chris Lattner 2009-12-29 09:01:33 +00:00
parent 9330b2f7ec
commit a0566979b7
19 changed files with 143 additions and 220 deletions

View File

@ -22,7 +22,6 @@ namespace llvm {
class LLVMContext; class LLVMContext;
class MDNode; class MDNode;
class MetadataContextImpl;
template<typename ValueSubClass, typename ItemParentClass> template<typename ValueSubClass, typename ItemParentClass>
class SymbolTableListTraits; class SymbolTableListTraits;
@ -316,7 +315,6 @@ private:
return Value::getSubclassDataFromValue(); return Value::getSubclassDataFromValue();
} }
friend class MetadataContextImpl;
void setHasMetadata(bool V) { void setHasMetadata(bool V) {
setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) | setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
(V ? HasMetadataBit : 0)); (V ? HasMetadataBit : 0));

View File

@ -18,7 +18,8 @@
namespace llvm { namespace llvm {
class LLVMContextImpl; class LLVMContextImpl;
class MetadataContext; class StringRef;
template <typename T> class SmallVectorImpl;
/// This is an important class for using LLVM in a threaded context. It /// 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 /// (opaquely) owns and manages the core "global" data of LLVM's core
@ -31,14 +32,23 @@ class LLVMContext {
void operator=(LLVMContext&); void operator=(LLVMContext&);
public: public:
LLVMContextImpl* const pImpl; LLVMContextImpl *const pImpl;
MetadataContext &getMetadata();
LLVMContext(); LLVMContext();
~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<StringRef> &Result) const;
}; };
/// FOR BACKWARDS COMPATIBILITY - Returns a global context. /// getGlobalContext - Returns a global context. This is for LLVM clients that
extern LLVMContext& getGlobalContext(); /// only care about operating on a single thread.
extern LLVMContext &getGlobalContext();
} }

View File

@ -25,7 +25,6 @@ class Constant;
class Instruction; class Instruction;
class LLVMContext; class LLVMContext;
class Module; class Module;
class MetadataContextImpl;
template <typename T> class SmallVectorImpl; template <typename T> 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<StringRef> &) const;
};
} // end llvm namespace } // end llvm namespace
#endif #endif

View File

@ -184,7 +184,7 @@ public:
/// Get the global data context. /// Get the global data context.
/// @returns LLVMContext - a container for LLVM's global information /// @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. /// Get any module-scope inline assembly blocks.
/// @returns a string containing the 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. /// if a global with the specified name is not found.
GlobalValue *getNamedValue(StringRef Name) const; 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<StringRef> &Result) const;
/// @} /// @}
/// @name Function Accessors /// @name Function Accessors
/// @{ /// @{

View File

@ -41,7 +41,6 @@ class raw_ostream;
class AssemblyAnnotationWriter; class AssemblyAnnotationWriter;
class ValueHandleBase; class ValueHandleBase;
class LLVMContext; class LLVMContext;
class MetadataContextImpl;
class Twine; class Twine;
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -18,7 +18,6 @@
#include "llvm/Intrinsics.h" #include "llvm/Intrinsics.h"
#include "llvm/IntrinsicInst.h" #include "llvm/IntrinsicInst.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/Analysis/ValueTracking.h" #include "llvm/Analysis/ValueTracking.h"
#include "llvm/ADT/SmallPtrSet.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. /// processModule - Process entire module and collect debug info.
void DebugInfoFinder::processModule(Module &M) { 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 (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI) for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)

View File

@ -18,8 +18,6 @@
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h" #include "llvm/InlineAsm.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/Metadata.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/Operator.h" #include "llvm/Operator.h"
#include "llvm/ValueSymbolTable.h" #include "llvm/ValueSymbolTable.h"
@ -1122,8 +1120,7 @@ bool LLParser::ParseOptionalCustomMetadata() {
MetadataBase *Node; MetadataBase *Node;
if (ParseMDNode(Node)) return true; if (ParseMDNode(Node)) return true;
MetadataContext &TheMetadata = M->getContext().getMetadata(); unsigned MDK = M->getMDKindID(Name.c_str());
unsigned MDK = TheMetadata.getMDKindID(Name.c_str());
MDsOnInst.push_back(std::make_pair(MDK, cast<MDNode>(Node))); MDsOnInst.push_back(std::make_pair(MDK, cast<MDNode>(Node)));
return false; return false;
} }

View File

@ -17,7 +17,6 @@
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h" #include "llvm/InlineAsm.h"
#include "llvm/IntrinsicInst.h" #include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/Operator.h" #include "llvm/Operator.h"
#include "llvm/AutoUpgrade.h" #include "llvm/AutoUpgrade.h"
@ -840,7 +839,7 @@ bool BitcodeReader::ParseMetadata() {
for (unsigned i = 1; i != RecordLength; ++i) for (unsigned i = 1; i != RecordLength; ++i)
Name[i-1] = Record[i]; Name[i-1] = Record[i];
unsigned NewKind = Context.getMetadata().getMDKindID(Name.str()); unsigned NewKind = TheModule->getMDKindID(Name.str());
assert(Kind == NewKind && assert(Kind == NewKind &&
"FIXME: Unable to handle custom metadata mismatch!");(void)NewKind; "FIXME: Unable to handle custom metadata mismatch!");(void)NewKind;
break; break;

View File

@ -170,7 +170,7 @@ class BitcodeReader : public ModuleProvider {
DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs; DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
public: public:
explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C) explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
: Context(C), Buffer(buffer), ErrorString(0), ValueList(C), MDValueList(C) { : Context(C), Buffer(buffer), ErrorString(0), ValueList(C), MDValueList(C) {
HasReversedFunctionsWithBodies = false; HasReversedFunctionsWithBodies = false;
} }

View File

@ -19,8 +19,6 @@
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h" #include "llvm/InlineAsm.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/Metadata.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/Operator.h" #include "llvm/Operator.h"
#include "llvm/TypeSymbolTable.h" #include "llvm/TypeSymbolTable.h"
@ -595,9 +593,8 @@ static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
// Write metadata kinds // Write metadata kinds
// METADATA_KIND - [n x [id, name]] // METADATA_KIND - [n x [id, name]]
MetadataContext &TheMetadata = M->getContext().getMetadata();
SmallVector<StringRef, 4> Names; SmallVector<StringRef, 4> Names;
TheMetadata.getMDKindNames(Names); M->getMDKindNames(Names);
assert(Names[0] == "" && "MDKind #0 is invalid"); assert(Names[0] == "" && "MDKind #0 is invalid");
if (Names.size() == 1) return; if (Names.size() == 1) return;

View File

@ -395,8 +395,7 @@ void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB,
BasicBlock::iterator End, BasicBlock::iterator End,
bool &HadTailCall) { bool &HadTailCall) {
SDB->setCurrentBasicBlock(BB); SDB->setCurrentBasicBlock(BB);
MetadataContext &TheMetadata =LLVMBB->getParent()->getContext().getMetadata(); unsigned MDDbgKind = LLVMBB->getContext().getMDKindID("dbg");
unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
// Lower all of the non-terminator instructions. If a call is emitted // Lower all of the non-terminator instructions. If a call is emitted
// as a tail call, cease emitting nodes for this block. // as a tail call, cease emitting nodes for this block.
@ -676,8 +675,7 @@ void SelectionDAGISel::SelectAllBasicBlocks(Function &Fn,
#endif #endif
); );
MetadataContext &TheMetadata = Fn.getContext().getMetadata(); unsigned MDDbgKind = Fn.getContext().getMDKindID("dbg");
unsigned MDDbgKind = TheMetadata.getMDKindID("dbg");
// Iterate over all basic blocks in the function. // Iterate over all basic blocks in the function.
for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) { for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {

View File

@ -24,7 +24,6 @@
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/Analysis/DebugInfo.h" #include "llvm/Analysis/DebugInfo.h"
@ -220,9 +219,8 @@ static bool StripDebugInfo(Module &M) {
Changed = true; Changed = true;
NMD->eraseFromParent(); 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 (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
++FI) ++FI)

View File

@ -420,8 +420,7 @@ void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
// //
BasicBlock::iterator I = NewBB->begin(); BasicBlock::iterator I = NewBB->begin();
// FIXME: Only use of context. unsigned DbgKind = OldFunc->getContext().getMDKindID("dbg");
unsigned DbgKind = OldFunc->getContext().getMetadata().getMDKindID("dbg");
MDNode *TheCallMD = NULL; MDNode *TheCallMD = NULL;
SmallVector<Value *, 4> MDVs; SmallVector<Value *, 4> MDVs;
if (TheCall && TheCall->hasMetadata()) if (TheCall && TheCall->hasMetadata())

View File

@ -22,7 +22,6 @@
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/InlineAsm.h" #include "llvm/InlineAsm.h"
#include "llvm/IntrinsicInst.h" #include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Operator.h" #include "llvm/Operator.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/ValueSymbolTable.h" #include "llvm/ValueSymbolTable.h"
@ -1338,7 +1337,7 @@ public:
AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M); AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
// FIXME: Provide MDPrinter // FIXME: Provide MDPrinter
if (M) if (M)
M->getContext().getMetadata().getMDKindNames(MDNames); M->getMDKindNames(MDNames);
} }
void write(const Module *M) { printModule(M); } void write(const Module *M) { printModule(M); }

View File

@ -15,7 +15,6 @@
#include "llvm/Support/IRBuilder.h" #include "llvm/Support/IRBuilder.h"
#include "llvm/GlobalVariable.h" #include "llvm/GlobalVariable.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/Metadata.h"
#include "llvm/LLVMContext.h" #include "llvm/LLVMContext.h"
using namespace llvm; using namespace llvm;
@ -37,7 +36,7 @@ Value *IRBuilderBase::CreateGlobalString(const char *Str, const Twine &Name) {
/// information. /// information.
void IRBuilderBase::SetCurrentDebugLocation(MDNode *L) { void IRBuilderBase::SetCurrentDebugLocation(MDNode *L) {
if (DbgMDKind == 0) if (DbgMDKind == 0)
DbgMDKind = Context.getMetadata().getMDKindID("dbg"); DbgMDKind = Context.getMDKindID("dbg");
CurDbgLocation = L; CurDbgLocation = L;
} }

View File

@ -17,9 +17,7 @@
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/Instruction.h" #include "llvm/Instruction.h"
#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/ValueHandle.h"
#include "LLVMContextImpl.h" #include "LLVMContextImpl.h"
using namespace llvm; using namespace llvm;
static ManagedStatic<LLVMContext> GlobalContext; static ManagedStatic<LLVMContext> GlobalContext;
@ -44,6 +42,3 @@ GetElementPtrConstantExpr::GetElementPtrConstantExpr
OperandList[i+1] = IdxList[i]; OperandList[i+1] = IdxList[i];
} }
MetadataContext &LLVMContext::getMetadata() {
return pImpl->TheMetadata;
}

View File

@ -23,6 +23,7 @@
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/Assembly/Writer.h" #include "llvm/Assembly/Writer.h"
#include "llvm/Support/ValueHandle.h"
#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"
@ -171,7 +172,17 @@ public:
typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy; typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
ValueHandlesTy ValueHandles; ValueHandlesTy ValueHandles;
MetadataContext TheMetadata; /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
StringMap<unsigned> CustomMDKindNames;
typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
typedef SmallVector<MDPairTy, 2> MDMapTy;
/// MetadataStore - Collection of per-instruction metadata used in this
/// context.
DenseMap<const Instruction *, MDMapTy> MetadataStore;
LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0), LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
VoidTy(C, Type::VoidTyID), VoidTy(C, Type::VoidTyID),
LabelTy(C, Type::LabelTyID), LabelTy(C, Type::LabelTyID),
@ -187,8 +198,7 @@ public:
Int32Ty(C, 32), Int32Ty(C, 32),
Int64Ty(C, 64) { } Int64Ty(C, 64) { }
~LLVMContextImpl() ~LLVMContextImpl() {
{
ExprConstants.freeConstants(); ExprConstants.freeConstants();
ArrayConstants.freeConstants(); ArrayConstants.freeConstants();
StructConstants.freeConstants(); StructConstants.freeConstants();

View File

@ -242,152 +242,9 @@ void NamedMDNode::dropAllReferences() {
} }
//===----------------------------------------------------------------------===//
// MetadataContextImpl implementation.
//
namespace llvm {
class MetadataContextImpl {
public:
typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
typedef SmallVector<MDPairTy, 2> MDMapTy;
typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
friend class BitcodeReader;
private:
/// MetadataStore - Collection of metadata used in this context.
MDStoreTy MetadataStore;
/// MDHandlerNames - Map to hold metadata handler names.
StringMap<unsigned> MDHandlerNames;
public:
// Name <-> ID mapping methods.
unsigned getMDKindID(StringRef Name);
void getMDKindNames(SmallVectorImpl<StringRef> &) const;
// Instruction metadata methods.
MDNode *getMetadata(const Instruction *Inst, unsigned Kind);
void getAllMetadata(const Instruction *Inst,
SmallVectorImpl<std::pair<unsigned, MDNode*> > &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<StringRef> &Names) const {
Names.resize(MDHandlerNames.size()+1);
Names[0] = "";
for (StringMap<unsigned>::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<std::pair<unsigned, MDNode*> > &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 implementation.
// //
MetadataContext::MetadataContext() : pImpl(new MetadataContextImpl()) { }
MetadataContext::~MetadataContext() { delete pImpl; }
#ifndef NDEBUG #ifndef NDEBUG
/// isValidName - Return true if Name is a valid custom metadata handler name. /// isValidName - Return true if Name is a valid custom metadata handler name.
@ -408,15 +265,25 @@ static bool isValidName(StringRef MDName) {
#endif #endif
/// getMDKindID - Return a unique non-zero ID for the specified metadata kind. /// 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"); 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 /// getHandlerNames - Populate client supplied smallvector using custome
/// metadata name and ID. /// metadata name and ID.
void MetadataContext::getMDKindNames(SmallVectorImpl<StringRef> &N) const { void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
pImpl->getMDKindNames(N); Names.resize(pImpl->CustomMDKindNames.size()+1);
Names[0] = "";
for (StringMap<unsigned>::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<StringRef> &N) const {
void Instruction::setMetadata(const char *Kind, MDNode *Node) { void Instruction::setMetadata(const char *Kind, MDNode *Node) {
if (Node == 0 && !hasMetadata()) return; if (Node == 0 && !hasMetadata()) return;
setMetadata(getContext().getMetadata().getMDKindID(Kind), Node); setMetadata(getContext().getMDKindID(Kind), Node);
} }
MDNode *Instruction::getMetadataImpl(const char *Kind) const { 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 /// 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) { void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
if (Node == 0 && !hasMetadata()) return; 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 { 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<std::pair<unsigned, void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
MDNode*> > &Result)const { MDNode*> > &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. /// removeAllMetadata - Remove all metadata from this instruction.
void Instruction::removeAllMetadata() { void Instruction::removeAllMetadata() {
assert(hasMetadata() && "Caller should check"); assert(hasMetadata() && "Caller should check");
getContext().getMetadata().pImpl->removeAllMetadata(this); getContext().pImpl->MetadataStore.erase(this);
setHasMetadata(false);
} }

View File

@ -118,6 +118,20 @@ GlobalValue *Module::getNamedValue(StringRef Name) const {
return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); return cast_or_null<GlobalValue>(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<StringRef> &Result) const {
return Context.getMDKindNames(Result);
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Methods for easy access to the functions in the module. // Methods for easy access to the functions in the module.
// //