AsmWriter: Extract writeMetadataAsOperand(), NFC
[oota-llvm.git] / lib / IR / DebugInfoMetadata.cpp
index 66dcc8e80558b4312efba5be3008a767a9f75a55..467d21ad36ade826c92a53f3b0799a8266061f98 100644 (file)
@@ -71,21 +71,26 @@ MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
                    Storage, Context.pImpl->MDLocations);
 }
 
-/// \brief Get the MDString, or nullptr if the string is empty.
-static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
-  if (S.empty())
-    return nullptr;
-  return MDString::get(Context, S);
+static StringRef getString(const MDString *S) {
+  if (S)
+    return S->getString();
+  return StringRef();
 }
 
+#ifndef NDEBUG
+static bool isCanonical(const MDString *S) {
+  return !S || !S->getString().empty();
+}
+#endif
+
 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
-                                            StringRef Header,
+                                            MDString *Header,
                                             ArrayRef<Metadata *> DwarfOps,
                                             StorageType Storage,
                                             bool ShouldCreate) {
   unsigned Hash = 0;
   if (Storage == Uniqued) {
-    GenericDebugNodeInfo::KeyTy Key(Tag, Header, DwarfOps);
+    GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
     if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
       return N;
     if (!ShouldCreate)
@@ -96,7 +101,8 @@ GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
   }
 
   // Use a nullptr for empty headers.
-  Metadata *PreOps[] = {getCanonicalMDString(Context, Header)};
+  assert(isCanonical(Header) && "Expected canonical MDString");
+  Metadata *PreOps[] = {Header};
   return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
                        Context, Storage, Hash, Tag, PreOps, DwarfOps),
                    Storage, Context.pImpl->GenericDebugNodes);