Use Twine, instead of StringRef, for consistency.
[oota-llvm.git] / lib / VMCore / Metadata.cpp
index fb1da998adefc1968e0373fab4b06ff0971714d8..e6a962bbca0a4f5de3f3a48c5f0b5014c44ed615 100644 (file)
@@ -18,6 +18,7 @@
 #include "llvm/Instruction.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/SmallString.h"
 #include "SymbolTableListTraitsImpl.h"
 #include "llvm/Support/ValueHandle.h"
 using namespace llvm;
@@ -31,7 +32,7 @@ MDString::MDString(LLVMContext &C, StringRef S)
 
 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
   LLVMContextImpl *pImpl = Context.pImpl;
-  StringMapEntry<MDString *> &Entry = 
+  StringMapEntry<MDString *> &Entry =
     pImpl->MDStringCache.GetOrCreateValue(Str);
   MDString *&S = Entry.getValue();
   if (!S) S = new MDString(Context, Entry.getKey());
@@ -40,7 +41,7 @@ MDString *MDString::get(LLVMContext &Context, StringRef Str) {
 
 MDString *MDString::get(LLVMContext &Context, const char *Str) {
   LLVMContextImpl *pImpl = Context.pImpl;
-  StringMapEntry<MDString *> &Entry = 
+  StringMapEntry<MDString *> &Entry =
     pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
   MDString *&S = Entry.getValue();
   if (!S) S = new MDString(Context, Entry.getKey());
@@ -58,11 +59,11 @@ class MDNodeOperand : public CallbackVH {
 public:
   MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
   ~MDNodeOperand() {}
-  
+
   void set(Value *V) {
     setValPtr(V);
   }
-  
+
   virtual void deleted();
   virtual void allUsesReplacedWith(Value *NV);
 };
@@ -94,7 +95,7 @@ MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
                bool isFunctionLocal)
 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
   NumOperands = NumVals;
+
   if (isFunctionLocal)
     setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
 
@@ -107,13 +108,13 @@ MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
 
 /// ~MDNode - Destroy MDNode.
 MDNode::~MDNode() {
-  assert((getSubclassDataFromValue() & DestroyFlag) != 0 && 
+  assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
          "Not being destroyed through destroy()?");
   if (!isNotUniqued()) {
     LLVMContextImpl *pImpl = getType()->getContext().pImpl;
     pImpl->MDNodeSet.RemoveNode(this);
   }
-  
+
   // Destroy the operands.
   for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
        Op != E; ++Op)
@@ -145,7 +146,7 @@ MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
         Value *V = Vals[i];
         if (!V) continue;
         if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
-            isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal()) {
+            (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
           isFunctionLocal = true;
           break;
         }
@@ -162,7 +163,7 @@ MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
     // Coallocate space for the node and Operands together, then placement new.
     void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
     N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
-    
+
     // InsertPoint will have been set by the FindNodeOrInsertPos call.
     pImpl->MDNodeSet.InsertNode(N, InsertPoint);
   }
@@ -192,7 +193,7 @@ void MDNode::Profile(FoldingSetNodeID &ID) const {
 // Replace value from this node's operand list.
 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
   Value *From = *Op;
-  
+
   if (From == To)
     return;
 
@@ -202,7 +203,7 @@ void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
   // If this node is already not being uniqued (because one of the operands
   // already went to null), then there is nothing else to do here.
   if (isNotUniqued()) return;
-  
+
   LLVMContextImpl *pImpl = getType()->getContext().pImpl;
 
   // Remove "this" from the context map.  FoldingSet doesn't have to reprofile
@@ -216,7 +217,7 @@ void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
     setIsNotUniqued();
     return;
   }
-  
+
   // Now that the node is out of the folding set, get ready to reinsert it.
   // First, check to see if another node with the same operands already exists
   // in the set.  If it doesn't exist, this returns the position to insert it.
@@ -239,32 +240,50 @@ void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
 //===----------------------------------------------------------------------===//
 // NamedMDNode implementation.
 //
