llvm/test/CodeGen/R600/fminnum.ll: Relax an expression for NaN on MSVCRT like r204118.
[oota-llvm.git] / tools / opt / opt.cpp
index e9c239aff115d1e04e70b28f820496b59b76b8f0..af86e2dbd601024bb8eae43ced958ecb8bb29e05 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/LLVMContext.h"
-#include "llvm/Module.h"
-#include "llvm/ModuleProvider.h"
-#include "llvm/PassManager.h"
-#include "llvm/CallGraphSCCPass.h"
-#include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/Assembly/PrintModulePass.h"
-#include "llvm/Analysis/Verifier.h"
-#include "llvm/Analysis/LoopPass.h"
+#include "BreakpointPrinter.h"
+#include "NewPMDriver.h"
+#include "PassPrinters.h"
+#include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/CallGraph.h"
-#include "llvm/Target/TargetData.h"
-#include "llvm/Target/TargetMachine.h"
-#include "llvm/Support/PassNameParser.h"
-#include "llvm/System/Signals.h"
+#include "llvm/Analysis/CallGraphSCCPass.h"
+#include "llvm/Analysis/LoopPass.h"
+#include "llvm/Analysis/RegionPass.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/Bitcode/BitcodeWriterPass.h"
+#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DebugInfo.h"
+#include "llvm/IR/IRPrintingPasses.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/LegacyPassNameParser.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Verifier.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/LinkAllIR.h"
+#include "llvm/LinkAllPasses.h"
+#include "llvm/MC/SubtargetFeature.h"
+#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/ManagedStatic.h"
-#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PluginLoader.h"
-#include "llvm/Support/StandardPasses.h"
+#include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/SystemUtils.h"
-#include "llvm/Support/raw_ostream.h"
-#include "llvm/LinkAllPasses.h"
-#include "llvm/LinkAllVMCore.h"
-#include <memory>
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
 #include <algorithm>
+#include <memory>
 using namespace llvm;
+using namespace opt_tool;
 
 // The OptimizationList is automatically populated with registered Passes by the
 // PassNameParser.
@@ -44,6 +62,15 @@ using namespace llvm;
 static cl::list<const PassInfo*, bool, PassNameParser>
 PassList(cl::desc("Optimizations available:"));
 
+// This flag specifies a textual description of the optimization pass pipeline
+// to run over the module. This flag switches opt to use the new pass manager
+// infrastructure, completely disabling all of the flags specific to the old
+// pass management.
+static cl::opt<std::string> PassPipeline(
+    "passes",
+    cl::desc("A textual description of the pass pipeline for optimizing"),
+    cl::Hidden);
+
 // Other command line options...
 //
 static cl::opt<std::string>
@@ -52,10 +79,10 @@ InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
 
 static cl::opt<std::string>
 OutputFilename("o", cl::desc("Override output filename"),
-               cl::value_desc("filename"), cl::init("-"));
+               cl::value_desc("filename"));
 
 static cl::opt<bool>
-Force("f", cl::desc("Overwrite output files"));
+Force("f", cl::desc("Enable binary output on terminals"));
 
 static cl::opt<bool>
 PrintEachXForm("p", cl::desc("Print module after each transformation"));
@@ -64,6 +91,9 @@ static cl::opt<bool>
 NoOutput("disable-output",
          cl::desc("Do not write result bitcode file"), cl::Hidden);
 
+static cl::opt<bool>
+OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
+
 static cl::opt<bool>
 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
 
@@ -81,33 +111,52 @@ static cl::opt<bool>
 DisableOptimizations("disable-opt",
                      cl::desc("Do not run any optimization passes"));
 
