[ThinLTO] Deduplicate function index loading into shared helper (NFC)
[oota-llvm.git] / tools / llvm-link / llvm-link.cpp
index 9291e9b58628a081690251ba5e1f35b30c865dc9..984dfdcf70e5e85dda9ca506172f926fc7a0082b 100644 (file)
@@ -141,27 +141,6 @@ static void diagnosticHandler(const DiagnosticInfo &DI) {
   errs() << '\n';
 }
 
-/// Load a function index if requested by the -functionindex option.
-static ErrorOr<std::unique_ptr<FunctionInfoIndex>>
-loadIndex(const Module *ExportingModule = nullptr) {
-  assert(!FunctionIndex.empty());
-  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
-      MemoryBuffer::getFileOrSTDIN(FunctionIndex);
-  std::error_code EC = FileOrErr.getError();
-  if (EC)
-    return EC;
-  MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef();
-  ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
-      object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler,
-                                              ExportingModule);
-  EC = ObjOrErr.getError();
-  if (EC)
-    return EC;
-
-  object::FunctionIndexObjectFile &Obj = **ObjOrErr;
-  return Obj.takeIndex();
-}
-
 /// Import any functions requested via the -import option.
 static bool importFunctions(const char *argv0, LLVMContext &Context,
                             Linker &L) {
@@ -208,7 +187,8 @@ static bool importFunctions(const char *argv0, LLVMContext &Context,
 
     std::unique_ptr<FunctionInfoIndex> Index;
     if (!FunctionIndex.empty()) {
-      ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr = loadIndex();
+      ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr =
+          llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler);
       std::error_code EC = IndexOrErr.getError();
       if (EC) {
         errs() << EC.message() << '\n';
@@ -245,7 +225,8 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
     // local functions/variables as exported and promote if necessary.
     std::unique_ptr<FunctionInfoIndex> Index;
     if (!FunctionIndex.empty()) {
-      ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr = loadIndex(&*M);
+      ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr =
+          llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler, &*M);
       std::error_code EC = IndexOrErr.getError();
       if (EC) {
         errs() << EC.message() << '\n';