+
+namespace llvm {
+// SymbolTableListTraits specialization for MDSymbolTable.
+void ilist_traits<NamedMDNode>
+::addNodeToList(NamedMDNode *N) {
+  assert(N->getParent() == 0 && "Value already in a container!!");
+  Module *Owner = getListOwner();
+  N->setParent(Owner);
+  MDSymbolTable &ST = Owner->getMDSymbolTable();
+  ST.insert(N->getName(), N);
+}
+
+void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
+  N->setParent(0);
+  Module *Owner = getListOwner();
+  MDSymbolTable &ST = Owner->getMDSymbolTable();
+  ST.remove(N->getName());
+}
+}
+
 static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
   return *(SmallVector<WeakVH, 4>*)Operands;
 }
 
-NamedMDNode::NamedMDNode(LLVMContext &C, StringRef N,
-                         MDNode *const *MDs, 
+NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
+                         MDNode *const *MDs,
                          unsigned NumMDs, Module *ParentModule)
   : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
   setName(N);
   Operands = new SmallVector<WeakVH, 4>();
-    
+
   SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
   for (unsigned i = 0; i != NumMDs; ++i)
     Node.push_back(WeakVH(MDs[i]));
 
-  if (ParentModule) {
+  if (ParentModule)
     ParentModule->getNamedMDList().push_back(this);
-    ParentModule->addMDNodeName(N, this);
-  }
 }
 
 NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
   assert(NMD && "Invalid source NamedMDNode!");
   SmallVector<MDNode *, 4> Elems;
   Elems.reserve(NMD->getNumOperands());
-  
+
   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
     Elems.push_back(NMD->getOperand(i));
   return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
@@ -295,7 +314,6 @@ void NamedMDNode::addOperand(MDNode *M) {
 /// eraseFromParent - Drop all references and remove the node from parent
 /// module.
 void NamedMDNode::eraseFromParent() {
-  getParent()->getMDSymbolTable().remove(getName());
   getParent()->getNamedMDList().erase(this);
 }
 
@@ -305,9 +323,23 @@ void NamedMDNode::dropAllReferences() {
 }
 
 /// setName - Set the name of this named metadata.
-void NamedMDNode::setName(StringRef N) {
-  if (!N.empty())
-    Name = N.str();
+void NamedMDNode::setName(const Twine &NewName) {
+  assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
+
+  SmallString<256> NameData;
+  NewName.toVector(NameData);
+
+  const char *NameStr = NameData.data();
+  unsigned NameLen = NameData.size();
+
+  StringRef NameRef = StringRef(NameStr, NameLen);
+  // Name isn't changing?
+  if (getName() == NameRef)
+    return;
+
+  Name = NameRef.str();
+  if (Parent)
+    Parent->getMDSymbolTable().insert(NameRef, this);
 }
 
 /// getName - Return a constant reference to this named metadata's name.
@@ -340,9 +372,9 @@ static bool isValidName(StringRef MDName) {
 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
 unsigned LLVMContext::getMDKindID(StringRef Name) const {
   assert(isValidName(Name) && "Invalid MDNode name");
-  
+
   unsigned &Entry = pImpl->CustomMDKindNames[Name];
-  
+
   // If this is new, assign it its ID.
   if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
   return Entry;
@@ -354,7 +386,7 @@ void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
   Names.resize(pImpl->CustomMDKindNames.size()+1);
   Names[0] = "";
   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
-       E = pImpl->CustomMDKindNames.end(); I != E; ++I) 
+       E = pImpl->CustomMDKindNames.end(); I != E; ++I)
     // MD Handlers are numbered from 1.
     Names[I->second] = I->first();
 }
@@ -377,7 +409,7 @@ MDNode *Instruction::getMetadataImpl(const char *Kind) const {
 /// Node is null.
 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
   if (Node == 0 && !hasMetadata()) return;
-  
+
   // Handle the case when we're adding/updating metadata on an instruction.
   if (Node) {
     LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
@@ -392,24 +424,24 @@ void Instruction::setMetadata(unsigned KindID, MDNode *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) {
@@ -424,7 +456,7 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
   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)
@@ -439,10 +471,10 @@ void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
   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());