-static cl::opt<bool>
-DisableInternalize("disable-internalize",
-                   cl::desc("Do not mark all symbols as internal"));
-
-static cl::opt<bool>
-StandardCompileOpts("std-compile-opts",
-                   cl::desc("Include the standard compile time optimizations"));
-
 static cl::opt<bool>
 StandardLinkOpts("std-link-opts",
                  cl::desc("Include the standard link time optimizations"));
 
 static cl::opt<bool>
 OptLevelO1("O1",
-           cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
+           cl::desc("Optimization level 1. Similar to clang -O1"));
 
 static cl::opt<bool>
 OptLevelO2("O2",
-           cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
+           cl::desc("Optimization level 2. Similar to clang -O2"));
+
+static cl::opt<bool>
+OptLevelOs("Os",
+           cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
+
+static cl::opt<bool>
+OptLevelOz("Oz",
+           cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
 
 static cl::opt<bool>
 OptLevelO3("O3",
-           cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
+           cl::desc("Optimization level 3. Similar to clang -O3"));
+
+static cl::opt<std::string>
+TargetTriple("mtriple", cl::desc("Override target triple for module"));
 
 static cl::opt<bool>
 UnitAtATime("funit-at-a-time",
-            cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"));
+            cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
+            cl::init(true));
+
+static cl::opt<bool>
+DisableLoopUnrolling("disable-loop-unrolling",
+                     cl::desc("Disable loop unrolling in all relevant passes"),
+                     cl::init(false));
+static cl::opt<bool>
+DisableLoopVectorization("disable-loop-vectorization",
+                     cl::desc("Disable the loop vectorization pass"),
+                     cl::init(false));
+
+static cl::opt<bool>
+DisableSLPVectorization("disable-slp-vectorization",
+                        cl::desc("Disable the slp vectorization pass"),
+                        cl::init(false));
+
 
 static cl::opt<bool>
 DisableSimplifyLibCalls("disable-simplify-libcalls",
@@ -122,415 +171,436 @@ QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
 static cl::opt<bool>
 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
 
-// ---------- Define Printers for module and function passes ------------
-namespace {
-
-struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
-  static char ID;
-  const PassInfo *PassToPrint;
-  CallGraphSCCPassPrinter(const PassInfo *PI) :
-    CallGraphSCCPass(&ID), PassToPrint(PI) {}
-
-  virtual bool runOnSCC(const std::vector<CallGraphNode *>&SCC) {
-    if (!Quiet) {
-      outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-
-      for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
-        Function *F = SCC[i]->getFunction();
-        if (F) {
-          outs().flush();
-          getAnalysisID<Pass>(PassToPrint).print(cout, F->getParent());
-          cout << std::flush;
-        }
-      }
-    }
-    // Get and print pass...
-    return false;
-  }
-
-  virtual const char *getPassName() const { return "'Pass' Printer"; }
-
-  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-    AU.addRequiredID(PassToPrint);
-    AU.setPreservesAll();
-  }
-};
-
-char CallGraphSCCPassPrinter::ID = 0;
-
-struct ModulePassPrinter : public ModulePass {
-  static char ID;
-  const PassInfo *PassToPrint;
-  ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID),
-                                          PassToPrint(PI) {}
-
-  virtual bool runOnModule(Module &M) {
-    if (!Quiet) {
-      outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-      outs().flush();
-      getAnalysisID<Pass>(PassToPrint).print(cout, &M);
-      cout << std::flush;
-    }
-
-    // Get and print pass...
-    return false;
-  }
-
-  virtual const char *getPassName() const { return "'Pass' Printer"; }
-
-  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-    AU.addRequiredID(PassToPrint);
-    AU.setPreservesAll();
-  }
-};
-
-char ModulePassPrinter::ID = 0;
-struct FunctionPassPrinter : public FunctionPass {
-  const PassInfo *PassToPrint;
-  static char ID;
-  FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID),
-                                            PassToPrint(PI) {}
-
-  virtual bool runOnFunction(Function &F) {
-    if (!Quiet) {
-      outs() << "Printing analysis '" << PassToPrint->getPassName()
-              << "' for function '" << F.getName() << "':\n";
-    }
-    // Get and print pass...
-    outs().flush();
-    getAnalysisID<Pass>(PassToPrint).print(cout, F.getParent());
-    cout << std::flush;
-    return false;
-  }
-
-  virtual const char *getPassName() const { return "FunctionPass Printer"; }
-
-  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-    AU.addRequiredID(PassToPrint);
-    AU.setPreservesAll();
-  }
-};
-
-char FunctionPassPrinter::ID = 0;
-
-struct LoopPassPrinter : public LoopPass {
-  static char ID;
-  const PassInfo *PassToPrint;
-  LoopPassPrinter(const PassInfo *PI) :
-    LoopPass(&ID), PassToPrint(PI) {}
-
-  virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
-    if (!Quiet) {
-      outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-      outs().flush();
-      getAnalysisID<Pass>(PassToPrint).print(cout,
-                                  L->getHeader()->getParent()->getParent());
-      cout << std::flush;
-    }
-    // Get and print pass...
-    return false;
-  }
-
-  virtual const char *getPassName() const { return "'Pass' Printer"; }
-
-  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-    AU.addRequiredID(PassToPrint);
-    AU.setPreservesAll();
-  }
-};
-
-char LoopPassPrinter::ID = 0;
-
-struct BasicBlockPassPrinter : public BasicBlockPass {
-  const PassInfo *PassToPrint;
-  static char ID;
-  BasicBlockPassPrinter(const PassInfo *PI)
-    : BasicBlockPass(&ID), PassToPrint(PI) {}
-
-  virtual bool runOnBasicBlock(BasicBlock &BB) {
-    if (!Quiet) {
-      outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
-             << "': Pass " << PassToPrint->getPassName() << ":\n";
-    }
+static cl::opt<bool>
+PrintBreakpoints("print-breakpoints-for-testing",
+                 cl::desc("Print select breakpoints location for testing"));
 
-    // Get and print pass...
-    outs().flush();
-    getAnalysisID<Pass>(PassToPrint).print(cout, BB.getParent()->getParent());
-    cout << std::flush;
-    return false;
-  }
+static cl::opt<std::string>
+DefaultDataLayout("default-data-layout",
+          cl::desc("data layout string to use if not specified by module"),
+          cl::value_desc("layout-string"), cl::init(""));
 
-  virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
 
-  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-    AU.addRequiredID(PassToPrint);
-    AU.setPreservesAll();
-  }
-};
 
