Convert llvm-ld to use the PluginLoader like opt instead of having its

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
This commit is contained in:
Reid Spencer 2006-08-20 19:18:36 +00:00
parent 3717ca965b
commit 8e33fae0ed

View File

@ -19,12 +19,19 @@
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/System/DynamicLibrary.h" #include "llvm/System/DynamicLibrary.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/IPO.h" #include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Support/PassNameParser.h"
#include "llvm/Support/PluginLoader.h"
using namespace llvm; 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 { enum OptimizationLevels {
OPT_FAST_COMPILE = 1, OPT_FAST_COMPILE = 1,
OPT_SIMPLE = 2, OPT_SIMPLE = 2,
@ -33,6 +40,7 @@ enum OptimizationLevels {
OPT_AGGRESSIVE_LINK_TIME = 5 OPT_AGGRESSIVE_LINK_TIME = 5
}; };
// Optimization Options
static cl::opt<OptimizationLevels> OptLevel( static cl::opt<OptimizationLevels> OptLevel(
cl::desc("Choose level of optimization to apply:"), cl::desc("Choose level of optimization to apply:"),
cl::init(OPT_FAST_COMPILE), cl::values( cl::init(OPT_FAST_COMPILE), cl::values(
@ -72,10 +80,6 @@ static cl::alias ExportDynamic("export-dynamic",
cl::aliasopt(DisableInternalize), cl::aliasopt(DisableInternalize),
cl::desc("Alias for -disable-internalize")); 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 utility function that adds a pass to the pass manager but will also add
// a verifier pass after if we're supposed to verify. // a verifier pass after if we're supposed to verify.
static inline void addPass(PassManager &PM, Pass *P) { static inline void addPass(PassManager &PM, Pass *P) {
@ -150,9 +154,8 @@ void Optimize(Module* M) {
addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas 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, createGlobalsModRefPass()); // IP alias analysis
addPass(Passes, createLICMPass()); // Hoist loop invariants addPass(Passes, createLICMPass()); // Hoist loop invariants
addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
addPass(Passes, createGCSEPass()); // Remove common subexprs addPass(Passes, createGCSEPass()); // Remove common subexprs
@ -168,18 +171,27 @@ void Optimize(Module* M) {
addPass(Passes, createGlobalDCEPass()); addPass(Passes, createGlobalDCEPass());
} }
std::vector<std::string> plugins = LoadableModules; // Create a new optimization pass for each one specified on the command line
for (std::vector<std::string>::iterator I = plugins.begin(), std::auto_ptr<TargetMachine> target;
E = plugins.end(); I != E; ++I) { for (unsigned i = 0; i < OptimizationList.size(); ++i) {
sys::DynamicLibrary dll(I->c_str()); const PassInfo *Opt = OptimizationList[i];
typedef void (*OptimizeFunc)(PassManager&,int);
OptimizeFunc OF = OptimizeFunc( if (Opt->getNormalCtor())
(intptr_t)dll.GetAddressOfSymbol("RunOptimizations")); Passes.add(Opt->getNormalCtor()());
if (OF == 0) { else if (Opt->getTargetCtor()) {
throw std::string("Optimization Module '") + *I + assert(target.get() && "Could not allocate target machine!");
"' is missing the RunOptimizations symbol"; Passes.add(Opt->getTargetCtor()(*target.get()));
} } else
(*OF)(Passes,OptLevel); 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. // Make sure everything is still good.