Handle register masks in branch folding.
[oota-llvm.git] / tools / llvm-ld / Optimize.cpp
index 31b0bf0a594e4ebe83e30d3e794a8c5992c4e2e7..7f3f90014fc28b092e9f6eb0fba8e3d7c45328f4 100644 (file)
 
 #include "llvm/Module.h"
 #include "llvm/PassManager.h"
-#include "llvm/Analysis/LoadValueNumbering.h"
-#include "llvm/Analysis/Passes.h"
-#include "llvm/Analysis/LoopPass.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Support/CommandLine.h"
-#include "llvm/System/DynamicLibrary.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
-#include "llvm/Transforms/IPO.h"
-#include "llvm/Transforms/Scalar.h"
 #include "llvm/Support/PassNameParser.h"
 #include "llvm/Support/PluginLoader.h"
-#include <iostream>
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/Scalar.h"
 using namespace llvm;
 
 // Pass Name Options as generated by the PassNameParser
@@ -35,35 +33,6 @@ static cl::list<const PassInfo*, bool, PassNameParser>
 //Don't verify at the end
 static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
 
-// Optimization Enumeration
-enum OptimizationLevels {
-  OPT_FAST_COMPILE         = 1,
-  OPT_SIMPLE               = 2,
-  OPT_AGGRESSIVE           = 3,
-  OPT_LINK_TIME            = 4,
-  OPT_AGGRESSIVE_LINK_TIME = 5
-};
-
-// Optimization Options
-static cl::opt<OptimizationLevels> OptLevel(
-  cl::desc("Choose level of optimization to apply:"),
-  cl::init(OPT_FAST_COMPILE), cl::values(
-    clEnumValN(OPT_FAST_COMPILE,"O0",
-      "An alias for the -O1 option."),
-    clEnumValN(OPT_FAST_COMPILE,"O1",
-      "Optimize for linking speed, not execution speed."),
-    clEnumValN(OPT_SIMPLE,"O2",
-      "Perform only required/minimal optimizations"),
-    clEnumValN(OPT_AGGRESSIVE,"O3",
-      "An alias for the -O2 option."),
-    clEnumValN(OPT_LINK_TIME,"O4",
-      "Perform standard link time optimizations"),
-    clEnumValN(OPT_AGGRESSIVE_LINK_TIME,"O5",
-      "Perform aggressive link time optimizations"),
-    clEnumValEnd
-  )
-);
-
 static cl::opt<bool> DisableInline("disable-inlining",
   cl::desc("Do not run the inliner pass"));
 
@@ -105,11 +74,10 @@ static inline void addPass(PassManager &PM, Pass *P) {
 }
 
 namespace llvm {
-
 /// Optimize - Perform link time optimizations. This will run the scalar
 /// optimizations, any loaded plugin-optimization modules, and then the
 /// inter-procedural optimizations if applicable.
-void Optimize(ModuleM) {
+void Optimize(Module *M) {
 
   // Instantiate the pass manager to organize the passes.
   PassManager Passes;
@@ -121,66 +89,9 @@ void Optimize(Module* M) {
   // Add an appropriate TargetData instance for this module...
   addPass(Passes, new TargetData(M));
 
-  if (!DisableOptimizations) {
-    // Now that composite has been compiled, scan through the module, looking
-    // for a main function.  If main is defined, mark all other functions
-    // internal.
-    if (!DisableInternalize)
-      addPass(Passes, createInternalizePass(true));
-
-    // Propagate constants at call sites into the functions they call.  This
-    // opens opportunities for globalopt (and inlining) by substituting function
-    // pointers passed as arguments to direct uses of functions.  
-    addPass(Passes, createIPSCCPPass());
-
-    // Now that we internalized some globals, see if we can hack on them!
-    addPass(Passes, createGlobalOptimizerPass());
-
-    // Linking modules together can lead to duplicated global constants, only
-    // keep one copy of each constant...
-    addPass(Passes, createConstantMergePass());
-
-    // Remove unused arguments from functions...
-    addPass(Passes, createDeadArgEliminationPass());
-
-    // Reduce the code after globalopt and ipsccp.  Both can open up significant
-    // simplification opportunities, and both can propagate functions through
-    // function pointers.  When this happens, we often have to resolve varargs
-    // calls, etc, so let instcombine do this.
-    addPass(Passes, createInstructionCombiningPass());
-
-    if (!DisableInline)
-      addPass(Passes, createFunctionInliningPass()); // Inline small functions
-
-    addPass(Passes, createPruneEHPass());            // Remove dead EH info
-    addPass(Passes, createGlobalOptimizerPass());    // Optimize globals again.
-    addPass(Passes, createGlobalDCEPass());          // Remove dead functions
-
-    // If we didn't decide to inline a function, check to see if we can
-    // transform it to pass arguments by value instead of by reference.
-    addPass(Passes, createArgumentPromotionPass());
-
-    // The IPO passes may leave cruft around.  Clean up after them.
-    addPass(Passes, createInstructionCombiningPass());
-
-    addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
-
-    // Run a few AA driven optimizations here and now, to cleanup the code.
-    addPass(Passes, createGlobalsModRefPass());      // IP alias analysis
-
-    addPass(Passes, createLICMPass());               // Hoist loop invariants
-    addPass(Passes, createGVNPass());                  // Remove redundancies
-    addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
-
-    // Cleanup and simplify the code after the scalar optimizations.
-    addPass(Passes, createInstructionCombiningPass());
-
-    // Delete basic blocks, which optimization passes may have killed...
-    addPass(Passes, createCFGSimplificationPass());
-
-    // Now that we have optimized the program, discard unreachable functions...
-    addPass(Passes, createGlobalDCEPass());
-  }
+  if (!DisableOptimizations)
+    PassManagerBuilder().populateLTOPassManager(Passes, !DisableInternalize,
+                                                !DisableInline);
 
   // If the -s or -S command line options were specified, strip the symbols out
   // of the resulting program to make it smaller.  -s and -S are GNU ld options
@@ -195,8 +106,8 @@ void Optimize(Module* M) {
     if (Opt->getNormalCtor())
       addPass(Passes, Opt->getNormalCtor()());
     else
-      std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName() 
-                << "\n";
+      errs() << "llvm-ld: cannot create pass: " << Opt->getPassName() 
+             << "\n";
   }
 
   // The user's passes may leave cruft around. Clean up after them them but
@@ -204,7 +115,7 @@ void Optimize(Module* M) {
   if (!DisableOptimizations) {
     addPass(Passes, createInstructionCombiningPass());
     addPass(Passes, createCFGSimplificationPass());
-    addPass(Passes, createDeadCodeEliminationPass());
+    addPass(Passes, createAggressiveDCEPass());
     addPass(Passes, createGlobalDCEPass());
   }