[lto] Add beginning of -save-temps option.

Summary:
This is useful for debugging issues with LTO.
The option follows the analogous option in ld64 and the gold plugin (per
Rafael's suggestion).

For starters, this only dumps the combined bitcode file.
In a future patch I will add dumping for the .o file.

The naming of the output follows ld64's convention which is slightly more
consistent IMO (consistent `.lto.<extension>` for all the files).

Reviewers: rafael, ruiu

Subscribers: joker.eph, Bigcheese, llvm-commits

Differential Revision: http://reviews.llvm.org/D18006

llvm-svn: 263055
diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp
index 8e8d77f..0f3f02b 100644
--- a/lld/ELF/SymbolTable.cpp
+++ b/lld/ELF/SymbolTable.cpp
@@ -147,6 +147,13 @@
   Mover.move(std::move(M), Keep, [](GlobalValue &, IRMover::ValueAdder) {});
 }
 
+static void saveBCFile(std::string Path, Module &M) {
+  std::error_code EC;
+  raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
+  check(EC);
+  WriteBitcodeToFile(&M, OS, /* ShouldPreserveUseListOrder */ true);
+}
+
 // Merge all the bitcode files we have seen, codegen the result and return
 // the resulting ObjectFile.
 template <class ELFT>
@@ -156,6 +163,8 @@
   IRMover Mover(Combined);
   for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
     addBitcodeFile(Mover, *F, Context);
+  if (Config->SaveTemps)
+    saveBCFile(Config->OutputFile.str() + ".lto.bc", Combined);
   std::unique_ptr<InputFile> F = codegen(Combined);
   ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(F.release()));
   return &*ObjectFiles.back();