aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/VMCore/LLVMContext.cpp4
-rw-r--r--lib/VMCore/LLVMContextImpl.h1
-rw-r--r--lib/VMCore/Metadata.cpp72
-rw-r--r--lib/VMCore/Value.cpp5
4 files changed, 82 insertions, 0 deletions
diff --git a/lib/VMCore/LLVMContext.cpp b/lib/VMCore/LLVMContext.cpp
index 7d233d9..03f0436 100644
--- a/lib/VMCore/LLVMContext.cpp
+++ b/lib/VMCore/LLVMContext.cpp
@@ -70,3 +70,7 @@ bool LLVMContext::RemoveDeadMetadata() {
}
return Changed;
}
+
+Metadata &LLVMContext::getMetadata() {
+ return pImpl->TheMetadata;
+}
diff --git a/lib/VMCore/LLVMContextImpl.h b/lib/VMCore/LLVMContextImpl.h
index cef770c..e4660c2 100644
--- a/lib/VMCore/LLVMContextImpl.h
+++ b/lib/VMCore/LLVMContextImpl.h
@@ -179,6 +179,7 @@ public:
typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy;
ValueHandlesTy ValueHandles;
+ Metadata TheMetadata;
LLVMContextImpl(LLVMContext &C) : TheTrueVal(0), TheFalseVal(0),
VoidTy(C, Type::VoidTyID),
LabelTy(C, Type::LabelTyID),
diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp
index 1163b47..062fb96 100644
--- a/lib/VMCore/Metadata.cpp
+++ b/lib/VMCore/Metadata.cpp
@@ -15,6 +15,7 @@
#include "llvm/Metadata.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
+#include "llvm/Instruction.h"
#include "SymbolTableListTraitsImpl.h"
using namespace llvm;
@@ -251,3 +252,74 @@ void NamedMDNode::dropAllReferences() {
NamedMDNode::~NamedMDNode() {
dropAllReferences();
}
+
+//===----------------------------------------------------------------------===//
+//Metadata implementation
+//
+
+/// RegisterMDKind - Register a new metadata kind and return its ID.
+/// A metadata kind can be registered only once.
+MDKindID Metadata::RegisterMDKind(const char *Name) {
+ MDKindID Count = MDHandlerNames.size();
+ StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
+ assert(I == MDHandlerNames.end() && "Already registered MDKind!");
+ MDHandlerNames[Name] = Count + 1;
+ return Count + 1;
+}
+
+/// getMDKind - Return metadata kind. If the requested metadata kind
+/// is not registered then return 0.
+MDKindID Metadata::getMDKind(const char *Name) {
+ StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
+ if (I == MDHandlerNames.end())
+ return 0;
+
+ return I->getValue();
+}
+
+/// setMD - Attach the metadata of given kind with an Instruction.
+void Metadata::setMD(MDKindID MDKind, MDNode *Node, Instruction *Inst) {
+ MDStoreTy::iterator I = MetadataStore.find(Inst);
+ Inst->HasMetadata = true;
+ if (I == MetadataStore.end()) {
+ MDMapTy Info;
+ Info.push_back(std::make_pair(MDKind, Node));
+ MetadataStore.insert(std::make_pair(Inst, Info));
+ return;
+ }
+
+ MDMapTy &Info = I->second;
+ Info.push_back(std::make_pair(MDKind, Node));
+ return;
+}
+
+/// getMD - Get the metadata of given kind attached with an Instruction.
+/// If the metadata is not found then return 0.
+MDNode *Metadata::getMD(MDKindID MDKind, const Instruction *Inst) {
+ MDNode *Node = NULL;
+ MDStoreTy::iterator I = MetadataStore.find(Inst);
+ if (I == MetadataStore.end())
+ return Node;
+
+ MDMapTy &Info = I->second;
+ for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
+ if (I->first == MDKind)
+ Node = dyn_cast_or_null<MDNode>(I->second);
+ return Node;
+}
+
+/// ValueIsDeleted - This handler is used to update metadata store
+/// when a value is deleted.
+void Metadata::ValueIsDeleted(const Instruction *Inst) {
+ // Find Metadata handles for this instruction.
+ MDStoreTy::iterator I = MetadataStore.find(Inst);
+ if (I == MetadataStore.end())
+ return;
+ MDMapTy &Info = I->second;
+
+ // FIXME : Give all metadata handlers a chance to adjust.
+
+ // Remove the entries for this instruction.
+ Info.clear();
+ MetadataStore.erase(Inst);
+}
diff --git a/lib/VMCore/Value.cpp b/lib/VMCore/Value.cpp
index f674062..92a5b70 100644
--- a/lib/VMCore/Value.cpp
+++ b/lib/VMCore/Value.cpp
@@ -60,6 +60,11 @@ Value::Value(const Type *ty, unsigned scid)
}
Value::~Value() {
+ if (HasMetadata) {
+ LLVMContext &Context = getContext();
+ Context.pImpl->TheMetadata.ValueIsDeleted(this);
+ }
+
// Notify all ValueHandles (if present) that this value is going away.
if (HasValueHandle)
ValueHandleBase::ValueIsDeleted(this);