2001-10-18 06:05:15 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-30 21:43:25 +00:00
|
|
|
// LLVM Modular Optimizer Utility: opt
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// 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-05-10 15:43:07 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-07-26 21:09:32 +00:00
|
|
|
#include "llvm/Support/PassNameParser.h"
|
2002-04-18 19:55:25 +00:00
|
|
|
#include "Support/Signals.h"
|
2001-10-18 06:13:08 +00:00
|
|
|
#include <fstream>
|
2001-11-26 19:22:39 +00:00
|
|
|
#include <memory>
|
2002-07-23 18:12:22 +00:00
|
|
|
#include <algorithm>
|
2002-06-25 21:43:28 +00:00
|
|
|
|
|
|
|
using std::cerr;
|
2002-07-25 16:31:09 +00:00
|
|
|
using std::string;
|
2002-05-10 15:43:07 +00:00
|
|
|
|
2002-04-13 18:32:47 +00:00
|
|
|
|
2002-07-23 18:12:22 +00:00
|
|
|
// The OptimizationList is automatically populated with registered Passes by the
|
|
|
|
// PassNameParser.
|
2002-01-31 00:47:12 +00:00
|
|
|
//
|
2002-07-26 21:09:32 +00:00
|
|
|
static cl::list<const PassInfo*, bool,
|
|
|
|
FilteredPassNameParser<PassInfo::Optimization> >
|
2002-07-23 18:12:22 +00:00
|
|
|
OptimizationList(cl::desc("Optimizations available:"));
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-04-12 18:21:13 +00:00
|
|
|
|
2002-07-23 18:12:22 +00:00
|
|
|
// Other command line options...
|
2002-01-31 00:47:12 +00:00
|
|
|
//
|
2002-07-22 02:10:13 +00:00
|
|
|
static cl::opt<string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<string>
|
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
PrintEachXForm("p", cl::desc("Print module after each transformation"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2002-07-31 16:52:49 +00:00
|
|
|
Quiet("q", cl::desc("Don't print 'program modified' message"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
|
|
|
static cl::alias
|
|
|
|
QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-07-23 18:12:22 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// main for opt
|
|
|
|
//
|
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
|
|
|
|
2002-07-23 18:12:22 +00:00
|
|
|
// FIXME: This should be parameterizable eventually for different target
|
|
|
|
// types...
|
|
|
|
TargetData TD("opt target");
|
|
|
|
|
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) {
|
2002-07-30 21:43:25 +00:00
|
|
|
cerr << argv[0] << ": bytecode didn't read correctly.\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
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!
|
2002-07-30 21:43:25 +00:00
|
|
|
cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
2002-01-20 22:54:45 +00:00
|
|
|
<< "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()) {
|
2002-07-30 21:43:25 +00:00
|
|
|
cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-04-18 19:55:25 +00:00
|
|
|
|
|
|
|
// Make sure that the Output file gets unlink'd from the disk if we get a
|
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2002-07-23 18:12:22 +00:00
|
|
|
const PassInfo *Opt = OptimizationList[i];
|
|
|
|
|
|
|
|
if (Opt->getNormalCtor())
|
|
|
|
Passes.add(Opt->getNormalCtor()());
|
|
|
|
else if (Opt->getDataCtor())
|
|
|
|
Passes.add(Opt->getDataCtor()(TD)); // Pass dummy target data...
|
|
|
|
else
|
2002-07-30 21:43:25 +00:00
|
|
|
cerr << argv[0] << ": cannot create pass: " << Opt->getPassName() << "\n";
|
2002-01-31 00:47:12 +00:00
|
|
|
|
|
|
|
if (PrintEachXForm)
|
2002-07-23 18:12:22 +00:00
|
|
|
Passes.add(new PrintModulePass(&cerr));
|
2002-01-31 00:47:12 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2002-06-25 16:13:24 +00:00
|
|
|
if (Passes.run(*M.get()) && !Quiet)
|
2002-01-31 00:47:12 +00:00
|
|
|
cerr << "Program modified.\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|