Pass the computed magic to createBinary and createObjectFile if available.

identify_magic is not free, so we should avoid calling it twice. The argument
also makes it cheap for createBinary to just forward to createObjectFile.

llvm-svn: 199813
diff --git a/llvm/lib/Object/Binary.cpp b/llvm/lib/Object/Binary.cpp
index 4f35d97..a0c412a 100644
--- a/llvm/lib/Object/Binary.cpp
+++ b/llvm/lib/Object/Binary.cpp
@@ -41,17 +41,19 @@
   return Data->getBufferIdentifier();
 }
 
-ErrorOr<Binary *> object::createBinary(MemoryBuffer *Source) {
+ErrorOr<Binary *> object::createBinary(MemoryBuffer *Source,
+                                       sys::fs::file_magic Type) {
   OwningPtr<MemoryBuffer> scopedSource(Source);
-  sys::fs::file_magic type = sys::fs::identify_magic(Source->getBuffer());
-  switch (type) {
+  if (Type == sys::fs::file_magic::unknown)
+    Type = sys::fs::identify_magic(Source->getBuffer());
+
+  switch (Type) {
     case sys::fs::file_magic::archive:
       return Archive::create(scopedSource.take());
     case sys::fs::file_magic::elf_relocatable:
     case sys::fs::file_magic::elf_executable:
     case sys::fs::file_magic::elf_shared_object:
     case sys::fs::file_magic::elf_core:
-      return ObjectFile::createELFObjectFile(scopedSource.take());
     case sys::fs::file_magic::macho_object:
     case sys::fs::file_magic::macho_executable:
     case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
@@ -62,19 +64,17 @@
     case sys::fs::file_magic::macho_bundle:
     case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
     case sys::fs::file_magic::macho_dsym_companion:
-      return ObjectFile::createMachOObjectFile(scopedSource.take());
-    case sys::fs::file_magic::macho_universal_binary:
-      return MachOUniversalBinary::create(scopedSource.take());
     case sys::fs::file_magic::coff_object:
     case sys::fs::file_magic::coff_import_library:
     case sys::fs::file_magic::pecoff_executable:
-      return ObjectFile::createCOFFObjectFile(scopedSource.take());
+      return ObjectFile::createObjectFile(scopedSource.take(), Type);
+    case sys::fs::file_magic::macho_universal_binary:
+      return MachOUniversalBinary::create(scopedSource.take());
     case sys::fs::file_magic::unknown:
     case sys::fs::file_magic::bitcode:
-    case sys::fs::file_magic::windows_resource: {
+    case sys::fs::file_magic::windows_resource:
       // Unrecognized object file format.
       return object_error::invalid_file_type;
-    }
   }
   llvm_unreachable("Unexpected Binary File Type");
 }