-char BasicBlockPassPrinter::ID = 0;
-inline void addPass(PassManager &PM, Pass *P) {
+static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
   // Add the pass to the pass manager...
   PM.add(P);
 
   // If we are verifying all of the intermediate steps, add the verifier...
-  if (VerifyEach) PM.add(createVerifierPass());
+  if (VerifyEach)
+    PM.add(createVerifierPass());
 }
 
-/// AddOptimizationPasses - This routine adds optimization passes
-/// based on selected optimization level, OptLevel. This routine
-/// duplicates llvm-gcc behaviour.
+/// This routine adds optimization passes based on selected optimization level,
+/// OptLevel.
 ///
 /// OptLevel - Optimization Level
-void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM,
-                           unsigned OptLevel) {
-  createStandardFunctionPasses(&FPM, OptLevel);
-
-  llvm::Pass *InliningPass = OptLevel > 1 ? createFunctionInliningPass() : 0;
-  createStandardModulePasses(&MPM, OptLevel,
-                             /*OptimizeSize=*/ false,
-                             UnitAtATime,
-                             /*UnrollLoops=*/ OptLevel > 1,
-                             !DisableSimplifyLibCalls,
-                             /*HaveExceptions=*/ true,
-                             InliningPass);
+static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
+                                  legacy::FunctionPassManager &FPM,
+                                  unsigned OptLevel, unsigned SizeLevel) {
+  FPM.add(createVerifierPass()); // Verify that input is correct
+
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+
+  if (DisableInline) {
+    // No inlining pass
+  } else if (OptLevel > 1) {
+    Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel);
+  } else {
+    Builder.Inliner = createAlwaysInlinerPass();
+  }
+  Builder.DisableUnitAtATime = !UnitAtATime;
+  Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
+                               DisableLoopUnrolling : OptLevel == 0;
+
+  // This is final, unless there is a #pragma vectorize enable
+  if (DisableLoopVectorization)
+    Builder.LoopVectorize = false;
+  // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
+  else if (!Builder.LoopVectorize)
+    Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
+
+  // When #pragma vectorize is on for SLP, do the same as above
+  Builder.SLPVectorize =
+      DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
+
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
 }
 
