IR: Take an LLVMContext in Metadata::Metadata()
[oota-llvm.git] / lib / IR / Metadata.cpp
index 59beb7341843edab52efe6a3dfa97a70c578f180..0f5f969d2f66a39fe864930044d466e63ea517b2 100644 (file)
 #include "llvm/IR/LeakDetector.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/ValueHandle.h"
+
 using namespace llvm;
 
+Metadata::Metadata(LLVMContext &Context, unsigned ID)
+    : Value(Type::getMetadataTy(Context), ID) {}
+
 //===----------------------------------------------------------------------===//
 // MDString implementation.
 //
@@ -37,13 +41,21 @@ MDString::MDString(LLVMContext &C)
   : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
 
 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
-  LLVMContextImpl *pImpl = Context.pImpl;
-  StringMapEntry<Value*> &Entry =
-    pImpl->MDStringCache.GetOrCreateValue(Str);
-  Value *&S = Entry.getValue();
-  if (!S) S = new MDString(Context);
-  S->setValueName(&Entry);
-  return cast<MDString>(S);
+  auto &Store = Context.pImpl->MDStringCache;
+  auto I = Store.find(Str);
+  if (I != Store.end())
+    return &I->second;
+
+  auto *Entry =
+      StringMapEntry<MDString>::Create(Str, Store.getAllocator(), Context);
+  bool WasInserted = Store.insert(Entry);
+  (void)WasInserted;
+  assert(WasInserted && "Expected entry to be inserted");
+  return &Entry->second;
+}
+
+StringRef MDString::getString() const {
+  return StringMapEntry<MDString>::GetStringMapEntryFromValue(*this).first();
 }
 
 //===----------------------------------------------------------------------===//
@@ -109,8 +121,8 @@ void MDNode::replaceOperandWith(unsigned i, Value *Val) {
   replaceOperand(Op, Val);
 }
 
-MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
-: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
+MDNode::MDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
+    : Metadata(C, Value::MDNodeVal) {
   NumOperands = Vals.size();
 
   if (isFunctionLocal)
@@ -555,13 +567,13 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
 // NamedMDNode implementation.
 //
 
-static SmallVector<TrackingVH<Value>, 4> &getNMDOps(void *Operands) {
-  return *(SmallVector<TrackingVH<Value>, 4> *)Operands;
+static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
+  return *(SmallVector<TrackingVH<MDNode>, 4> *)Operands;
 }
 
 NamedMDNode::NamedMDNode(const Twine &N)
     : Name(N.str()), Parent(nullptr),
-      Operands(new SmallVector<TrackingVH<Value>, 4>()) {}
+      Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {}
 
 NamedMDNode::~NamedMDNode() {
   dropAllReferences();
@@ -572,16 +584,15 @@ unsigned NamedMDNode::getNumOperands() const {
   return (unsigned)getNMDOps(Operands).size();
 }
 
-Value *NamedMDNode::getOperand(unsigned i) const {
+MDNode *NamedMDNode::getOperand(unsigned i) const {
   assert(i < getNumOperands() && "Invalid Operand number!");
   return &*getNMDOps(Operands)[i];
 }
 
-void NamedMDNode::addOperand(Value *V) {
-  auto *M = cast<MDNode>(V);
+void NamedMDNode::addOperand(MDNode *M) {
   assert(!M->isFunctionLocal() &&
          "NamedMDNode operands must not be function-local!");
-  getNMDOps(Operands).push_back(TrackingVH<Value>(M));
+  getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
 }
 
 void NamedMDNode::eraseFromParent() {
@@ -600,23 +611,16 @@ StringRef NamedMDNode::getName() const {
 // Instruction Metadata method implementations.
 //
 
-void Instruction::setMetadata(StringRef Kind, Value *MD) {
-  if (!MD && !hasMetadata()) return;
-  setMetadata(getContext().getMDKindID(Kind), MD);
+void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
+  if (!Node && !hasMetadata())
+    return;
+  setMetadata(getContext().getMDKindID(Kind), Node);
 }
 
-Value *Instruction::getMetadataImpl(StringRef Kind) const {
+MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
   return getMetadataImpl(getContext().getMDKindID(Kind));
 }
 
-MDNode *Instruction::getMDNodeImpl(unsigned KindID) const {
-  return cast_or_null<MDNode>(getMetadataImpl(KindID));
-}
-
-MDNode *Instruction::getMDNodeImpl(StringRef Kind) const {
-  return cast_or_null<MDNode>(getMetadataImpl(Kind));
-}
-
 void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
   SmallSet<unsigned, 5> KnownSet;
   KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
@@ -663,12 +667,10 @@ void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
 
 /// setMetadata - Set the metadata of of the specified kind to the specified
 /// node.  This updates/replaces metadata if already present, or removes it if
-/// MD is null.
-void Instruction::setMetadata(unsigned KindID, Value *MD) {
-  if (!MD && !hasMetadata()) return;
-
-  // For now, we only expect MDNodes here.
-  MDNode *Node = cast_or_null<MDNode>(MD);
+/// Node is null.
+void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
+  if (!Node && !hasMetadata())
+    return;
 
   // Handle 'dbg' as a special case since it is not stored in the hash table.
   if (KindID == LLVMContext::MD_dbg) {
@@ -729,7 +731,7 @@ void Instruction::setAAMetadata(const AAMDNodes &N) {
   setMetadata(LLVMContext::MD_noalias, N.NoAlias);
 }
 
-Value *Instruction::getMetadataImpl(unsigned KindID) const {
+MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
   // Handle 'dbg' as a special case since it is not stored in the hash table.
   if (KindID == LLVMContext::MD_dbg)
     return DbgLoc.getAsMDNode(getContext());
@@ -746,7 +748,7 @@ Value *Instruction::getMetadataImpl(unsigned KindID) const {
 }
 
 void Instruction::getAllMetadataImpl(
-    SmallVectorImpl<std::pair<unsigned, Value *>> &Result) const {
+    SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
   Result.clear();
   
   // Handle 'dbg' as a special case since it is not stored in the hash table.
@@ -771,7 +773,7 @@ void Instruction::getAllMetadataImpl(
 }
 
 void Instruction::getAllMetadataOtherThanDebugLocImpl(
-    SmallVectorImpl<std::pair<unsigned, Value *>> &Result) const {
+    SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
   Result.clear();
   assert(hasMetadataHashEntry() &&
          getContext().pImpl->MetadataStore.count(this) &&