ValueMapper: Split out mapSimpleMetadata, NFC

Split out a helper for mapping metadata without operands.  This is any
metadata that is not an MDNode, and any MDNode where the answer is known
without looking at operands.

Through some weird twists, this function is co-recursive:

    mapSimpleMetadata
    => MapValue
    => materializeInitFor
    => linkFunctionBody
    => RemapInstructions
    => MapMetadata
    => mapSimpleMetadata

I plan to break the recursion in a follow-up.

llvm-svn: 265270
diff --git a/llvm/lib/Transforms/Utils/ValueMapper.cpp b/llvm/lib/Transforms/Utils/ValueMapper.cpp
index 64e9185..01a29cd 100644
--- a/llvm/lib/Transforms/Utils/ValueMapper.cpp
+++ b/llvm/lib/Transforms/Utils/ValueMapper.cpp
@@ -62,6 +62,9 @@
   Metadata *mapMetadataImpl(const Metadata *MD);
   Metadata *mapMetadataOp(Metadata *Op);
 
+  /// Map metadata that doesn't require visiting operands.
+  Optional<Metadata *> mapSimpleMetadata(const Metadata *MD);
+
   /// Remap the operands of an MDNode.
   ///
   /// If \c Node is temporary, uniquing cycles are ignored.  If \c Node is
@@ -317,7 +320,7 @@
   return MDNode::replaceWithUniqued(std::move(ClonedMD));
 }
 
-Metadata *Mapper::mapMetadataImpl(const Metadata *MD) {
+Optional<Metadata *> Mapper::mapSimpleMetadata(const Metadata *MD) {
   // If the value already exists in the map, use it.
   if (Optional<Metadata *> NewMD = VM.getMappedMD(MD))
     return *NewMD;
@@ -346,16 +349,22 @@
     return nullptr;
   }
 
-  // Note: this cast precedes the Flags check so we always get its associated
-  // assertion.
-  const MDNode *Node = cast<MDNode>(MD);
+  assert(isa<MDNode>(MD) && "Expected a metadata node");
 
   // If this is a module-level metadata and we know that nothing at the
   // module level is changing, then use an identity mapping.
   if (Flags & RF_NoModuleLevelChanges)
     return mapToSelf(MD);
 
+  return None;
+}
+
+Metadata *Mapper::mapMetadataImpl(const Metadata *MD) {
+  if (Optional<Metadata *> NewMD = mapSimpleMetadata(MD))
+    return *NewMD;
+
   // Require resolved nodes whenever metadata might be remapped.
+  auto *Node = cast<MDNode>(MD);
   assert(Node->isResolved() && "Unexpected unresolved node");
 
   if (Node->isDistinct())