-void AddStandardCompilePasses(PassManager &PM) {
-  PM.add(createVerifierPass());                  // Verify that input is correct
-
-  addPass(PM, createLowerSetJmpPass());          // Lower llvm.setjmp/.longjmp
+static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
+  PassManagerBuilder Builder;
+  Builder.VerifyInput = true;
+  if (DisableOptimizations)
+    Builder.OptLevel = 0;
 
-  // If the -strip-debug command line option was specified, do it.
-  if (StripDebug)
-    addPass(PM, createStripSymbolsPass(true));
-
-  if (DisableOptimizations) return;
+  if (!DisableInline)
+    Builder.Inliner = createFunctionInliningPass();
+  Builder.populateLTOPassManager(PM);
+}
 
-  llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
+//===----------------------------------------------------------------------===//
+// CodeGen-related helper functions.
+//
 
-  // -std-compile-opts adds the same module passes as -O3.
-  createStandardModulePasses(&PM, 3,
-                             /*OptimizeSize=*/ false,
-                             /*UnitAtATime=*/ true,
-                             /*UnrollLoops=*/ true,
-                             /*SimplifyLibCalls=*/ true,
-                             /*HaveExceptions=*/ true,
-                             InliningPass);
+static CodeGenOpt::Level GetCodeGenOptLevel() {
+  if (OptLevelO1)
+    return CodeGenOpt::Less;
+  if (OptLevelO2)
+    return CodeGenOpt::Default;
+  if (OptLevelO3)
+    return CodeGenOpt::Aggressive;
+  return CodeGenOpt::None;
 }
 
-void AddStandardLinkPasses(PassManager &PM) {
-  PM.add(createVerifierPass());                  // Verify that input is correct
+// Returns the TargetMachine instance or zero if no triple is provided.
+static TargetMachine* GetTargetMachine(Triple TheTriple) {
+  std::string Error;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
+                                                         Error);
+  // Some modules don't specify a triple, and this is okay.
+  if (!TheTarget) {
+    return nullptr;
+  }
 
-  // If the -strip-debug command line option was specified, do it.
-  if (StripDebug)
-    addPass(PM, createStripSymbolsPass(true));
+  // Package up features to be passed to target/subtarget
+  std::string FeaturesStr;
+  if (MAttrs.size() || MCPU == "native") {
+    SubtargetFeatures Features;
+
+    // If user asked for the 'native' CPU, we need to autodetect features.
+    // This is necessary for x86 where the CPU might not support all the
+    // features the autodetected CPU name lists in the target. For example,
+    // not all Sandybridge processors support AVX.
+    if (MCPU == "native") {
+      StringMap<bool> HostFeatures;
+      if (sys::getHostCPUFeatures(HostFeatures))
+        for (auto &F : HostFeatures)
+          Features.AddFeature(F.first(), F.second);
+    }
 
-  if (DisableOptimizations) return;
+    for (unsigned i = 0; i != MAttrs.size(); ++i)
+      Features.AddFeature(MAttrs[i]);
+    FeaturesStr = Features.getString();
+  }
 
-  createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
-                          /*RunInliner=*/ !DisableInline,
-                          /*VerifyEach=*/ VerifyEach);
-}
+  if (MCPU == "native")
+    MCPU = sys::getHostCPUName();
 
-} // anonymous namespace
+  return TheTarget->createTargetMachine(TheTriple.getTriple(),
+                                        MCPU, FeaturesStr,
+                                        InitTargetOptionsFromCodeGenFlags(),
+                                        RelocModel, CMModel,
+                                        GetCodeGenOptLevel());
+}
 
