Convert llvm-ld to use the PluginLoader like opt instead of having its
authorReid Spencer <rspencer@reidspencer.com>
Sun, 20 Aug 2006 19:18:36 +0000 (19:18 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Sun, 20 Aug 2006 19:18:36 +0000 (19:18 +0000)
one-off (and broken) RunOptimizations function. Also, run some cleanup
passes after the user's loaded passes run. This make sure to clean up
any cruft left around by thos passes.

This patch was inspired by a patch submitted by Bram Adams.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29781 91177308-0d34-0410-b5e6-96231b3b80d8

tools/llvm-ld/Optimize.cpp

index b61e8ef7db4e3d094bd91d4681ebeff735a12769..1e391d63b84aa9271f6447d92b1ef7432712a3f3 100644 (file)
 #include "llvm/Support/CommandLine.h"
 #include "llvm/System/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"
 using namespace llvm;
 
-// Optimization Options
+// Pass Name Options as generated by the PassNameParser
+static cl::list<const PassInfo*, bool, 
+  FilteredPassNameParser<PassInfo::Optimization> >
+  OptimizationList(cl::desc("Optimizations available:"));
 
+// Optimization Enumeration
 enum OptimizationLevels {
   OPT_FAST_COMPILE         = 1,
   OPT_SIMPLE               = 2,
@@ -33,6 +40,7 @@ enum OptimizationLevels {
   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(
@@ -72,10 +80,6 @@ static cl::alias ExportDynamic("export-dynamic",
   cl::aliasopt(DisableInternalize),
   cl::desc("Alias for -disable-internalize"));
 
-static cl::list<std::string> LoadableModules("load",
-  cl::value_desc("path"),
-  cl::desc("Load an optimization module and run it"));
-
 // A utility function that adds a pass to the pass manager but will also add
 // a verifier pass after if we're supposed to verify.
 static inline void addPass(PassManager &PM, Pass *P) {
@@ -150,9 +154,8 @@ void Optimize(Module* M) {
 
     addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
 
-    // Run a few AA driven optimizations here and now, to cleanup the code.
+    // Run a few AA driven optimizations, to cleanup the code.
     addPass(Passes, createGlobalsModRefPass());      // IP alias analysis
-
     addPass(Passes, createLICMPass());               // Hoist loop invariants
     addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
     addPass(Passes, createGCSEPass());               // Remove common subexprs
@@ -168,18 +171,27 @@ void Optimize(Module* M) {
     addPass(Passes, createGlobalDCEPass());
   }
 
-  std::vector<std::string> plugins = LoadableModules;
-  for (std::vector<std::string>::iterator I = plugins.begin(),
-      E = plugins.end(); I != E; ++I) {
-    sys::DynamicLibrary dll(I->c_str());
-    typedef void (*OptimizeFunc)(PassManager&,int);
-    OptimizeFunc OF = OptimizeFunc(
-        (intptr_t)dll.GetAddressOfSymbol("RunOptimizations"));
-    if (OF == 0) {
-      throw std::string("Optimization Module '") + *I +
-        "' is missing the RunOptimizations symbol";
-    }
-    (*OF)(Passes,OptLevel);
+  // Create a new optimization pass for each one specified on the command line
+  std::auto_ptr<TargetMachine> target;
+  for (unsigned i = 0; i < OptimizationList.size(); ++i) {
+    const PassInfo *Opt = OptimizationList[i];
+    
+    if (Opt->getNormalCtor())
+      Passes.add(Opt->getNormalCtor()());
+    else if (Opt->getTargetCtor()) {
+      assert(target.get() && "Could not allocate target machine!");
+      Passes.add(Opt->getTargetCtor()(*target.get()));
+    } else
+      std::cerr << "llvm-ld: cannot create pass: " << Opt->getPassName() 
+                << "\n";
+  }
+
+  // The user's passes may leave cruft around. Clean up after them them but
+  // only if we haven't got DisableOptimizations set
+  if (!DisableOptimizations) {
+    addPass(Passes, createInstructionCombiningPass());
+    addPass(Passes, createCFGSimplificationPass());
+    addPass(Passes, createGlobalDCEPass());
   }
 
   // Make sure everything is still good.