X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=tools%2Fllvm-ld%2FOptimize.cpp;h=6143dc87d356a2d10b3efe2100a6e6d6a4346322;hb=9e6d1d1f5034347d237941f1bf08fba5c1583cd3;hp=1961a95ef53f4c1b5252b4814e1f575e609e6c93;hpb=3da94aec4d429b2ba0f65fa040c33650cade196b;p=oota-llvm.git diff --git a/tools/llvm-ld/Optimize.cpp b/tools/llvm-ld/Optimize.cpp index 1961a95ef53..6143dc87d35 100644 --- a/tools/llvm-ld/Optimize.cpp +++ b/tools/llvm-ld/Optimize.cpp @@ -2,8 +2,8 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by Reid Spencer and is distributed under the -// University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -13,44 +13,27 @@ #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/Support/StandardPasses.h" +#include "llvm/Support/raw_ostream.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 - -enum OptimizationLevels { - OPT_FAST_COMPILE = 1, - OPT_SIMPLE = 2, - OPT_AGGRESSIVE = 3, - OPT_LINK_TIME = 4, - OPT_AGGRESSIVE_LINK_TIME = 5 -}; - -static cl::opt 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 - ) -); +// Pass Name Options as generated by the PassNameParser +static cl::list + OptimizationList(cl::desc("Optimizations available:")); + +//Don't verify at the end +static cl::opt DontVerify("disable-verify", cl::ReallyHidden); static cl::opt DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); @@ -62,19 +45,24 @@ DisableOptimizations("disable-opt", static cl::opt DisableInternalize("disable-internalize", cl::desc("Do not mark all symbols as internal")); -static cl::opt Verify("verify", - cl::desc("Verify intermediate results of all passes")); - -static cl::opt Strip("s", - cl::desc("Strip symbol info from executable")); +static cl::opt VerifyEach("verify-each", + cl::desc("Verify intermediate results of all passes")); static cl::alias ExportDynamic("export-dynamic", cl::aliasopt(DisableInternalize), cl::desc("Alias for -disable-internalize")); -static cl::list LoadableModules("load", - cl::value_desc("path to loadable optimization module"), - cl::desc("Load an optimization module and run it")); +static cl::opt Strip("strip-all", + cl::desc("Strip all symbol info from executable")); + +static cl::alias A0("s", cl::desc("Alias for --strip-all"), + cl::aliasopt(Strip)); + +static cl::opt StripDebug("strip-debug", + cl::desc("Strip debugger symbol info from executable")); + +static cl::alias A1("S", cl::desc("Alias for --strip-debug"), + cl::aliasopt(StripDebug)); // 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. @@ -83,7 +71,7 @@ static inline void addPass(PassManager &PM, Pass *P) { PM.add(P); // If we are verifying all of the intermediate steps, add the verifier... - if (Verify) + if (VerifyEach) PM.add(createVerifierPass()); } @@ -98,94 +86,45 @@ void Optimize(Module* M) { PassManager Passes; // If we're verifying, start off with a verification pass. - if (Verify) + if (VerifyEach) Passes.add(createVerifierPass()); // Add an appropriate TargetData instance for this module... - addPass(Passes, new TargetData("gccld", M)); - - // Often if the programmer does not specify proper prototypes for the - // functions they are calling, they end up calling a vararg version of the - // function that does not get a body filled in (the real function has typed - // arguments). This pass merges the two functions. - addPass(Passes, createFunctionResolvingPass()); + addPass(Passes, new TargetData(M)); + + if (!DisableOptimizations) + createStandardLTOPasses(&Passes, !DisableInternalize, !DisableInline, + VerifyEach); + + // 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 + // that we are supporting; they alias -strip-all and -strip-debug. + if (Strip || StripDebug) + addPass(Passes, createStripSymbolsPass(StripDebug && !Strip)); + + // Create a new optimization pass for each one specified on the command line + std::auto_ptr target; + for (unsigned i = 0; i < OptimizationList.size(); ++i) { + const PassInfo *Opt = OptimizationList[i]; + if (Opt->getNormalCtor()) + addPass(Passes, Opt->getNormalCtor()()); + else + errs() << "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) { - if (!DisableInternalize) { - // Now that composite has been compiled, scan through the module, looking - // for a main function. If main is defined, mark all other functions - // internal. - addPass(Passes, createInternalizePass()); - } - - // 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()); - - // If the -s command line option was specified, strip the symbols out of the - // resulting program to make it smaller. -s is a GLD option that we are - // supporting. - if (Strip) - addPass(Passes, createStripSymbolsPass()); - - // Propagate constants at call sites into the functions they call. - addPass(Passes, createIPConstantPropagationPass()); - - // Remove unused arguments from functions... - addPass(Passes, createDeadArgEliminationPass()); - - if (!DisableInline) - addPass(Passes, createFunctionInliningPass()); // Inline small functions - - addPass(Passes, createPruneEHPass()); // Remove dead EH info - 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, createLoadValueNumberingPass()); // GVN for load instrs - addPass(Passes, createGCSEPass()); // Remove common subexprs - 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, createAggressiveDCEPass()); addPass(Passes, createGlobalDCEPass()); } - std::vector plugins = LoadableModules; - for (std::vector::iterator I = plugins.begin(), - E = plugins.end(); I != E; ++I) { - sys::DynamicLibrary dll(I->c_str()); - typedef void (*OptimizeFunc)(PassManager&,int); - OptimizeFunc OF = OptimizeFunc( - dll.GetAddressOfSymbol("RunOptimizations")); - if (OF == 0) { - throw std::string("Optimization Module '") + *I + - "' is missing the RunOptimizations symbol"; - } - (*OF)(Passes,OptLevel); - } - // Make sure everything is still good. - Passes.add(createVerifierPass()); + if (!DontVerify) + Passes.add(createVerifierPass()); // Run our queue of passes all at once now, efficiently. Passes.run(*M);