+#ifdef LINK_POLLY_INTO_TOOLS
+namespace polly {
+void initializePollyPasses(llvm::PassRegistry &Registry);
+}
+#endif
 
 //===----------------------------------------------------------------------===//
 // main for opt
 //
 int main(int argc, char **argv) {
-  llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
+  sys::PrintStackTraceOnErrorSignal();
+  llvm::PrettyStackTraceProgram X(argc, argv);
+
+  // Enable debug stream buffering.
+  EnableDebugBuffering = true;
+
+  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
   LLVMContext &Context = getGlobalContext();
-  try {
-    cl::ParseCommandLineOptions(argc, argv,
-      "llvm .bc -> .bc modular optimizer and analysis printer\n");
-    sys::PrintStackTraceOnErrorSignal();
-
-    // Allocate a full target machine description only if necessary.
-    // FIXME: The choice of target should be controllable on the command line.
-    std::auto_ptr<TargetMachine> target;
-
-    std::string ErrorMessage;
-
-    // Load the input module...
-    std::auto_ptr<Module> M;
-    if (MemoryBuffer *Buffer
-          = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
-      M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
-      delete Buffer;
-    }
 
-    if (M.get() == 0) {
-      errs() << argv[0] << ": ";
-      if (ErrorMessage.size())
-        errs() << ErrorMessage << "\n";
-      else
-        errs() << "bitcode didn't read correctly.\n";
-      return 1;
-    }
+  InitializeAllTargets();
+  InitializeAllTargetMCs();
+  InitializeAllAsmPrinters();
+
+  // Initialize passes
+  PassRegistry &Registry = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeObjCARCOpts(Registry);
+  initializeVectorization(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeIPA(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
+  // For codegen passes, only passes that do IR to IR transformation are
+  // supported.
+  initializeCodeGenPreparePass(Registry);
+  initializeAtomicExpandPass(Registry);
+  initializeRewriteSymbolsPass(Registry);
+  initializeWinEHPreparePass(Registry);
+  initializeDwarfEHPreparePass(Registry);
+
+#ifdef LINK_POLLY_INTO_TOOLS
+  polly::initializePollyPasses(Registry);
+#endif
+
+  cl::ParseCommandLineOptions(argc, argv,
+    "llvm .bc -> .bc modular optimizer and analysis printer\n");
+
+  if (AnalyzeOnly && NoOutput) {
+    errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
+    return 1;
+  }
 
-    // Figure out what stream we are supposed to write to...
-    // FIXME: outs() is not binary!
-    raw_ostream *Out = &outs();  // Default to printing to stdout...
-    if (OutputFilename != "-") {
-      std::string ErrorInfo;
-      Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
-                               Force, ErrorInfo);
-      if (!ErrorInfo.empty()) {
-        errs() << ErrorInfo << '\n';
-        if (!Force)
-          errs() << "Use -f command line argument to force output\n";
-        delete Out;
-        return 1;
-      }
+  SMDiagnostic Err;
 
-      // Make sure that the Output file gets unlinked from the disk if we get a
-      // SIGINT
-      sys::RemoveFileOnSignal(sys::Path(OutputFilename));
-    }
+  // Load the input module...
+  std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
 
-    // If the output is set to be emitted to standard out, and standard out is a
-    // console, print out a warning message and refuse to do it.  We don't
-    // impress anyone by spewing tons of binary goo to a terminal.
-    if (!Force && !NoOutput && CheckBitcodeOutputToConsole(Out,!Quiet)) {
-      NoOutput = true;
+  if (!M) {
+    Err.print(argv[0], errs());
+    return 1;
+  }
+
+  // Strip debug info before running the verifier.
+  if (StripDebug)
+    StripDebugInfo(*M);
+
+  // Immediately run the verifier to catch any problems before starting up the
+  // pass pipelines.  Otherwise we can crash on broken code during
+  // doInitialization().
+  if (!NoVerify && verifyModule(*M, &errs())) {
+    errs() << argv[0] << ": " << InputFilename
+           << ": error: input module is broken!\n";
+    return 1;
+  }
+
+  // If we are supposed to override the target triple, do so now.
+  if (!TargetTriple.empty())
+    M->setTargetTriple(Triple::normalize(TargetTriple));
+
+  // Figure out what stream we are supposed to write to...
+  std::unique_ptr<tool_output_file> Out;
+  if (NoOutput) {
+    if (!OutputFilename.empty())
+      errs() << "WARNING: The -o (output filename) option is ignored when\n"
+                "the --disable-output option is used.\n";
+  } else {
+    // Default to standard output.
+    if (OutputFilename.empty())
+      OutputFilename = "-";
+
+    std::error_code EC;
+    Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
+    if (EC) {
+      errs() << EC.message() << '\n';
+      return 1;
     }
+  }
 
-    // Create a PassManager to hold and optimize the collection of passes we are
-    // about to build...
-    //
-    PassManager Passes;
+  Triple ModuleTriple(M->getTargetTriple());
+  TargetMachine *Machine = nullptr;
+  if (ModuleTriple.getArch())
+    Machine = GetTargetMachine(ModuleTriple);
+  std::unique_ptr<TargetMachine> TM(Machine);
+
+  // If the output is set to be emitted to standard out, and standard out is a
+  // console, print out a warning message and refuse to do it.  We don't
+  // impress anyone by spewing tons of binary goo to a terminal.
+  if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
+    if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
+      NoOutput = true;
 
-    // Add an appropriate TargetData instance for this module...
-    Passes.add(new TargetData(M.get()));
+  if (PassPipeline.getNumOccurrences() > 0) {
+    OutputKind OK = OK_NoOutput;
+    if (!NoOutput)
+      OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
+
+    VerifierKind VK = VK_VerifyInAndOut;
+    if (NoVerify)
+      VK = VK_NoVerifier;
+    else if (VerifyEach)
+      VK = VK_VerifyEachPass;
+
+    // The user has asked to use the new pass manager and provided a pipeline
+    // string. Hand off the rest of the functionality to the new code for that
+    // layer.
+    return runPassPipeline(argv[0], Context, *M, TM.get(), Out.get(),
+                           PassPipeline, OK, VK)
+               ? 0
+               : 1;
+  }
 
-    FunctionPassManager *FPasses = NULL;
-    if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
-      FPasses = new FunctionPassManager(new ExistingModuleProvider(M.get()));
-      FPasses->add(new TargetData(M.get()));
-    }
+  // Create a PassManager to hold and optimize the collection of passes we are
+  // about to build.
+  //
+  legacy::PassManager Passes;
 
-    // If the -strip-debug command line option was specified, add it.  If
-    // -std-compile-opts was also specified, it will handle StripDebug.
-    if (StripDebug && !StandardCompileOpts)
-      addPass(Passes, createStripSymbolsPass(true));
-
-    // Create a new optimization pass for each one specified on the command line
-    for (unsigned i = 0; i < PassList.size(); ++i) {
-      // Check to see if -std-compile-opts was specified before this option.  If
-      // so, handle it.
-      if (StandardCompileOpts &&
-          StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
-        AddStandardCompilePasses(Passes);
-        StandardCompileOpts = false;
-      }
+  // Add an appropriate TargetLibraryInfo pass for the module's triple.
+  TargetLibraryInfoImpl TLII(ModuleTriple);
 
-      if (StandardLinkOpts &&
-          StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
-        AddStandardLinkPasses(Passes);
-        StandardLinkOpts = false;
-      }
+  // The -disable-simplify-libcalls flag actually disables all builtin optzns.
+  if (DisableSimplifyLibCalls)
+    TLII.disableAllFunctions();
+  Passes.add(new TargetLibraryInfoWrapperPass(TLII));
 
-      if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
-        AddOptimizationPasses(Passes, *FPasses, 1);
-        OptLevelO1 = false;
-      }
+  // Add an appropriate DataLayout instance for this module.
+  const DataLayout &DL = M->getDataLayout();
+  if (DL.isDefault() && !DefaultDataLayout.empty()) {
+    M->setDataLayout(DefaultDataLayout);
+  }
 
-      if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
-        AddOptimizationPasses(Passes, *FPasses, 2);
-        OptLevelO2 = false;
-      }
+  // Add internal analysis passes from the target machine.
+  Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
+                                                     : TargetIRAnalysis()));
 
