2001-10-18 14:05:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08: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 14:05:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-01-31 08:47:12 +08:00
|
|
|
#include "llvm/PassManager.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2002-01-31 08:47:12 +08:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-10-19 23:39:14 +08:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2001-10-19 04:06:45 +08:00
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
2001-10-31 12:29:44 +08:00
|
|
|
#include "llvm/Transforms/CleanupGCCOutput.h"
|
2001-11-01 10:41:09 +08:00
|
|
|
#include "llvm/Transforms/LevelChange.h"
|
2002-01-22 07:17:48 +08:00
|
|
|
#include "llvm/Transforms/MethodInlining.h"
|
|
|
|
#include "llvm/Transforms/SymbolStripping.h"
|
2002-01-22 09:04:08 +08:00
|
|
|
#include "llvm/Transforms/ChangeAllocations.h"
|
2002-01-21 15:52:35 +08:00
|
|
|
#include "llvm/Transforms/IPO/SimpleStructMutation.h"
|
2001-11-27 03:22:39 +08:00
|
|
|
#include "llvm/Transforms/IPO/GlobalDCE.h"
|
2002-01-22 07:17:48 +08:00
|
|
|
#include "llvm/Transforms/Scalar/DCE.h"
|
|
|
|
#include "llvm/Transforms/Scalar/ConstantProp.h"
|
2001-12-04 12:32:04 +08:00
|
|
|
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
|
2001-12-15 00:50:35 +08:00
|
|
|
#include "llvm/Transforms/Scalar/InstructionCombining.h"
|
2002-01-22 07:17:48 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2001-10-18 14:13:08 +08:00
|
|
|
#include <fstream>
|
2001-11-27 03:22:39 +08:00
|
|
|
#include <memory>
|
2001-06-30 14:38:31 +08:00
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Opts enum - All of the transformations we can do...
|
2001-07-23 10:35:57 +08:00
|
|
|
enum Opts {
|
|
|
|
// Basic optimizations
|
2001-12-15 00:50:35 +08:00
|
|
|
dce, constprop, inlining, constmerge, strip, mstrip,
|
2001-07-23 10:35:57 +08:00
|
|
|
|
2001-10-18 14:05:15 +08:00
|
|
|
// Miscellaneous Transformations
|
2002-01-22 08:13:51 +08:00
|
|
|
trace, tracem, print, raiseallocs, cleangcc,
|
2001-10-18 14:05:15 +08:00
|
|
|
|
2001-07-23 10:35:57 +08:00
|
|
|
// More powerful optimizations
|
2001-12-15 00:50:35 +08:00
|
|
|
indvars, instcombine, sccp, adce, raise,
|
2001-11-27 03:22:39 +08:00
|
|
|
|
|
|
|
// Interprocedural optimizations...
|
2002-01-21 15:31:50 +08:00
|
|
|
globaldce, swapstructs, sortstructs,
|
2001-07-23 10:35:57 +08:00
|
|
|
};
|
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
|
|
|
|
// New template functions - Provide functions that return passes of specified
|
|
|
|
// types, with specified arguments...
|
|
|
|
//
|
|
|
|
template<class PassClass>
|
|
|
|
Pass *New() {
|
|
|
|
return new PassClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class PassClass, typename ArgTy1, ArgTy1 Arg1>
|
|
|
|
Pass *New() {
|
|
|
|
return new PassClass(Arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class PassClass, typename ArgTy1, ArgTy1 Arg1,
|
|
|
|
typename ArgTy2, ArgTy1 Arg2>
|
|
|
|
Pass *New() {
|
|
|
|
return new PassClass(Arg1, Arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pass *NewPrintMethodPass() {
|
|
|
|
return new PrintMethodPass("Current Method: \n", &cerr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// OptTable - Correlate enum Opts to Pass constructors...
|
|
|
|
//
|
2001-06-07 04:29:01 +08:00
|
|
|
struct {
|
2001-07-23 10:35:57 +08:00
|
|
|
enum Opts OptID;
|
2002-01-31 08:47:12 +08:00
|
|
|
Pass * (*PassCtor)();
|
2001-06-07 04:29:01 +08:00
|
|
|
} OptTable[] = {
|
2002-01-31 08:47:12 +08:00
|
|
|
{ dce , New<DeadCodeElimination> },
|
|
|
|
{ constprop , New<ConstantPropogation> },
|
|
|
|
{ inlining , New<MethodInlining> },
|
|
|
|
{ constmerge , New<ConstantMerge> },
|
|
|
|
{ strip , New<SymbolStripping> },
|
|
|
|
{ mstrip , New<FullSymbolStripping> },
|
|
|
|
{ indvars , New<InductionVariableSimplify> },
|
|
|
|
{ instcombine, New<InstructionCombining> },
|
|
|
|
{ sccp , New<SCCPPass> },
|
|
|
|
{ adce , New<AgressiveDCE> },
|
|
|
|
{ raise , New<RaisePointerReferences> },
|
|
|
|
{ trace , New<InsertTraceCode, bool, true, bool, true> },
|
|
|
|
{ tracem , New<InsertTraceCode, bool, false, bool, true> },
|
|
|
|
{ print , NewPrintMethodPass },
|
|
|
|
{ raiseallocs, New<RaiseAllocations> },
|
|
|
|
{ cleangcc , New<CleanupGCCOutput> },
|
|
|
|
{ globaldce , New<GlobalDCE> },
|
|
|
|
{ swapstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
|
|
|
|
SimpleStructMutation::SwapElements>},
|
|
|
|
{ sortstructs, New<SimpleStructMutation, SimpleStructMutation::Transform,
|
|
|
|
SimpleStructMutation::SortElements>},
|
2001-06-07 04:29:01 +08:00
|
|
|
};
|
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Command line option handling code...
|
|
|
|
//
|
2001-07-24 04:22:30 +08: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 08:47:12 +08:00
|
|
|
cl::Flag PrintEachXForm("p", "Print module after each transformation");
|
2001-07-23 10:35:57 +08:00
|
|
|
cl::Flag Quiet ("q", "Don't print modifying pass names", 0, false);
|
2001-07-24 04:22:30 +08:00
|
|
|
cl::Alias QuietA ("quiet", "Alias for -q", cl::NoFlags, Quiet);
|
2001-07-23 10:35:57 +08:00
|
|
|
cl::EnumList<enum Opts> OptimizationList(cl::NoFlags,
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(dce , "Dead Code Elimination"),
|
|
|
|
clEnumVal(constprop , "Simple Constant Propogation"),
|
|
|
|
clEnumValN(inlining , "inline", "Method Integration"),
|
|
|
|
clEnumVal(constmerge , "Merge identical global constants"),
|
|
|
|
clEnumVal(strip , "Strip Symbols"),
|
|
|
|
clEnumVal(mstrip , "Strip Module Symbols"),
|
|
|
|
clEnumVal(indvars , "Simplify Induction Variables"),
|
2002-01-31 08:47:12 +08:00
|
|
|
clEnumVal(instcombine, "Combine redundant instructions"),
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
|
|
|
|
clEnumVal(adce , "Agressive DCE"),
|
2001-11-10 15:16:10 +08:00
|
|
|
|
2001-12-15 00:50:35 +08:00
|
|
|
clEnumVal(globaldce , "Remove unreachable globals"),
|
2001-11-27 03:22:39 +08:00
|
|
|
clEnumVal(swapstructs, "Swap structure types around"),
|
2002-01-21 15:31:50 +08:00
|
|
|
clEnumVal(sortstructs, "Sort structure elements"),
|
2001-11-27 03:22:39 +08:00
|
|
|
|
2002-01-22 08:13:51 +08:00
|
|
|
clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
|
2001-12-15 00:50:35 +08: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"),
|
|
|
|
clEnumVal(print , "Print working method to stderr"),
|
2001-07-23 10:35:57 +08:00
|
|
|
0);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-07-23 10:35:57 +08:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
|
|
|
" llvm .bc -> .bc modular optimizer\n");
|
2002-01-31 08:47:12 +08:00
|
|
|
|
|
|
|
// Load the input module...
|
2001-11-27 03:22:39 +08:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
|
|
|
|
if (M.get() == 0) {
|
2001-06-07 04:29:01 +08:00
|
|
|
cerr << "bytecode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-01-31 08:47:12 +08:00
|
|
|
// Figure out what stream we are supposed to write to...
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostream *Out = &std::cout; // Default to printing to stdout...
|
2001-07-24 03:27:24 +08:00
|
|
|
if (OutputFilename != "") {
|
2002-01-23 05:07:24 +08:00
|
|
|
if (!Force && std::ifstream(OutputFilename.c_str())) {
|
2002-01-21 06:54:45 +08: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-07 04:29:01 +08:00
|
|
|
if (!Out->good()) {
|
2001-07-24 03:27:24 +08:00
|
|
|
cerr << "Error opening " << OutputFilename << "!\n";
|
2001-06-07 04:29:01 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-31 08:47:12 +08: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07 04:29:01 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|