2004-04-02 05:06:57 +00:00
|
|
|
//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
|
2003-10-20 17:47:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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-09-16 16:09:43 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2002-10-29 20:48:09 +00:00
|
|
|
#include "llvm/Target/TargetMachineImpls.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"
|
2004-04-02 05:06:57 +00:00
|
|
|
#include "Support/SystemUtils.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
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
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
|
|
|
//
|
2003-05-22 20:13:16 +00:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 02:10:13 +00:00
|
|
|
InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
|
|
|
|
|
2003-05-22 20:13:16 +00:00
|
|
|
static cl::opt<std::string>
|
2002-07-22 02:10:13 +00:00
|
|
|
OutputFilename("o", cl::desc("Override output filename"),
|
2003-12-10 14:41:33 +00:00
|
|
|
cl::value_desc("filename"), cl::init("-"));
|
2002-07-22 02:10:13 +00:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Force("f", cl::desc("Overwrite output files"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
PrintEachXForm("p", cl::desc("Print module after each transformation"));
|
|
|
|
|
2003-02-12 18:43:33 +00:00
|
|
|
static cl::opt<bool>
|
2003-02-26 20:00:41 +00:00
|
|
|
NoOutput("disable-output",
|
|
|
|
cl::desc("Do not write result bytecode file"), cl::Hidden);
|
2003-02-12 18:43:33 +00:00
|
|
|
|
2003-02-12 18:45:08 +00:00
|
|
|
static cl::opt<bool>
|
2003-02-26 20:00:41 +00:00
|
|
|
NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
|
2003-02-12 18:45:08 +00:00
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
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");
|
2004-02-19 20:32:12 +00:00
|
|
|
PrintStackTraceOnErrorSignal();
|
2002-01-31 00:47:12 +00:00
|
|
|
|
2002-09-16 16:09:43 +00:00
|
|
|
// Allocate a full target machine description only if necessary...
|
|
|
|
// FIXME: The choice of target should be controllable on the command line.
|
|
|
|
std::auto_ptr<TargetMachine> target;
|
|
|
|
|
|
|
|
TargetMachine* TM = NULL;
|
2003-04-16 20:51:36 +00:00
|
|
|
std::string ErrorMessage;
|
2002-09-16 16:09:43 +00:00
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Load the input module...
|
2003-04-16 20:51:36 +00:00
|
|
|
std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
|
2001-11-26 19:22:39 +00:00
|
|
|
if (M.get() == 0) {
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << argv[0] << ": ";
|
2003-04-16 20:51:36 +00:00
|
|
|
if (ErrorMessage.size())
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << ErrorMessage << "\n";
|
2003-04-16 20:51:36 +00:00
|
|
|
else
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << "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...
|
2003-12-10 14:41:33 +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!
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << argv[0] << ": error opening '" << OutputFilename
|
|
|
|
<< "': file exists!\n"
|
|
|
|
<< "Use -f command line argument to force output\n";
|
2002-01-20 22:54:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
Out = new std::ofstream(OutputFilename.c_str());
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
if (!Out->good()) {
|
2003-05-22 20:13:16 +00:00
|
|
|
std::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
|
|
|
|
2003-10-10 17:56:49 +00:00
|
|
|
// Make sure that the Output file gets unlinked from the disk if we get a
|
2002-04-18 19:55:25 +00:00
|
|
|
// SIGINT
|
|
|
|
RemoveFileOnSignal(OutputFilename);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-04-02 05:06:57 +00:00
|
|
|
// If the output is set to be emitted to standard out, and standard out is a
|
|
|
|
// console, print out a warning message and refuse to do it. We don't impress
|
|
|
|
// anyone by spewing tons of binary goo to a terminal.
|
|
|
|
if (Out == &std::cout && isStandardOutAConsole() && !Force && !NoOutput) {
|
|
|
|
std::cerr << "WARNING: It looks like you're attempting to print out a "
|
|
|
|
<< "bytecode file. I'm\ngoing to pretend you didn't ask me to do"
|
|
|
|
<< " this (for your own good). If you\nREALLY want to taste LLVM"
|
|
|
|
<< " bytecode first hand, you can force output with the\n'-f'"
|
|
|
|
<< " option.\n\n";
|
|
|
|
NoOutput = true;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2003-04-24 19:13:02 +00:00
|
|
|
// Add an appropriate TargetData instance for this module...
|
|
|
|
Passes.add(new TargetData("opt", M.get()));
|
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// 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()());
|
2002-09-16 16:09:43 +00:00
|
|
|
else if (Opt->getTargetCtor()) {
|
2003-04-16 23:02:16 +00:00
|
|
|
#if 0
|
2002-09-16 16:09:43 +00:00
|
|
|
if (target.get() == NULL)
|
|
|
|
target.reset(allocateSparcTargetMachine()); // FIXME: target option
|
2003-04-16 23:02:16 +00:00
|
|
|
#endif
|
2002-09-16 16:09:43 +00:00
|
|
|
assert(target.get() && "Could not allocate target machine!");
|
|
|
|
Passes.add(Opt->getTargetCtor()(*target.get()));
|
|
|
|
} else
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName()
|
|
|
|
<< "\n";
|
2002-01-31 00:47:12 +00:00
|
|
|
|
|
|
|
if (PrintEachXForm)
|
2003-05-22 20:13:16 +00:00
|
|
|
Passes.add(new PrintModulePass(&std::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
|
2003-02-12 18:45:08 +00:00
|
|
|
if (!NoVerify)
|
|
|
|
Passes.add(createVerifierPass());
|
2002-02-20 17:56:53 +00:00
|
|
|
|
2002-01-31 00:47:12 +00:00
|
|
|
// Write bytecode out to disk or cout as the last step...
|
2003-02-12 18:43:33 +00:00
|
|
|
if (!NoOutput)
|
|
|
|
Passes.add(new WriteBytecodePass(Out, Out != &std::cout));
|
2002-01-31 00:47:12 +00:00
|
|
|
|
|
|
|
// 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)
|
2003-05-22 20:13:16 +00:00
|
|
|
std::cerr << "Program modified.\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|