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"
|
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Bytecode/Writer.h"
|
2001-06-30 06:38:31 +00:00
|
|
|
#include "llvm/Optimizations/AllOpts.h"
|
2001-10-18 06:05:15 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
2001-10-19 15:39:14 +00:00
|
|
|
#include "llvm/Assembly/PrintModulePass.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"
|
2001-11-10 07:16:10 +00:00
|
|
|
#include "llvm/Transforms/SwapStructContents.h"
|
2001-11-26 19:22:39 +00:00
|
|
|
#include "llvm/Transforms/IPO/GlobalDCE.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"
|
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
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
enum Opts {
|
|
|
|
// Basic optimizations
|
2001-12-14 16:50:35 +00:00
|
|
|
dce, constprop, inlining, constmerge, strip, mstrip,
|
2001-07-23 02:35:57 +00:00
|
|
|
|
2001-10-18 06:05:15 +00:00
|
|
|
// Miscellaneous Transformations
|
2001-11-26 19:22:39 +00:00
|
|
|
trace, tracem, print, cleangcc,
|
2001-10-18 06:05:15 +00:00
|
|
|
|
2001-07-23 02:35:57 +00:00
|
|
|
// More powerful optimizations
|
2001-12-14 16:50:35 +00:00
|
|
|
indvars, instcombine, sccp, adce, raise,
|
2001-11-26 19:22:39 +00:00
|
|
|
|
|
|
|
// Interprocedural optimizations...
|
|
|
|
globaldce, swapstructs,
|
2001-07-23 02:35:57 +00:00
|
|
|
};
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
struct {
|
2001-07-23 02:35:57 +00:00
|
|
|
enum Opts OptID;
|
2001-10-18 01:31:43 +00:00
|
|
|
Pass *ThePass;
|
2001-06-06 20:29:01 +00:00
|
|
|
} OptTable[] = {
|
2001-11-26 18:18:53 +00:00
|
|
|
{ swapstructs, 0 },
|
2001-12-14 16:50:35 +00:00
|
|
|
{ dce , new opt::DeadCodeElimination() },
|
|
|
|
{ constprop , new opt::ConstantPropogation() },
|
|
|
|
{ inlining , new opt::MethodInlining() },
|
|
|
|
{ constmerge , new ConstantMerge() },
|
|
|
|
{ strip , new opt::SymbolStripping() },
|
|
|
|
{ mstrip , new opt::FullSymbolStripping() },
|
|
|
|
{ indvars , new InductionVariableSimplify() },
|
|
|
|
{ instcombine, new InstructionCombining() },
|
|
|
|
{ sccp , new opt::SCCPPass() },
|
|
|
|
{ adce , new opt::AgressiveDCE() },
|
|
|
|
{ raise , new RaisePointerReferences() },
|
|
|
|
{ trace , new InsertTraceCode(true, true) },
|
|
|
|
{ tracem , new InsertTraceCode(false, true) },
|
|
|
|
{ print , new PrintModulePass("Current Method: \n",&cerr) },
|
|
|
|
{ cleangcc , new CleanupGCCOutput() },
|
2001-06-06 20:29:01 +00:00
|
|
|
};
|
|
|
|
|
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);
|
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"),
|
|
|
|
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"),
|
|
|
|
clEnumVal(instcombine, "Simplify Induction Variables"),
|
|
|
|
clEnumVal(sccp , "Sparse Conditional Constant Propogation"),
|
|
|
|
clEnumVal(adce , "Agressive DCE"),
|
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"),
|
|
|
|
|
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"),
|
|
|
|
clEnumVal(print , "Print working method to stderr"),
|
2001-07-23 02:35:57 +00:00
|
|
|
0);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-11-26 19:22:39 +00:00
|
|
|
static void RunOptimization(Module *M, enum Opts Opt) {
|
|
|
|
for (unsigned j = 0; j < sizeof(OptTable)/sizeof(OptTable[0]); ++j)
|
|
|
|
if (Opt == OptTable[j].OptID) {
|
|
|
|
if (OptTable[j].ThePass->run(M) && !Quiet)
|
|
|
|
cerr << OptimizationList.getArgName(Opt)
|
|
|
|
<< " pass made modifications!\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special cases that haven't been fit into a consistent framework yet...
|
|
|
|
switch (Opt) {
|
|
|
|
case globaldce: {
|
|
|
|
GlobalDCE GDCE; GDCE.run(M); return;
|
|
|
|
}
|
|
|
|
case swapstructs: {
|
|
|
|
PrebuiltStructMutation SM(M, PrebuiltStructMutation::SortElements);
|
|
|
|
SM.run(M); return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
cerr << "Optimization tables inconsistent!!\n";
|
|
|
|
}
|
|
|
|
}
|
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");
|
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;
|
|
|
|
}
|
|
|
|
|
2001-11-26 19:22:39 +00:00
|
|
|
// Run all of the optimizations specified on the command line
|
|
|
|
for (unsigned i = 0; i < OptimizationList.size(); ++i)
|
|
|
|
RunOptimization(M.get(), OptimizationList[i]);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
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-20 22:54:45 +00:00
|
|
|
if (!Force && !std::ifstream(OutputFilename.c_str())) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we're done now... write out result...
|
2001-11-26 19:22:39 +00:00
|
|
|
WriteBytecodeToFile(M.get(), *Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
if (Out != &cout) delete Out;
|
|
|
|
return 0;
|
|
|
|
}
|