-      if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
-        AddOptimizationPasses(Passes, *FPasses, 3);
-        OptLevelO3 = false;
-      }
+  std::unique_ptr<legacy::FunctionPassManager> FPasses;
+  if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
+    FPasses.reset(new legacy::FunctionPassManager(M.get()));
+    FPasses->add(createTargetTransformInfoWrapperPass(
+        TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
+  }
 
-      const PassInfo *PassInf = PassList[i];
-      Pass *P = 0;
-      if (PassInf->getNormalCtor())
-        P = PassInf->getNormalCtor()();
-      else
-        errs() << argv[0] << ": cannot create pass: "
-               << PassInf->getPassName() << "\n";
-      if (P) {
-        bool isBBPass = dynamic_cast<BasicBlockPass*>(P) != 0;
-        bool isLPass = !isBBPass && dynamic_cast<LoopPass*>(P) != 0;
-        bool isFPass = !isLPass && dynamic_cast<FunctionPass*>(P) != 0;
-        bool isCGSCCPass = !isFPass && dynamic_cast<CallGraphSCCPass*>(P) != 0;
-
-        addPass(Passes, P);
-
-        if (AnalyzeOnly) {
-          if (isBBPass)
-            Passes.add(new BasicBlockPassPrinter(PassInf));
-          else if (isLPass)
-            Passes.add(new LoopPassPrinter(PassInf));
-          else if (isFPass)
-            Passes.add(new FunctionPassPrinter(PassInf));
-          else if (isCGSCCPass)
-            Passes.add(new CallGraphSCCPassPrinter(PassInf));
-          else
-            Passes.add(new ModulePassPrinter(PassInf));
-        }
+  if (PrintBreakpoints) {
+    // Default to standard output.
+    if (!Out) {
+      if (OutputFilename.empty())
+        OutputFilename = "-";
+
+      std::error_code EC;
+      Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
+                                                sys::fs::F_None);
+      if (EC) {
+        errs() << EC.message() << '\n';
+        return 1;
       }
+    }
+    Passes.add(createBreakpointPrinter(Out->os()));
+    NoOutput = true;
+  }
 
-      if (PrintEachXForm)
-        Passes.add(createPrintModulePass(&errs()));
+  // Create a new optimization pass for each one specified on the command line
+  for (unsigned i = 0; i < PassList.size(); ++i) {
+    if (StandardLinkOpts &&
+        StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
+      AddStandardLinkPasses(Passes);
+      StandardLinkOpts = false;
     }
 
-    // If -std-compile-opts was specified at the end of the pass list, add them.
-    if (StandardCompileOpts) {
-      AddStandardCompilePasses(Passes);
-      StandardCompileOpts = false;
+    if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
+      AddOptimizationPasses(Passes, *FPasses, 1, 0);
+      OptLevelO1 = false;
     }
 
-    if (StandardLinkOpts) {
-      AddStandardLinkPasses(Passes);
-      StandardLinkOpts = false;
+    if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
+      AddOptimizationPasses(Passes, *FPasses, 2, 0);
+      OptLevelO2 = false;
     }
 
-    if (OptLevelO1) {
-      AddOptimizationPasses(Passes, *FPasses, 1);
+    if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
+      AddOptimizationPasses(Passes, *FPasses, 2, 1);
+      OptLevelOs = false;
     }
 
-    if (OptLevelO2) {
-      AddOptimizationPasses(Passes, *FPasses, 2);
+    if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
+      AddOptimizationPasses(Passes, *FPasses, 2, 2);
+      OptLevelOz = false;
     }
 
-    if (OptLevelO3) {
-      AddOptimizationPasses(Passes, *FPasses, 3);
+    if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
+      AddOptimizationPasses(Passes, *FPasses, 3, 0);
+      OptLevelO3 = false;
     }
 
-    if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
-      FPasses->doInitialization();
-      for (Module::iterator I = M.get()->begin(), E = M.get()->end();
-           I != E; ++I)
-        FPasses->run(*I);
+    const PassInfo *PassInf = PassList[i];
+    Pass *P = nullptr;
+    if (PassInf->getTargetMachineCtor())
+      P = PassInf->getTargetMachineCtor()(TM.get());
+    else if (PassInf->getNormalCtor())
+      P = PassInf->getNormalCtor()();
+    else
+      errs() << argv[0] << ": cannot create pass: "
+             << PassInf->getPassName() << "\n";
+    if (P) {
+      PassKind Kind = P->getPassKind();
+      addPass(Passes, P);
+
+      if (AnalyzeOnly) {
+        switch (Kind) {
+        case PT_BasicBlock:
+          Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
+          break;
+        case PT_Region:
+          Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
+          break;
+        case PT_Loop:
+          Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
+          break;
+        case PT_Function:
+          Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
+          break;
+        case PT_CallGraphSCC:
+          Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
+          break;
+        default:
+          Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
+          break;
+        }
+      }
     }
 
-    // Check that the module is well formed on completion of optimization
-    if (!NoVerify && !VerifyEach)
-      Passes.add(createVerifierPass());
+    if (PrintEachXForm)
+      Passes.add(createPrintModulePass(errs()));
+  }
+
+  if (StandardLinkOpts) {
+    AddStandardLinkPasses(Passes);
+    StandardLinkOpts = false;
+  }
+
+  if (OptLevelO1)
+    AddOptimizationPasses(Passes, *FPasses, 1, 0);
+
+  if (OptLevelO2)
+    AddOptimizationPasses(Passes, *FPasses, 2, 0);
 
-    // Write bitcode out to disk or outs() as the last step...
-    if (!NoOutput && !AnalyzeOnly)
-      Passes.add(createBitcodeWriterPass(*Out));
+  if (OptLevelOs)
+    AddOptimizationPasses(Passes, *FPasses, 2, 1);
 
-    // Now that we have all of the passes ready, run them.
-    Passes.run(*M.get());
+  if (OptLevelOz)
+    AddOptimizationPasses(Passes, *FPasses, 2, 2);
 
-    // Delete the raw_fd_ostream.
-    if (Out != &outs())
-      delete Out;
-    return 0;
+  if (OptLevelO3)
+    AddOptimizationPasses(Passes, *FPasses, 3, 0);
 
-  } catch (const std::string& msg) {
-    errs() << argv[0] << ": " << msg << "\n";
-  } catch (...) {
-    errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
+  if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
+    FPasses->doInitialization();
+    for (Function &F : *M)
+      FPasses->run(F);
+    FPasses->doFinalization();
   }
-  llvm_shutdown();
-  return 1;
+
+  // Check that the module is well formed on completion of optimization
+  if (!NoVerify && !VerifyEach)
+    Passes.add(createVerifierPass());
+
+  // Write bitcode or assembly to the output as the last step...
+  if (!NoOutput && !AnalyzeOnly) {
+    if (OutputAssembly)
+      Passes.add(createPrintModulePass(Out->os()));
+    else
+      Passes.add(createBitcodeWriterPass(Out->os()));
+  }
+
+  // Before executing passes, print the final values of the LLVM options.
+  cl::PrintOptionValues();
+
+  // Now that we have all of the passes ready, run them.
+  Passes.run(*M);
+
+  // Declare success.
+  if (!NoOutput || PrintBreakpoints)
+    Out->keep();
+
+  return 0;
 }