2004-11-20 19:43:28 +00:00
|
|
|
//===- Optimize.cpp - Optimize a complete program -------------------------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2004-11-20 19:43:28 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2004-11-20 19:43:28 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements all optimization of the linked module for llvm-ld.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2011-05-22 00:37:51 +00:00
|
|
|
#include "llvm/Support/PassManagerBuilder.h"
|
2009-07-15 16:35:29 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/DynamicLibrary.h"
|
2004-11-20 19:43:28 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2006-08-20 19:18:36 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/PassNameParser.h"
|
|
|
|
#include "llvm/Support/PluginLoader.h"
|
2004-11-20 19:43:28 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2006-08-20 19:18:36 +00:00
|
|
|
// Pass Name Options as generated by the PassNameParser
|
2006-08-27 22:07:43 +00:00
|
|
|
static cl::list<const PassInfo*, bool, PassNameParser>
|
2006-08-20 19:18:36 +00:00
|
|
|
OptimizationList(cl::desc("Optimizations available:"));
|
2004-11-20 19:43:28 +00:00
|
|
|
|
2008-03-19 22:32:43 +00:00
|
|
|
//Don't verify at the end
|
2008-03-19 20:49:51 +00:00
|
|
|
static cl::opt<bool> DontVerify("disable-verify", cl::ReallyHidden);
|
|
|
|
|
2005-04-22 00:00:37 +00:00
|
|
|
static cl::opt<bool> DisableInline("disable-inlining",
|
2004-11-20 19:43:28 +00:00
|
|
|
cl::desc("Do not run the inliner pass"));
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
2006-08-20 20:48:44 +00:00
|
|
|
static cl::opt<bool> VerifyEach("verify-each",
|
|
|
|
cl::desc("Verify intermediate results of all passes"));
|
2004-11-20 19:43:28 +00:00
|
|
|
|
2005-04-22 00:00:37 +00:00
|
|
|
static cl::alias ExportDynamic("export-dynamic",
|
2004-11-20 19:43:28 +00:00
|
|
|
cl::aliasopt(DisableInternalize),
|
|
|
|
cl::desc("Alias for -disable-internalize"));
|
|
|
|
|
2007-02-08 18:13:59 +00:00
|
|
|
static cl::opt<bool> 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<bool> 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));
|
|
|
|
|
2004-11-20 19:43:28 +00:00
|
|
|
// 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) {
|
|
|
|
// Add the pass to the pass manager...
|
|
|
|
PM.add(P);
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2004-11-20 19:43:28 +00:00
|
|
|
// If we are verifying all of the intermediate steps, add the verifier...
|
2006-08-20 20:48:44 +00:00
|
|
|
if (VerifyEach)
|
2004-11-20 19:43:28 +00:00
|
|
|
PM.add(createVerifierPass());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2005-04-22 00:00:37 +00:00
|
|
|
/// Optimize - Perform link time optimizations. This will run the scalar
|
|
|
|
/// optimizations, any loaded plugin-optimization modules, and then the
|
2004-11-20 19:43:28 +00:00
|
|
|
/// inter-procedural optimizations if applicable.
|
2011-05-22 00:21:15 +00:00
|
|
|
void Optimize(Module *M) {
|
2004-11-20 19:43:28 +00:00
|
|
|
|
|
|
|
// Instantiate the pass manager to organize the passes.
|
|
|
|
PassManager Passes;
|
|
|
|
|
|
|
|
// If we're verifying, start off with a verification pass.
|
2006-08-20 20:48:44 +00:00
|
|
|
if (VerifyEach)
|
2004-11-20 19:43:28 +00:00
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
|
|
|
// Add an appropriate TargetData instance for this module...
|
2006-06-16 18:23:49 +00:00
|
|
|
addPass(Passes, new TargetData(M));
|
2004-11-20 19:43:28 +00:00
|
|
|
|
2009-06-03 21:06:14 +00:00
|
|
|
if (!DisableOptimizations)
|
2011-05-22 00:21:15 +00:00
|
|
|
PassManagerBuilder().populateLTOPassManager(Passes, !DisableInternalize,
|
|
|
|
!DisableInline);
|
2004-11-20 19:43:28 +00:00
|
|
|
|
2007-02-08 18:13:59 +00:00
|
|
|
// 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));
|
|
|
|
|
2006-08-20 19:18:36 +00:00
|
|
|
// 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())
|
2006-08-20 20:54:38 +00:00
|
|
|
addPass(Passes, Opt->getNormalCtor()());
|
2006-12-01 21:59:37 +00:00
|
|
|
else
|
2009-07-15 16:35:29 +00:00
|
|
|
errs() << "llvm-ld: cannot create pass: " << Opt->getPassName()
|
|
|
|
<< "\n";
|
2006-08-20 19:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
2008-07-02 18:42:07 +00:00
|
|
|
addPass(Passes, createAggressiveDCEPass());
|
2006-08-20 19:18:36 +00:00
|
|
|
addPass(Passes, createGlobalDCEPass());
|
2004-11-20 19:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure everything is still good.
|
2008-03-19 22:32:43 +00:00
|
|
|
if (!DontVerify)
|
2008-03-19 20:49:51 +00:00
|
|
|
Passes.add(createVerifierPass());
|
2004-11-20 19:43:28 +00:00
|
|
|
|
|
|
|
// Run our queue of passes all at once now, efficiently.
|
|
|
|
Passes.run(*M);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|