Move command line options to the users of libLTO. Fixes --enable-shared build.

Patch by Richard Sandiford.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191680 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/llvm-lto/llvm-lto.cpp b/tools/llvm-lto/llvm-lto.cpp
index 82a2c82..2f51c0c 100644
--- a/tools/llvm-lto/llvm-lto.cpp
+++ b/tools/llvm-lto/llvm-lto.cpp
@@ -12,6 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/CommandFlags.h"
 #include "llvm/LTO/LTOCodeGenerator.h"
 #include "llvm/LTO/LTOModule.h"
 #include "llvm/Support/CommandLine.h"
@@ -23,13 +24,26 @@
 
 using namespace llvm;
 
-static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
-                                            cl::desc("<input bitcode files>"));
+static cl::opt<bool>
+DisableOpt("disable-opt", cl::init(false),
+  cl::desc("Do not run any optimization passes"));
 
-static cl::opt<std::string> OutputFilename("o",
-                                           cl::desc("Override output filename"),
-                                           cl::init(""),
-                                           cl::value_desc("filename"));
+static cl::opt<bool>
+DisableInline("disable-inlining", cl::init(false),
+  cl::desc("Do not run the inliner pass"));
+
+static cl::opt<bool>
+DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
+  cl::desc("Do not run the GVN load PRE pass"));
+
+static cl::list<std::string>
+InputFilenames(cl::Positional, cl::OneOrMore,
+  cl::desc("<input bitcode files>"));
+
+static cl::opt<std::string>
+OutputFilename("o", cl::init(""),
+  cl::desc("Override output filename"),
+  cl::value_desc("filename"));
 
 int main(int argc, char **argv) {
   // Print a stack trace if we signal out.
@@ -45,6 +59,28 @@
   InitializeAllAsmPrinters();
   InitializeAllAsmParsers();
 
+  // set up the TargetOptions for the machine
+  TargetOptions Options;
+  Options.LessPreciseFPMADOption = EnableFPMAD;
+  Options.NoFramePointerElim = DisableFPElim;
+  Options.AllowFPOpFusion = FuseFPOps;
+  Options.UnsafeFPMath = EnableUnsafeFPMath;
+  Options.NoInfsFPMath = EnableNoInfsFPMath;
+  Options.NoNaNsFPMath = EnableNoNaNsFPMath;
+  Options.HonorSignDependentRoundingFPMathOption =
+    EnableHonorSignDependentRoundingFPMath;
+  Options.UseSoftFloat = GenerateSoftFloatCalls;
+  if (FloatABIForCalls != FloatABI::Default)
+    Options.FloatABIType = FloatABIForCalls;
+  Options.NoZerosInBSS = DontPlaceZerosInBSS;
+  Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
+  Options.DisableTailCalls = DisableTailCalls;
+  Options.StackAlignmentOverride = OverrideStackAlignment;
+  Options.TrapFuncName = TrapFuncName;
+  Options.PositionIndependentExecutable = EnablePIE;
+  Options.EnableSegmentedStacks = SegmentedStacks;
+  Options.UseInitArray = UseInitArray;
+
   unsigned BaseArg = 0;
   std::string ErrorMessage;
 
@@ -52,11 +88,12 @@
 
   CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
   CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
+  CodeGen.setTargetOptions(Options);
 
   for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
     std::string error;
     OwningPtr<LTOModule> Module(LTOModule::makeLTOModule(InputFilenames[i].c_str(),
-                                                         error));
+                                                         Options, error));
     if (!error.empty()) {
       errs() << argv[0] << ": error loading file '" << InputFilenames[i]
              << "': " << error << "\n";
@@ -74,7 +111,8 @@
   if (!OutputFilename.empty()) {
     size_t len = 0;
     std::string ErrorInfo;
-    const void *Code = CodeGen.compile(&len, ErrorInfo);
+    const void *Code = CodeGen.compile(&len, DisableOpt, DisableInline,
+                                       DisableGVNLoadPRE, ErrorInfo);
     if (Code == NULL) {
       errs() << argv[0]
              << ": error compiling the code: " << ErrorInfo << "\n";
@@ -92,7 +130,8 @@
   } else {
     std::string ErrorInfo;
     const char *OutputName = NULL;
-    if (!CodeGen.compile_to_file(&OutputName, ErrorInfo)) {
+    if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
+                                 DisableGVNLoadPRE, ErrorInfo)) {
       errs() << argv[0]
              << ": error compiling the code: " << ErrorInfo
              << "\n";
diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp
index 441bc77..7996720 100644
--- a/tools/lto/lto.cpp
+++ b/tools/lto/lto.cpp
@@ -13,11 +13,24 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm-c/lto.h"
+#include "llvm/CodeGen/CommandFlags.h"
 #include "llvm/LTO/LTOCodeGenerator.h"
 #include "llvm/LTO/LTOModule.h"
 #include "llvm-c/Core.h"
 #include "llvm-c/Target.h"
 
+// extra command-line flags needed for LTOCodeGenerator
+static cl::opt<bool>
+DisableOpt("disable-opt", cl::init(false),
+  cl::desc("Do not run any optimization passes"));
+
+static cl::opt<bool>
+DisableInline("disable-inlining", cl::init(false),
+  cl::desc("Do not run the inliner pass"));
+
+static cl::opt<bool>
+DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
+  cl::desc("Do not run the GVN load PRE pass"));
 
 // Holds most recent error string.
 // *** Not thread safe ***
@@ -40,6 +53,28 @@
   }
 }
 
