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
|
|
|
|
//
|
2005-04-22 00:00:37 +00:00
|
|
|
// This file was developed by Reid Spencer and is distributed under the
|
2004-11-20 19:43:28 +00:00
|
|
|
// 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/PassManager.h"
|
|
|
|
#include "llvm/Analysis/LoadValueNumbering.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/System/DynamicLibrary.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
2006-08-20 19:18:36 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2004-11-20 19:43:28 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2006-08-20 19:18:36 +00:00
|
|
|
#include "llvm/Support/PassNameParser.h"
|
|
|
|
#include "llvm/Support/PluginLoader.h"
|
2006-11-17 10:09:22 +00:00
|
|
|
#include <iostream>
|
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
|
|
|
|
2006-08-20 19:18:36 +00:00
|
|
|
// Optimization Enumeration
|
2004-11-20 19:43:28 +00:00
|
|
|
enum OptimizationLevels {
|
|
|
|
OPT_FAST_COMPILE = 1,
|
|
|
|
OPT_SIMPLE = 2,
|
|
|
|
OPT_AGGRESSIVE = 3,
|
|
|
|
OPT_LINK_TIME = 4,
|
|
|
|
OPT_AGGRESSIVE_LINK_TIME = 5
|
|
|
|
};
|
|
|
|
|
2006-08-20 19:18:36 +00:00
|
|
|
// Optimization Options
|
2004-11-20 19:43:28 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
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.
|
|
|
|
void Optimize(Module* M) {
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
if (!DisableOptimizations) {
|
2005-10-18 06:29:43 +00:00
|
|
|
// 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(!DisableInternalize));
|
2004-11-20 19:43:28 +00:00
|
|
|
|
2007-02-08 18:13:59 +00:00
|
|
|
// 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());
|
|
|
|
|
2004-11-20 19:43:28 +00:00
|
|
|
// 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());
|
|
|
|
|
2007-02-08 18:13:59 +00:00
|
|
|
// 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());
|
|
|
|
|
2004-11-20 19:43:28 +00:00
|
|
|
if (!DisableInline)
|
|
|
|
addPass(Passes, createFunctionInliningPass()); // Inline small functions
|
|
|
|
|
|
|
|
addPass(Passes, createPruneEHPass()); // Remove dead EH info
|
2007-02-08 18:13:59 +00:00
|
|
|
addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again.
|
2004-11-20 19:43:28 +00:00
|
|
|
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
|
|
|
|
|
2007-02-08 18:13:59 +00:00
|
|
|
// Run a few AA driven optimizations here and now, to cleanup the code.
|
2004-11-20 19:43:28 +00:00
|
|
|
addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
|
2007-02-08 18:13:59 +00:00
|
|
|
|
2004-11-20 19:43:28 +00:00
|
|
|
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());
|
|
|
|
|
2004-12-11 03:03:54 +00:00
|
|
|
// Delete basic blocks, which optimization passes may have killed...
|
|
|
|
addPass(Passes, createCFGSimplificationPass());
|
|
|
|
|
2004-11-20 19:43:28 +00:00
|
|
|
// Now that we have optimized the program, discard unreachable functions...
|
|
|
|
addPass(Passes, createGlobalDCEPass());
|
|
|
|
}
|
|
|
|
|
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
|
2006-08-20 19:18:36 +00:00
|
|
|
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());
|
2004-11-20 19:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure everything is still good.
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
|
|
|
// Run our queue of passes all at once now, efficiently.
|
|
|
|
Passes.run(*M);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|