2001-10-18 06:05:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
// LLVM 'OPT' UTILITY
|
|
|
|
//
|
|
|
|
// Optimizations may be specified an arbitrary number of times on the command
|
|
|
|
// line, they are run in the order specified.
|
|
|
|
//
|
2001-10-18 06:05:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 00:47:12 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2002-01-31 00:47:12 +00:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-10-19 15:39:14 +00:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2002-02-20 17:56:53 +00:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2002-02-01 04:54:11 +00:00
|
|
|
#include "llvm/Transforms/UnifyMethodExitNodes.h"
|
2001-10-18 20:06:45 +00:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2001-10-31 04:29:44 +00:00
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.h"
|
2001-11-01 02:41:09 +00:00
|
|
|
#include "llvm/Transforms/LevelChange.h"
|
2002-01-21 23:17:48 +00:00
|
|
|
#include "llvm/Transforms/MethodInlining.h"
|
|
|
|
#include "llvm/Transforms/SymbolStripping.h"
|
2002-01-22 01:04:08 +00:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2002-01-21 07:52:35 +00:00
|
|
|
#include "llvm/Transforms/IPO/SimpleStructMutation.h"
|
2001-11-26 19:22:39 +00:00
|
|
|
#include "llvm/Transforms/IPO/GlobalDCE.h"
|
2002-01-21 23:17:48 +00:00
|
|
|
#include "llvm/Transforms/Scalar/DCE.h"
|
|
|
|
#include "llvm/Transforms/Scalar/ConstantProp.h"
|
2001-12-04 04:32:04 +00:00
|
|
|
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
|
2001-12-14 16:50:35 +00:00
|
|
|
#include "llvm/Transforms/Scalar/InstructionCombining.h"
|
2002-02-12 17:17:33 +00:00
|
|
|
#include "llvm/Transforms/Scalar/PromoteMemoryToRegister.h"
|
2002-01-21 23:17:48 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
2002-02-26 19:57:59 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/ProfilePaths.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2001-10-18 06:13:08 +00:00
|
|
|
#include <fstream>
|
2001-11-26 19:22:39 +00:00
|
|
|
#include <memory>
|
2001-06-30 06:38:31 +00:00
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Opts enum - All of the transformations we can do...
|
2001-07-23 02:35:57 +00:00
|
|
|
enum Opts {
|
|
|
|
// Basic optimizations
|
2002-03-14 22:36:15 +00:00
|
|
|
dce, die, constprop, inlining, constmerge, strip, mstrip, mergereturn,
|
2001-07-23 02:35:57 +00:00
|
|
|
|
2001-10-18 06:05:15 +00:00
|
|
|
// Miscellaneous Transformations
|
2002-02-26 19:57:59 +00:00
|
|
|
raiseallocs, cleangcc,
|
2002-02-20 17:56:53 +00:00
|
|
|
|
|
|
|
// Printing and verifying...
|
|
|
|
print, verify,
|
2001-10-18 06:05:15 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
// More powerful optimizations
|
2002-02-12 17:17:33 +00:00
|
|
|
indvars, instcombine, sccp, adce, raise, mem2reg,
|
2001-11-26 19:22:39 +00:00
|
|
|
|
2002-02-26 19:57:59 +00:00
|
|
|
// Instrumentation
|
|
|
|
trace, tracem, paths,
|
|
|
|
|
2001-11-26 19:22:39 +00:00
|
|
|
// Interprocedural optimizations...
|
2002-01-21 07:31:50 +00:00
|
|
|
globaldce, swapstructs, sortstructs,
|
2001-07-23 02:35:57 +00:00
|
|
|
};
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
static Pass *createPrintMethodPass() {
|
2002-01-31 00:47:12 +00:00
|
|
|
return new PrintMethodPass("Current Method: \n", &cerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptTable - Correlate enum Opts to Pass constructors...
|
|
|
|
//
|
2001-06-06 20:29:01 +00:00
|
|
|
struct {
|
2001-07-23 02:35:57 +00:00
|
|
|
enum Opts OptID;
|
2002-01-31 00:47:12 +00:00
|
|
|
Pass * (*PassCtor)();
|
2001-06-06 20:29:01 +00:00
|
|
|
} OptTable[] = {
|
2002-02-26 21:46:54 +00:00
|
|
|
{ dce , createDeadCodeEliminationPass },
|
2002-03-14 22:36:15 +00:00
|
|
|
{ die , createDeadInstEliminationPass },
|
2002-02-26 21:46:54 +00:00
|
|
|
{ constprop , createConstantPropogationPass },
|
|
|
|
{ inlining , createMethodInliningPass },
|
|
|
|
{ constmerge , createConstantMergePass },
|
|
|
|
{ strip , createSymbolStrippingPass },
|
|
|
|
{ mstrip , createFullSymbolStrippingPass },
|
|
|
|
{ mergereturn, createUnifyMethodExitNodesPass },
|
|
|
|
|
|
|
|
{ indvars , createIndVarSimplifyPass },
|
|
|
|
{ instcombine, createInstructionCombiningPass },
|
|
|
|
{ sccp , createSCCPPass },
|
|
|
|
{ adce , createAgressiveDCEPass },
|
|
|
|
{ raise , createRaisePointerReferencesPass },
|
2002-02-12 17:17:33 +00:00
|
|
|
{ mem2reg , newPromoteMemoryToRegister },
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
{ trace , createTraceValuesPassForBasicBlocks },
|
|
|
|
{ tracem , createTraceValuesPassForMethod },
|
2002-02-26 20:04:59 +00:00
|
|
|
{ paths , createProfilePathsPass },
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
{ print , createPrintMethodPass },
|
2002-02-20 17:56:53 +00:00
|
|
|
{ verify , createVerifierPass },
|
2002-02-26 20:04:59 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
{ raiseallocs, createRaiseAllocationsPass },
|
|
|
|
{ cleangcc , createCleanupGCCOutputPass },
|
|
|
|
{ globaldce , createGlobalDCEPass },
|
|
|
|
{ swapstructs, createSwapElementsPass },
|
|
|
|
{ sortstructs, createSortElementsPass },
|
2001-06-06 20:29:01 +00:00
|
|
|
};
|
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Command line option handling code...
|
|
|
|
//
|
2001-07-23 20:22:30 +00:00
|
|
|
cl::String InputFilename ("", "Load <arg> file to optimize", cl::NoFlags, "-");
|
|
|
|
cl::String OutputFilename("o", "Override output filename", cl::NoFlags, "");
|
|
|
|
cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false);
|
2002-01-31 00:47:12 +00:00
|
|
|
cl::Flag PrintEachXForm("p", "Print module after each transformation");
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
|
2001-07-23 20:22:30 +00:00
|
|
|
cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
|
2001-07-23 02:35:57 +00:00
|
|
|
cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(dce , "Dead Code Elimination"),
|
2002-03-14 22:36:15 +00:00
|
|
|
clEnumVal(die , "Dead Instruction Elimination"),
|
2002-02-01 04:54:11 +00:00
|
|
|
clEnumVal(constprop , "Simple constant propogation"),
|
|
|
|
clEnumValN(inlining , "inline", "Method integration"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(constmerge , "Merge identical global constants"),
|
2002-02-01 04:54:11 +00:00
|
|
|
clEnumVal(strip , "Strip symbols"),
|
|
|
|
clEnumVal(mstrip , "Strip module symbols"),
|
|
|
|
clEnumVal(mergereturn, "Unify method exit nodes"),
|
|
|
|
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(indvars , "Simplify Induction Variables"),
|
2002-01-31 00:47:12 +00:00
|
|
|
clEnumVal(instcombine, "Combine redundant instructions"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
|
|
|
|
clEnumVal(adce , "Agressive DCE"),
|
2002-02-12 17:17:33 +00:00
|
|
|
clEnumVal(mem2reg , "Promote alloca locations to registers"),
|
2001-11-10 07:16:10 +00:00
|
|
|
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(globaldce , "Remove unreachable globals"),
|
2001-11-26 19:22:39 +00:00
|
|
|
clEnumVal(swapstructs, "Swap structure types around"),
|
2002-01-21 07:31:50 +00:00
|
|
|
clEnumVal(sortstructs, "Sort structure elements"),
|
2001-11-26 19:22:39 +00:00
|
|
|
|
2002-01-22 00:13:51 +00:00
|
|
|
clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(cleangcc , "Cleanup GCC Output"),
|
|
|
|
clEnumVal(raise , "Raise to Higher Level"),
|
|
|
|
clEnumVal(trace , "Insert BB & Method trace code"),
|
|
|
|
clEnumVal(tracem , "Insert Method trace code only"),
|
2002-02-26 19:57:59 +00:00
|
|
|
clEnumVal(paths , "Insert path profiling instrumentation"),
|
2001-12-14 16:50:35 +00:00
|
|
|
clEnumVal(print , "Print working method to stderr"),
|
2002-02-20 17:56:53 +00:00
|
|
|
clEnumVal(verify , "Verify module is well formed"),
|
2001-07-23 02:35:57 +00:00
|
|
|
0);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
" llvm .bc -> .bc modular optimizer\n");
|
2002-01-31 00:47:12 +00:00
|
|
|
|
|
|
|
// Load the input module...
|
2001-11-26 19:22:39 +00:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
|
|
|
if (M.get() == 0) {
|
2001-06-06 20:29:01 +00:00
|
|
|
cerr << "bytecode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Figure out what stream we are supposed to write to...
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout...
|
2001-07-23 19:27:24 +00:00
|
|
|
if (OutputFilename != "") {
|
2002-01-22 21:07:24 +00:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-20 22:54:45 +00:00
|
|
|
// If force is not specified, make sure not to overwrite a file!
|
|
|
|
cerr << "Error opening '" << OutputFilename << "': File exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
if (!Out->good()) {
|
2001-07-23 19:27:24 +00:00
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Create a PassManager to hold and optimize the collection of passes we are
|
|
|
|
// about to build...
|
|
|
|
//
|
|
|
|
PassManager Passes;
|
|
|
|
|
|
|
|
// Create a new optimization pass for each one specified on the command line
|
|
|
|
for (unsigned i = 0; i < OptimizationList.size(); ++i) {
|
|
|
|
enum Opts Opt = OptimizationList[i];
|
|
|
|
for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
|
|
|
|
if (Opt == OptTable[j].OptID) {
|
|
|
|
Passes.add(OptTable[j].PassCtor());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintEachXForm)
|
|
|
|
Passes.add(new PrintModulePass(&std::cerr));
|
|
|
|
}
|
|
|
|
|
2002-02-20 17:56:53 +00:00
|
|
|
// Check that the module is well formed on completion of optimization
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Write bytecode out to disk or cout as the last step...
|
|
|
|
Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
|
|
|
|
|
|
|
|
// Now that we have all of the passes ready, run them.
|
|
|
|
if (Passes.run(M.get()) && !Quiet)
|
|
|
|
cerr << "Program modified.\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|