+static void lto_set_target_options(llvm::TargetOptions &Options) {
+  Options.LessPreciseFPMADOption = EnableFPMAD;
+  Options.NoFramePointerElim = DisableFPElim;
+  Options.AllowFPOpFusion = FuseFPOps;
+  Options.UnsafeFPMath = EnableUnsafeFPMath;
+  Options.NoInfsFPMath = EnableNoInfsFPMath;
+  Options.NoNaNsFPMath = EnableNoNaNsFPMath;
+  Options.HonorSignDependentRoundingFPMathOption =
+    EnableHonorSignDependentRoundingFPMath;
+  Options.UseSoftFloat = GenerateSoftFloatCalls;
+  if (FloatABIForCalls != llvm::FloatABI::Default)
+    Options.FloatABIType = FloatABIForCalls;
+  Options.NoZerosInBSS = DontPlaceZerosInBSS;
+  Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
+  Options.DisableTailCalls = DisableTailCalls;
+  Options.StackAlignmentOverride = OverrideStackAlignment;
+  Options.TrapFuncName = TrapFuncName;
+  Options.PositionIndependentExecutable = EnablePIE;
+  Options.EnableSegmentedStacks = SegmentedStacks;
+  Options.UseInitArray = UseInitArray;
+}
+
 /// lto_get_version - Returns a printable string.
 extern const char* lto_get_version() {
   return LTOCodeGenerator::getVersionString();
@@ -82,14 +117,18 @@
 /// (check lto_get_error_message() for details).
 lto_module_t lto_module_create(const char* path) {
   lto_initialize();
-  return LTOModule::makeLTOModule(path, sLastErrorString);
+  llvm::TargetOptions Options;
+  lto_set_target_options(Options);
+  return LTOModule::makeLTOModule(path, Options, sLastErrorString);
 }
 
 /// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
 /// error (check lto_get_error_message() for details).
 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
   lto_initialize();
-  return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
+  llvm::TargetOptions Options;
+  lto_set_target_options(Options);
+  return LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString);
 }
 
 /// lto_module_create_from_fd_at_offset - Loads an object file from disk.
@@ -99,14 +138,19 @@
                                                  size_t map_size,
                                                  off_t offset) {
   lto_initialize();
-  return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString);
+  llvm::TargetOptions Options;
+  lto_set_target_options(Options);
+  return LTOModule::makeLTOModule(fd, path, map_size, offset, Options,
+                                  sLastErrorString);
 }
 
 /// lto_module_create_from_memory - Loads an object file from memory. Returns
 /// NULL on error (check lto_get_error_message() for details).
 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
   lto_initialize();
-  return LTOModule::makeLTOModule(mem, length, sLastErrorString);
+  llvm::TargetOptions Options;
+  lto_set_target_options(Options);
+  return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString);
 }
 
 /// lto_module_dispose - Frees all memory for a module. Upon return the
@@ -150,7 +194,14 @@
 /// is an error.
 lto_code_gen_t lto_codegen_create(void) {
   lto_initialize();
-  return new LTOCodeGenerator();
+
+  TargetOptions Options;
+  lto_set_target_options(Options);
+
+  LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
+  if (CodeGen)
+    CodeGen->setTargetOptions(Options);
+  return CodeGen;
 }
 
 /// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
@@ -220,14 +271,16 @@
 /// lto_codegen_compile() is called again. On failure, returns NULL (check
 /// lto_get_error_message() for details).
 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
-  return cg->compile(length, sLastErrorString);
+  return cg->compile(length, DisableOpt, DisableInline, DisableGVNLoadPRE,
+                     sLastErrorString);
 }
 
 /// lto_codegen_compile_to_file - Generates code for all added modules into one
 /// native object file. The name of the file is written to name. Returns true on
 /// error.
 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
-  return !cg->compile_to_file(name, sLastErrorString);
+  return !cg->compile_to_file(name, DisableOpt, DisableInline, DisableGVNLoadPRE,
+                              sLastErrorString);
 }
 
 /// lto_codegen_debug_options - Used to pass extra options to the code