now that libsystem no longer uses SmallVector, we can move
[oota-llvm.git] / lib / VMCore / Metadata.cpp
index 4fadfed503be03e5fb14aa0cfc411188e404590c..ac91a40e10db2bb08251fa8f866e3ef52ced441d 100644 (file)
@@ -33,22 +33,31 @@ MDString *MDString::get(LLVMContext &Context, StringRef Str) {
   StringMapEntry<MDString *> &Entry = 
     pImpl->MDStringCache.GetOrCreateValue(Str);
   MDString *&S = Entry.getValue();
-  if (S) return S;
-  
-  return S = 
-    new MDString(Context, Entry.getKey());
+  if (!S) S = new MDString(Context, Entry.getKey());
+  return S;
+}
+
+MDString *MDString::get(LLVMContext &Context, const char *Str) {
+  LLVMContextImpl *pImpl = Context.pImpl;
+  StringMapEntry<MDString *> &Entry = 
+    pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
+  MDString *&S = Entry.getValue();
+  if (!S) S = new MDString(Context, Entry.getKey());
+  return S;
 }
 
 //===----------------------------------------------------------------------===//
 // MDNode implementation.
 //
-MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals)
+MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
+               Function *LocalFunc)
   : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
   NodeSize = NumVals;
   Node = new ElementVH[NodeSize];
   ElementVH *Ptr = Node;
   for (unsigned i = 0; i != NumVals; ++i) 
     *Ptr++ = ElementVH(Vals[i], this);
+  LocalFunction = LocalFunc;
 }
 
 void MDNode::Profile(FoldingSetNodeID &ID) const {
@@ -56,35 +65,29 @@ void MDNode::Profile(FoldingSetNodeID &ID) const {
     ID.AddPointer(getElement(i));
 }
 
-MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
+MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
+                    Function *LocalFunction) {
   LLVMContextImpl *pImpl = Context.pImpl;
   FoldingSetNodeID ID;
   for (unsigned i = 0; i != NumVals; ++i)
     ID.AddPointer(Vals[i]);
+  if (LocalFunction)
+    ID.AddPointer(LocalFunction);
 
   void *InsertPoint;
-  MDNode *N;
-  {
-    N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
-  }  
-  if (N) return N;
-  
-  N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
+  MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
   if (!N) {
     // InsertPoint will have been set by the FindNodeOrInsertPos call.
-    N = new MDNode(Context, Vals, NumVals);
+    N = new MDNode(Context, Vals, NumVals, LocalFunction);
     pImpl->MDNodeSet.InsertNode(N, InsertPoint);
   }
-
   return N;
 }
 
 /// ~MDNode - Destroy MDNode.
 MDNode::~MDNode() {
-  {
-    LLVMContextImpl *pImpl = getType()->getContext().pImpl;
-    pImpl->MDNodeSet.RemoveNode(this);
-  }
+  LLVMContextImpl *pImpl = getType()->getContext().pImpl;
+  pImpl->MDNodeSet.RemoveNode(this);
   delete [] Node;
   Node = NULL;
 }
@@ -220,7 +223,7 @@ public:
   /// addMD - Attach the metadata of given kind to an Instruction.
   void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
   
-  /// removeMD - Remove metadata of given kind attached with an instuction.
+  /// removeMD - Remove metadata of given kind attached with an instruction.
   void removeMD(unsigned Kind, Instruction *Inst);
   
   /// removeAllMetadata - Remove all metadata attached with an instruction.
@@ -230,7 +233,7 @@ public:
   /// the same metadata to In2.
   void copyMD(Instruction *In1, Instruction *In2);
 
-  /// getHandlerNames - Populate client supplied smallvector using custome
+  /// getHandlerNames - Populate client-supplied smallvector using custom
   /// metadata name and ID.
   void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
 
@@ -291,7 +294,7 @@ void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
   Info.push_back(std::make_pair(MDKind, Node));
 }
 
-/// removeMD - Remove metadata of given kind attached with an instuction.
+/// removeMD - Remove metadata of given kind attached with an instruction.
 void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
   MDStoreTy::iterator I = MetadataStore.find(Inst);
   if (I == MetadataStore.end())
@@ -306,7 +309,7 @@ void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
     }
   }
 }
-  
+
 /// removeAllMetadata - Remove all metadata attached with an instruction.
 void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
   MetadataStore.erase(Inst);
@@ -341,11 +344,11 @@ MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
 /// getMDs - Get the metadata attached to an Instruction.
 void MetadataContextImpl::
 getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const {
-  MDStoreTy::iterator I = MetadataStore.find(Inst);
+  MDStoreTy::const_iterator I = MetadataStore.find(Inst);
   if (I == MetadataStore.end())
     return;
   MDs.resize(I->second.size());
-  for (MDMapTy::iterator MI = I->second.begin(), ME = I->second.end();
+  for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
        MI != ME; ++MI)
     // MD kinds are numbered from 1.
     MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
@@ -443,12 +446,12 @@ getMDs(const Instruction *Inst,
 void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
   pImpl->addMD(Kind, Node, Inst);
 }
-  
-/// removeMD - Remove metadata of given kind attached with an instuction.
+
+/// removeMD - Remove metadata of given kind attached with an instruction.
 void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
   pImpl->removeMD(Kind, Inst);
 }
-  
+
 /// removeAllMetadata - Remove all metadata attached with an instruction.
 void MetadataContext::removeAllMetadata(Instruction *Inst) {
   pImpl->removeAllMetadata(Inst);